Java 不可见组件仍然占用空间JPanel
在一个JPanel集合中,我有一系列组件在彼此下面,作为一个GridLayout。 我需要暂时隐藏组件,但是Java 不可见组件仍然占用空间JPanel,java,swing,user-interface,jpanel,grid-layout,Java,Swing,User Interface,Jpanel,Grid Layout,在一个JPanel集合中,我有一系列组件在彼此下面,作为一个GridLayout。 我需要暂时隐藏组件,但是setVisible(false)无法将其剪切,因为组件所在的位置仍然有一个空白 有没有一个快速简单的方法来做到这一点?还是我必须保存JPanel的状态,删除组件,然后恢复它 SSCCE: [GridLayout2.java] import java.awt.Component; import java.awt.Container; import java.awt.Dimension; i
setVisible(false)
无法将其剪切,因为组件所在的位置仍然有一个空白
有没有一个快速简单的方法来做到这一点?还是我必须保存JPanel的状态,删除组件,然后恢复它
SSCCE:
[GridLayout2.java]
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
public class GridLayout2 extends GridLayout
{
public GridLayout2() {
this(1, 0, 0, 0);
}
public GridLayout2(int rows, int cols) {
this(rows, cols, 0, 0);
}
public GridLayout2(int rows, int cols, int hgap, int vgap) {
super(rows, cols, hgap, vgap);
}
public Dimension preferredLayoutSize(Container parent) {
//System.err.println("preferredLayoutSize");
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = getRows();
int ncols = getColumns();
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
}
else {
nrows = (ncomponents + ncols - 1) / ncols;
}
int[] w = new int[ncols];
int[] h = new int[nrows];
for (int i = 0; i < ncomponents; i ++) {
int r = i / ncols;
int c = i % ncols;
Component comp = parent.getComponent(i);
Dimension d = comp.getPreferredSize();
if (w[c] < d.width) {
w[c] = d.width;
}
if (h[r] < d.height) {
h[r] = d.height;
}
}
int nw = 0;
for (int j = 0; j < ncols; j ++) {
nw += w[j];
}
int nh = 0;
for (int i = 0; i < nrows; i ++) {
nh += h[i];
}
return new Dimension(insets.left + insets.right + nw + (ncols-1)*getHgap(),
insets.top + insets.bottom + nh + (nrows-1)*getVgap());
}
}
public Dimension minimumLayoutSize(Container parent) {
System.err.println("minimumLayoutSize");
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = getRows();
int ncols = getColumns();
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
}
else {
nrows = (ncomponents + ncols - 1) / ncols;
}
int[] w = new int[ncols];
int[] h = new int[nrows];
for (int i = 0; i < ncomponents; i ++) {
int r = i / ncols;
int c = i % ncols;
Component comp = parent.getComponent(i);
Dimension d = comp.getMinimumSize();
if (w[c] < d.width) {
w[c] = d.width;
}
if (h[r] < d.height) {
h[r] = d.height;
}
}
int nw = 0;
for (int j = 0; j < ncols; j ++) {
nw += w[j];
}
int nh = 0;
for (int i = 0; i < nrows; i ++) {
nh += h[i];
}
return new Dimension(insets.left + insets.right + nw + (ncols-1)*getHgap(),
insets.top + insets.bottom + nh + (nrows-1)*getVgap());
}
}
public void layoutContainer(Container parent) {
//System.err.println("layoutContainer");
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = getRows();
int ncols = getColumns();
if (ncomponents == 0) {
return;
}
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
}
else {
nrows = (ncomponents + ncols - 1) / ncols;
}
int hgap = getHgap();
int vgap = getVgap();
// scaling factors
Dimension pd = preferredLayoutSize(parent);
double sw = (1.0 * parent.getWidth()) / pd.width;
double sh = (1.0 * parent.getHeight()) / pd.height;
// scale
int[] w = new int[ncols];
int[] h = new int[nrows];
for (int i = 0; i < ncomponents; i ++) {
int r = i / ncols;
int c = i % ncols;
Component comp = parent.getComponent(i);
Dimension d = comp.getPreferredSize();
d.width = (int) (sw * d.width);
d.height = (int) (sh * d.height);
if (w[c] < d.width) {
w[c] = d.width;
}
if (h[r] < d.height) {
h[r] = d.height;
}
}
for (int c = 0, x = insets.left; c < ncols; c ++) {
for (int r = 0, y = insets.top; r < nrows; r ++) {
int i = r * ncols + c;
if (i < ncomponents) {
parent.getComponent(i).setBounds(x, y, w[c], h[r]);
}
y += h[r] + vgap;
}
x += w[c] + hgap;
}
}
}
}
import java.awt.Color;
import javax.swing.*;
import javax.swing.border.*;
public class SSCCE extends JFrame{
JPanel innerPane = new JPanel();
JScrollPane scr = new JScrollPane(innerPane);
public static void main(String[] args) {
new SSCCE();
}
public SSCCE() {
setSize(400, 800);
innerPane.setLayout(new GridLayout2(0, 1));
add(scr);
for (int i = 0; i < 30; i++)
{
innerPane.add(getPane());
}
setVisible(true);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
for (int i = 0; i < 30; i++)
{
if (i%2==0)
innerPane.getComponent(i).setVisible(false);
}
}
private JPanel getPane()
{
JPanel ret = new JPanel();
JLabel lbl = new JLabel("This is a pane.");
ret.add(lbl);
ret.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
ret.setBackground(Color.gray);
return ret;
}
}
导入java.awt.Component;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.awt.GridLayout;
导入java.awt.Insets;
公共类GridLayout2扩展了GridLayout
{
公共网格布局2(){
这(1,0,0,0);
}
公共网格布局2(int行、int列){
这(行、列、0、0);
}
公共网格布局2(int行、int列、int hgap、int vgap){
超级(行、列、hgap、vgap);
}
公共维度preferredLayoutSize(容器父级){
//System.err.println(“preferredLayoutSize”);
已同步(父级.getTreeLock()){
Insets Insets=parent.getInsets();
int ncomponents=parent.getComponentCount();
int nrows=getRows();
int ncols=getColumns();
如果(nrows>0){
ncols=(ncomponents+nrows-1)/nrows;
}
否则{
nrows=(ncomponents+ncols-1)/ncols;
}
int[]w=新的int[ncols];
int[]h=新的int[nrows];
对于(int i=0;i0){
ncols=(ncomponents+nrows-1)/nrows;
}
否则{
nrows=(ncomponents+ncols-1)/ncols;
}
int[]w=新的int[ncols];
int[]h=新的int[nrows];
对于(int i=0;i0){
ncols=(ncomponents+nrows-1)/nrows;
}
否则{
nrows=(ncomponents+ncols-1)/ncols;
}
int hgap=getHgap();
int vgap=getVgap();
//比例因子
维度pd=首选布局大小(父级);
double sw=(1.0*parent.getWidth())/pd.width;
double sh=(1.0*parent.getHeight())/pd.height;
//鳞片
int[]w=新的int[ncols];
int[]h=新的int[nrows];
对于(int i=0;i
[SSCCE.java]
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
public class GridLayout2 extends GridLayout
{
public GridLayout2() {
this(1, 0, 0, 0);
}
public GridLayout2(int rows, int cols) {
this(rows, cols, 0, 0);
}
public GridLayout2(int rows, int cols, int hgap, int vgap) {
super(rows, cols, hgap, vgap);
}
public Dimension preferredLayoutSize(Container parent) {
//System.err.println("preferredLayoutSize");
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = getRows();
int ncols = getColumns();
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
}
else {
nrows = (ncomponents + ncols - 1) / ncols;
}
int[] w = new int[ncols];
int[] h = new int[nrows];
for (int i = 0; i < ncomponents; i ++) {
int r = i / ncols;
int c = i % ncols;
Component comp = parent.getComponent(i);
Dimension d = comp.getPreferredSize();
if (w[c] < d.width) {
w[c] = d.width;
}
if (h[r] < d.height) {
h[r] = d.height;
}
}
int nw = 0;
for (int j = 0; j < ncols; j ++) {
nw += w[j];
}
int nh = 0;
for (int i = 0; i < nrows; i ++) {
nh += h[i];
}
return new Dimension(insets.left + insets.right + nw + (ncols-1)*getHgap(),
insets.top + insets.bottom + nh + (nrows-1)*getVgap());
}
}
public Dimension minimumLayoutSize(Container parent) {
System.err.println("minimumLayoutSize");
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = getRows();
int ncols = getColumns();
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
}
else {
nrows = (ncomponents + ncols - 1) / ncols;
}
int[] w = new int[ncols];
int[] h = new int[nrows];
for (int i = 0; i < ncomponents; i ++) {
int r = i / ncols;
int c = i % ncols;
Component comp = parent.getComponent(i);
Dimension d = comp.getMinimumSize();
if (w[c] < d.width) {
w[c] = d.width;
}
if (h[r] < d.height) {
h[r] = d.height;
}
}
int nw = 0;
for (int j = 0; j < ncols; j ++) {
nw += w[j];
}
int nh = 0;
for (int i = 0; i < nrows; i ++) {
nh += h[i];
}
return new Dimension(insets.left + insets.right + nw + (ncols-1)*getHgap(),
insets.top + insets.bottom + nh + (nrows-1)*getVgap());
}
}
public void layoutContainer(Container parent) {
//System.err.println("layoutContainer");
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = getRows();
int ncols = getColumns();
if (ncomponents == 0) {
return;
}
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
}
else {
nrows = (ncomponents + ncols - 1) / ncols;
}
int hgap = getHgap();
int vgap = getVgap();
// scaling factors
Dimension pd = preferredLayoutSize(parent);
double sw = (1.0 * parent.getWidth()) / pd.width;
double sh = (1.0 * parent.getHeight()) / pd.height;
// scale
int[] w = new int[ncols];
int[] h = new int[nrows];
for (int i = 0; i < ncomponents; i ++) {
int r = i / ncols;
int c = i % ncols;
Component comp = parent.getComponent(i);
Dimension d = comp.getPreferredSize();
d.width = (int) (sw * d.width);
d.height = (int) (sh * d.height);
if (w[c] < d.width) {
w[c] = d.width;
}
if (h[r] < d.height) {
h[r] = d.height;
}
}
for (int c = 0, x = insets.left; c < ncols; c ++) {
for (int r = 0, y = insets.top; r < nrows; r ++) {
int i = r * ncols + c;
if (i < ncomponents) {
parent.getComponent(i).setBounds(x, y, w[c], h[r]);
}
y += h[r] + vgap;
}
x += w[c] + hgap;
}
}
}
}
import java.awt.Color;
import javax.swing.*;
import javax.swing.border.*;
public class SSCCE extends JFrame{
JPanel innerPane = new JPanel();
JScrollPane scr = new JScrollPane(innerPane);
public static void main(String[] args) {
new SSCCE();
}
public SSCCE() {
setSize(400, 800);
innerPane.setLayout(new GridLayout2(0, 1));
add(scr);
for (int i = 0; i < 30; i++)
{
innerPane.add(getPane());
}
setVisible(true);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
for (int i = 0; i < 30; i++)
{
if (i%2==0)
innerPane.getComponent(i).setVisible(false);
}
}
private JPanel getPane()
{
JPanel ret = new JPanel();
JLabel lbl = new JLabel("This is a pane.");
ret.add(lbl);
ret.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
ret.setBackground(Color.gray);
return ret;
}
}
导入java.awt.Color;
导入javax.swing.*;
导入javax.swing.border.*;
公共类SSCCE扩展JFrame{
JPanel innerPane=新的JPanel();
JScrollPane scr=新的JScrollPane(内部窗格);
公共静态void main(字符串[]args){
新SSCCE();
}
公共服务{
设置大小(400800);
setLayout(新的GridLayout2(0,1));
添加(scr);
对于(int i=0;i<30;i++)
{
添加(getPane());
}
setVisible(真);
试一试{
《睡眠》(2000年);
}捕获(中断异常e){}
对于(int i=0;i<30;i++)
{
如果(i%2==0)
innerPane.getComponent(i).setVisible(false);
}
}
私有JPanel getPane()
{
JPanel ret=新的JPanel();
JLabel lbl=新的JLabel(“这是一个窗格”);
重新添加(lbl);
ret.setboorder(BorderFactory.createBevelOrder(BevelOrder.RAISED));
后退地面(颜色为灰色);
返回ret;
}
}
b
button.setVisible(false);
button.setEnabled(false);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DisableComponents {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JPanel gui = new JPanel(new BorderLayout());
JToolBar tb = new JToolBar();
gui.add(tb, BorderLayout.NORTH);
final JButton openTool = new JButton("Open");
final JButton saveTool = new JButton("Save");
tb.add( openTool );
tb.add( saveTool );
JPanel buttonFlow = new JPanel(new FlowLayout(3));
gui.add(buttonFlow, BorderLayout.CENTER);
final JButton openFlow = new JButton("Open");
final JButton saveFlow = new JButton("Save");
buttonFlow.add( openFlow );
buttonFlow.add( saveFlow );
JPanel buttonBox = new JPanel();
gui.add(buttonBox, BorderLayout.EAST);
BoxLayout bl = new BoxLayout(buttonBox, BoxLayout.Y_AXIS);
buttonBox.setLayout(bl);
final JButton openBox = new JButton("Open");
final JButton saveBox = new JButton("Save");
buttonBox.add( openBox );
buttonBox.add( saveBox );
final JCheckBox openChoice = new JCheckBox("Enable open", true);
openChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
openTool.setEnabled(openChoice.isSelected());
openFlow.setEnabled(openChoice.isSelected());
openBox.setEnabled(openChoice.isSelected());
}
});
gui.add(openChoice, BorderLayout.SOUTH);
JOptionPane.showMessageDialog(null, gui);
}
});
}
}
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.*;
public class SSCCE extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel innerPane = new JPanel();
private JScrollPane scr = new JScrollPane(innerPane);
private Timer timer;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SSCCE sSCCE = new SSCCE();
}
});
}
private JPanel getPane() {
JPanel ret = new JPanel();
JLabel lbl = new JLabel("This is a pane.");
ret.add(lbl);
ret.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
ret.setBackground(Color.gray);
return ret;
}
public SSCCE() {
innerPane.setLayout(new GridLayout(0, 1));
add(scr);
for (int i = 0; i < 30; i++) {
innerPane.add(getPane());
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
start();
}
private void start() {
timer = new javax.swing.Timer(2000, updateCol());
timer.start();
timer.setRepeats(false);
}
private Action updateCol() {
return new AbstractAction("Hide Row Action") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 30; i++) {
if (i % 2 == 0) {
innerPane.getComponent(i).setVisible(false);
}
}
}
};
}
}
JPanel innerPane = new JPanel();
BoxLayout innerPaneLayout = new BoxLayout(innerPane,BoxLayout.Y_AXIS);
innerPane.setLayout(innerPaneLayout);
for (int i = 0; i < 30; i++)
{
JPane newPane = getPane();
innerPane.add(newPane);
newPane.setAlignmentY(Component.TOP_ALIGNMENT);
}
for (int i = 0; i < 30; i++)
{
if (i%2==0)
innerPane.getComponent(i).setVisible(false);
}