首次执行后布局方法中断

时间:2013-01-06 07:32:03

标签: java swing layout-manager gridbaglayout

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();
        }

屏幕截图

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

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

1 个答案:

答案 0 :(得分:3)

添加到我的评论中:

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

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();
    }
}