开发者

Aligning JButtons

I'm not sure why I can't adjust the location of my buttons in the code below. I've got a couple of lines that set the boundries, but I must be missing something.

public DayGUI()
      {  
         mainFrame = new JFrame("W开发者_JAVA技巧hat Kind Of Day Are You Having?");  
         btnGood = new JButton("Good");  
         btnBad = new JButton("Bad");

         btnGood.setBounds(10, 10, 80, 20);
         btnBad.setBounds(10, 30, 80, 20);

       Container c = mainFrame.getContentPane();  
       c.setLayout(new FlowLayout());  
       c.add(btnGood);  
       c.add(btnBad);  


You can align your buttons using a GridLayout, a GridBagLayout, a SpringLayout, a GroupLayout, and others.

This is a tutorial on GridBagLayout. Here is one on GridLayout. Here is one on GroupLayout.

This is the overview on layout managers. Layout managers allow you to define components in a container in relationship to the container and each other. They each have their own strengths and weaknesses.

For your needs I would consider GridBagLayout.


You should use a layout manager: http://download.oracle.com/javase/tutorial/uiswing/layout/using.html


You used FlowLayout as layout manager. This layout manager don't resize the components to fit all the same size. Instead of setBounds you should use setPreferredSize.


FlowLayout assumes the "prefered size" for the component when laying them out. Set it properly and it should just work.

the setBound call will be ignored as soon as the layout manager does his work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜