2012-05-08 57 views
3

enter image description here大小按鈕在Java

我想控制按鈕的尺寸,我使用方法的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); 
    } 
    } 
    } 

回答

6

LayoutManager覆蓋你多次設置的界限,這是GridLayout的情況。

我建議你通過layout managers tutorial

+0

AWWW擊敗了我! :) +1 – fireshadow52

+0

是 我知道這個解決辦法,但我的老師不希望 我不喜歡形成按鈕,我想 – Seetah

+0

最小化@Seetah告訴在現實生活中,並不是使用'LayoutManagers'你的老師是一個快速發展。這些東西是因爲 – Robin

2

嘗試使用按鈕上的setPreferredSize()方法,雖然佈局管理器對到底是用什麼尺寸的最後一句話。

+0

無法正常工作而發明的:|非常感謝你 – Seetah

1

據我瞭解,GridLayout不允許您調整父組件內部的組件。使用另一佈局並使用

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

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

其中SIZEX是寬度和SIZEY是高度。