GridLayout仅在手动调整窗口大小时更新

时间:2015-08-05 20:04:21

标签: java swing jframe jpanel grid-layout

我有一个使用3x3 GridLayout的面板,我想通过单击单选按钮将其更改为4x4布局。这是我设置菜单和所需按钮的面板类。请记住,此代码是纯草稿。我只想尝试使用单选按钮来更新布局。

public class MenuChangesLayoutPanel extends JPanel implements ActionListener{

    private JMenuBar menuBar;
    private JMenu menu;
    private JRadioButtonMenuItem r1, r2;
    private ButtonGroup group;
    private JButton [] b;
    public MenuChangesLayoutPanel(){
        setLayout(new GridLayout(0,3));
        setOpaque(true);
        b=new JButton[9];
        for(int i=0; i<9; i++){
            b[i]=new JButton(" ");
            add(b[i]);
        }
        menuBar=new JMenuBar();
        menu=new JMenu("The Menu");
        r1=new JRadioButtonMenuItem("3x3");
        r2=new JRadioButtonMenuItem("4x4");
        group=new ButtonGroup();
        group.add(r1);
        group.add(r2);
        menuBar.add(menu);
        menu.add(r1);
        menu.add(r2);
        r1.setSelected(true);
        r1.addActionListener(this);
        r2.addActionListener(this);
        this.setPreferredSize(new Dimension(500,500));
    }
    public JMenuBar getBar(){
        return menuBar;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        JButton [] buttons=new JButton[7];
        if(e.getSource()==r2){
            System.out.println("r2 pressed");
            ((GridLayout) this.getLayout()).setRows(4);
            ((GridLayout) this.getLayout()).setRows(4);
            for(int i=0; i<7; i++){
                buttons[i]=new JButton();
                this.add(buttons[i]);
            }
            repaint();
        }
    }
}

框架类:

public class MenuChangesLayout extends JFrame {
    private static final long serialVersionUID = 1L;
    private MenuChangesLayoutPanel panel;

    public MenuChangesLayout(){
        panel=new MenuChangesLayoutPanel();
        this.setJMenuBar(panel.getBar());

        this.setContentPane(panel);

        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MenuChangesLayout();
            }
        });
    }
}

当我运行程序时,我得到了预期的初始效果(3x3网格): enter image description here

但是当我按下4x4按钮时,在我手动调整框架大小之前不会发生任何变化。由于我已经在actionPerformed方法中调用了重绘方法,所以不应该自动发生这种情况吗? 这是我手动调整尺寸后看到的图像: enter image description here

2 个答案:

答案 0 :(得分:4)

没有。您必须强制JPanel重新验证组件:

this.revalidate();

答案 1 :(得分:3)

添加对基本面板的验证调用。

 if(e.getSource()==r2){
        System.out.println("r2 pressed");
        ((GridLayout) this.getLayout()).setRows(4);
        ((GridLayout) this.getLayout()).setRows(4);
        for(int i=0; i<7; i++){
            buttons[i]=new JButton();
            this.add(buttons[i]);
        }

        validate()
        repaint();
    }