GridLayout 仅在 window 手动调整大小时更新
GridLayout only updates when window is manually resized
我有一个使用 3x3 GridLayout 的面板,我想通过单击单选按钮将其更改为 4x4 布局。这是我设置菜单和所需按钮的面板 class。请记住,此代码是纯草稿。我只是想让主要想法发挥作用,即使用单选按钮更新布局。
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();
}
}
}
框架class:
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 网格):
但是当我按下 4x4 按钮时,在我手动调整框架大小之前没有任何变化。因为我已经在 actionPerformed 方法中调用了 repaint 方法,所以这不应该自动发生吗?
这是我手动调整大小后看到的图像:
添加对基本面板的验证调用。
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();
}
没有。您必须强制 JPanel 重新验证组件:
this.revalidate();
我有一个使用 3x3 GridLayout 的面板,我想通过单击单选按钮将其更改为 4x4 布局。这是我设置菜单和所需按钮的面板 class。请记住,此代码是纯草稿。我只是想让主要想法发挥作用,即使用单选按钮更新布局。
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();
}
}
}
框架class:
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 网格):
但是当我按下 4x4 按钮时,在我手动调整框架大小之前没有任何变化。因为我已经在 actionPerformed 方法中调用了 repaint 方法,所以这不应该自动发生吗?
这是我手动调整大小后看到的图像:
添加对基本面板的验证调用。
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();
}
没有。您必须强制 JPanel 重新验证组件:
this.revalidate();