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コンポーネントが最後のコンポーネントの右/下に追加されることを忘れないでください....

例えば...

ここに画像の説明を入力

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 に答える
1

fillerが単なる JLabel の場合、次のことができます。

filler.setText("add text here");

または、別のコンポーネントを置き換える場合は、カード レイアウトを使用するパネルを作成することをお勧めします。次に、2 つのコンポーネントを交換するだけです。詳細については、カード レイアウトの使用方法に関する 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 に答える