JButton在FlowLayout、BorderLayout等一些布局中使用setSize()设置大小没效果,可以使用setPreferredSize方法进行设置,例如:

public static void main(String[] args) {
  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLayout(new FlowLayout());
  JButton button = new JButton("测试按钮");
  Dimension preferredSize = new Dimension(300,100);//设置尺寸
  button.setPreferredSize(preferredSize );
  frame.add(button);
  frame.setBounds(0,0, 400, 300);
  frame.setVisible(true);
 }

posted on 2012-03-03 00:17  aparche  阅读(24350)  评论(0编辑  收藏  举报