1

当该addComponents()方法运行一次时,我得到了所需的 JPanel 布局。但是,当里面的代码addComponents()被多次执行时,JPanel 的布局就完全错误了。有什么我似乎做错了吗?

public class DeleteStudent extends JPanel {

        public SearchPanel search = new SearchPanel();
        private final JButton deleteButton = new JButton("Delete from database");
        private GridBagConstraints cons = new GridBagConstraints();
        private final GridBagLayout gridBag = new GridBagLayout();

        public DeleteStudent() {
        super();
        setLayout(gridBag);
        setPreferredSize(new Dimension(400, 300));

        addComponents();
        addComponents(); //Method fails when run more than once!
        }

        public void addComponents() {
        cons.gridy = 1;
        cons.insets = new Insets(50, 0, 0, 0);
        gridBag.setConstraints(deleteButton, cons);

        removeAll();
        add(search);
        add(deleteButton);
        update();
        }

        private void update() {
        revalidate();
        repaint();
        }

截图

1次方法调用后的JPanel:http: //img402.imageshack.us/img402/6409/oncer.png

2 次方法调用后的 JPanel:http: //imageshack.us/scaled/landing/254/twiced.png

4

1 回答 1

3

添加到我的评论:

似乎问题是您在调用之前设置JPanelsGridBagLayout约束removeAll()应该在调用之后完成,removeAll();这样当我们添加新组件时,LayoutManager它仍然有效并且尚未重置为其默认值。

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

    public Test() {
        createAndShowGui();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DeleteStudent());
        frame.pack();
        frame.setVisible(true);
    }
}

class DeleteStudent extends JPanel {

    public JPanel search = new JPanel() {//for testing
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    };
    private final JButton deleteButton = new JButton("Delete from database");
    private GridBagConstraints cons = new GridBagConstraints();
    private final GridBagLayout gridBag = new GridBagLayout();

    public DeleteStudent() {
        super();
        setLayout(gridBag);

        addComponents();
        addComponents(); //Method fails when run more than once!
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 300);
    }

    public void addComponents() {
        removeAll();//must call this before resetting Layout and adding new components

        cons.gridy = 1;
        cons.insets = new Insets(50, 0, 0, 0);
        gridBag.setConstraints(deleteButton, cons);

        add(search);
        add(deleteButton);
        update();
    }

    private void update() {
        revalidate();
        repaint();
    }
}
于 2013-01-06T07:47:06.483 回答