请问我重置之后怎么没有设置Layout成功import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.*;public class AutoFlash extends JFrame{ JMenuBar jmb = new JMenuBar();
JMenu jm = new JMenu("Menu");
JMenuItem jmi = new JMenuItem("jmi");
AutoFlash(){ this.setSize(400, 300);
this.setJMenuBar(jmb);
jmb.add(jm);
jm.add(jmi);
this.add(new JLabel("JLabel",JLabel.CENTER));
JButton jb = new JButton("button");
this.add(jb);
jb.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
createNew();
}
});
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
private void createNew() {
this.getContentPane().removeAll();
this.repaint();
this.setLayout(new GridLayout(3,2));
this.add(new JButton("1"));
this.add(new JButton("2"));
this.add(new JButton("3"));
this.add(new JButton("4"));
this.add(new JButton("5"));
this.add(new JButton("6"));
}
public static void main(String[] args) {
(new AutoFlash()).setVisible(true);
}} pack()方法会改变框架的形状
还有没有什么方法能够实现setlayout的 并不改变框架的形状的 谢谢!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.*;public class AutoFlash extends JFrame{ JMenuBar jmb = new JMenuBar();
JMenu jm = new JMenu("Menu");
JMenuItem jmi = new JMenuItem("jmi");
AutoFlash(){ this.setSize(400, 300);
this.setJMenuBar(jmb);
jmb.add(jm);
jm.add(jmi);
this.add(new JLabel("JLabel",JLabel.CENTER));
JButton jb = new JButton("button");
this.add(jb);
jb.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
createNew();
}
});
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
private void createNew() {
this.getContentPane().removeAll();
this.repaint();
this.setLayout(new GridLayout(3,2));
this.add(new JButton("1"));
this.add(new JButton("2"));
this.add(new JButton("3"));
this.add(new JButton("4"));
this.add(new JButton("5"));
this.add(new JButton("6"));
}
public static void main(String[] args) {
(new AutoFlash()).setVisible(true);
}} pack()方法会改变框架的形状
还有没有什么方法能够实现setlayout的 并不改变框架的形状的 谢谢!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货