setLayout 的问题

不才 2010-04-03 02:42:16
请问我重置之后怎么没有设置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的 并不改变框架的形状的 谢谢!
...全文
96 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cantalou 2010-04-05
  • 打赏
  • 举报
回复
ContentPane 刷新一下 doLayout或是validate
kai27ks 2010-04-04
  • 打赏
  • 举报
回复
问题出在这里 this.addActionListener(new new ActionListener()) 错了 注册监听应该是类本身 ,改成this.addActionListener(this)
AutoFlash应该implements ActionListener
麦穗 2010-04-04
  • 打赏
  • 举报
回复
看了半天,没有明白LZ的意思.

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧