0
jPanel1 = new JPanel();
GridBagLayout  layout = new GridBagLayout();
jPanel1.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();


 filler = new JLabel();
 gbc.gridx = 1;
 gbc.gridy = 1;
 gbc.weightx = 1;
 gbc.weighty = 1;

jPanel1.add(filler, gbc);    

我试图通过执行 jPanel1.remove(filler) 来删除,然后在该位置放置一个新的 JLabel,但显然不起作用。我究竟做错了什么?

谢谢!

4

2 回答 2

1

如果filler只是一个 JLabel,那么你可以这样做

filler.setText("add text here");

或者,如果您要更换不同的组件,更好的方法是创建一个使用卡片布局的面板。然后你可以交换这两个组件。有关更多信息,请参阅有关如何使用卡片布局的 Swing 教程。

另一种选择可能是执行以下操作:

GridBagLayout layout = (GridBagLayout)jPanel1.getLayout();
GridbagConstraint gbc = layout.getConstraint(oldComponent);
jPanel1.remove(oldComponent);
jPanel1.add(newComponent, gbc);
jPanel1.revalidate();
jPanel1.repaint();
于 2013-08-28T03:37:14.927 回答
1

至于你为什么会遇到问题,我只能想象。

不要忘记该filler组件应该添加到最后一个组件的右侧/底部......

例如...

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class GridBagLayoutTest {

    public static void main(String[] args) {
        new GridBagLayoutTest();
    }

    public GridBagLayoutTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final TestPane pane = new TestPane();
                JButton btn = new JButton("Add");
                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        pane.addNewItem();
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(pane);
                frame.add(btn, BorderLayout.SOUTH);
                frame.pack();
                frame.setSize(300, 300);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int gridX = 0;
        private int gridY = 0;
        private JLabel filler;

        private int columnCount = 4;

        public TestPane() {
            setLayout(new GridBagLayout());
            filler = new JLabel();
        }

        public void addNewItem() {

            remove(filler);

            JLabel label = new JLabel("Cell " + gridX + "x" + gridY);
            label.setBorder(new EmptyBorder(10, 10, 10, 10));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = gridX;
            gbc.gridy = gridY;
            add(label, gbc);
            gridX++;
            if (gridX >= columnCount) {
                gridX = 0;
                gridY++;
            }

            gbc = new GridBagConstraints();
            gbc.gridx = columnCount;
            gbc.gridy = gridY + 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            add(filler, gbc);

            revalidate();
            repaint();

        }

    }

}
于 2013-08-28T03:44:05.097 回答