buttonname.setBounds(xlocation,ylocation,width,height);不适合我吗?

时间:2013-12-06 10:55:16

标签: java eclipse jframe jbutton setbounds

 package com.tests;

 import javax.swing.ImageIcon;
 import javax.swing.JButton;
  import javax.swing.JFrame;

 public class LevelUp {

public static void main(String[] args)  {
    // TODO Auto-generated method stub
     final int width = 1000;
     final int height = width / 16*9;

 //buttons      
 JButton button = new JButton("Yus");

 //location
 button.setBounds(0, 0, 50, 50);

   JFrame frame = new JFrame("Level");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setResizable(false);
   frame.setVisible(true);
   frame.setIconImage(new ImageIcon("Biblioteker\\Billeder\\giggle.jpeg").getImage());
   frame.setSize(width, height);
   frame.setLocationRelativeTo(null);

   //add to jframe
   frame.add(button);

} }

为什么不这样做?另外我是java和eclipse的新手一般,任何帮助将不胜感激。提前谢谢!

1 个答案:

答案 0 :(得分:0)

setBounds在null布局中工作。添加此行..

frame.setLayout(null);