Seetah
Seetah

Reputation: 61

size of button in java

enter image description here

I want to control size of button , I use Method setBounds But there is no change and this my code

       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 

this main cod

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 to add components

   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);
   }
   }
   }

Upvotes: 3

Views: 196

Answers (3)

Basilio German
Basilio German

Reputation: 1819

to my understandings, GridLayout does not let you resize the components inside the parent component. Use another layout and use

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

or

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

where sizeX is width and sizeY is height.

Upvotes: 1

Óscar López
Óscar López

Reputation: 236140

Try using the setPreferredSize() method on the button, although the layout manager has the final word on what size is used in the end.

Upvotes: 2

MByD
MByD

Reputation: 137442

The LayoutManager overrides the bounds you set many times, and this is the case with GridLayout.

I suggest you go through the layout managers tutorial.

Upvotes: 6

Related Questions