java中按钮的大小

时间:2012-05-08 16:45:55

标签: java swing jbutton layout-manager

enter image description here

我想控制按钮的大小,我使用方法setBounds但是没有变化 这是我的代码

       public class levels extends JFrame implements ActionListener{

 //Variables 
private static JLabel chooseLevel;
private static JButton easyPuzzle;
private static JButton mediumPuzzle;
private static JButton hardPuzzle;
 // End Of Variables 

这个主要的鳕鱼

public static void main(String[]args){

levels level = new levels();
level.setBounds(400, 190, 450, 450);
level.setVisible(true); // frame is visible
level.setResizable(false); // not Resizable frame 
level.setTitle("Puzzle Number : New Game");  // Title Of the frame

添加组件的容器

   Container cN = level.getContentPane(); //  Container to add components for farme 1
   GridLayout gN = new GridLayout(0,1,10,20); //object from GridLayout
   cN.setLayout(gN);

   levels.chooseLevel = new JLabel("              Choose a level :");
   levels.easyPuzzle = new JButton("Easy puzzle from ( 1 - to -15)");
   levels.mediumPuzzle = new JButton("Medium Puzzle from (1- to- 29)");
   levels.hardPuzzle = new JButton("Hard Puzzle from (1- to- 59)");
  
    //add components for frame
  
     cN.add(chooseLevel);
     cN.add(easyPuzzle);
     cN.add(mediumPuzzle);
     cN.add(hardPuzzle);
   }
   }
   }

3 个答案:

答案 0 :(得分:6)

LayoutManager会覆盖您多次设置的边界,GridLayout就是这种情况。

我建议您浏览layout managers tutorial

答案 1 :(得分:2)

尝试在按钮上使用setPreferredSize()方法,尽管布局管理器最后确定了最终使用的大小。

答案 2 :(得分:1)

根据我的理解,GridLayout不允许您调整父组件内的组件的大小。使用其他布局并使用

button.setPreferredSize(new Dimension(sizeX,sizeY));

button.setSize(new Dimension(sizeX,sizeY));

其中sizeX是宽度,sizeY是高度。