Pergunta

I'm fairly new to Java. I set the size for JButton and JTextField, but it looks like it didn't take my variable. Would someone tell me how can I solve this problem: whatever I change the variable, it looks the same. Thanks in advance.

This is my code:

private static int categoryButtonWidth= 140;
private static int categoryTextFieldWidth=260;
private static int categoryHight=40;

private void setCategoryPanel(){
 //set the color label category       

 JPanel panelCategory=new JPanel();      
  panelCategory.setLayout(new FlowLayout(FlowLayout.LEADING));       


 JButton btnCategory1=new JButton("    ");    
 btnCategory_1.setSize(new Dimension ( categoryButtonWidth, categoryHight));
 btnCategory_1.setBackground(Color.red);
 btnCategory_1.addActionListener(this);   
 panelCategory.add(btnCategory_1);

 JTextField txtCategory1 = new JTextField(20);
 txtCategory_1.setSize(new Dimension (categoryTextFieldWidth, categoryHight));   
 panelCategory.add(txtCategory_1);


 JButton btnCategory_2=new JButton("    ");
 btnCategory_2.setSize(new Dimension ( categoryButtonWidth, categoryHight));      
 btnCategory_2.setBackground(Color.YELLOW);   
 btnCategory_2.addActionListener(this);
 panelCategory.add(btnCategory_2);

 JTextField txtCategory2 = new JTextField(20);
 txtCategory2.setSize(new Dimension (categoryTextFieldWidth, categoryHight));   
 panelCategory.add(txtCategory2);    
 this.add(panelCategory,  BorderLayout.NORTH);
 }
Foi útil?

Solução

A a rule of thumb, Java layout managers will ignore the size property of any component. Instead, layout managers will size a component anywhere between their minimum and maximum sizes, regardless of what is set for size based on what is "best" for the current layout. If you really need to force a size, use both .setMaximumSize() and .setMinimumSize() on a component to force it to a specific size.

private void setCategoryPanel(){
    //set the color label category       

    JPanel panelCategory=new JPanel();      
     panelCategory.setLayout(new FlowLayout(FlowLayout.LEADING));       


    JButton btnCategory1=new JButton("    ");
    Dimension btnCategory_1_Dimension = new Dimension(categoryButtonWidth, categoryHight);
    btnCategory_1.setSize(btnCategory_1_Dimension);
    btnCategory_1.setMaximumSize(btnCategory_1_Dimension);
    btnCategory_1.setMinimumSize(btnCategory_1_Dimension);
    btnCategory_1.setBackground(Color.red);
    btnCategory_1.addActionListener(this);   
    panelCategory.add(btnCategory_1);

    JTextField txtCategory1 = new JTextField(20);
    Dimension txtCategory_1_Dimension = new Dimension (categoryTextFieldWidth, categoryHight);
    txtCategory_1.setSize(txtCategory_1_Dimension);  
    txtCategory_1.setMinimumSize(txtCategory_1_Dimension);
    txtCategory_1.setMaximumSize(txtCategory_1_Dimension);
    panelCategory.add(txtCategory_1);


    JButton btnCategory_2=new JButton("    ");
    Dimension btnCategory_2_Dimension = new Dimension(categoryButtonWidth, categoryHight);
    btnCategory_2.setSize(btnCategory_2_Dimension);
    btnCategory_2.setMaximumSize(btnCategory_2_Dimension);
    btnCategory_2.setMinimumSize(btnCategory_2_Dimension);
    btnCategory_2.setSize(new Dimension ( categoryButtonWidth, categoryHight));      
    btnCategory_2.setBackground(Color.YELLOW);   
    btnCategory_2.addActionListener(this);
    panelCategory.add(btnCategory_2);

    JTextField txtCategory2 = new JTextField(20);
    Dimension txtCategory_2_Dimension = new Dimension (categoryTextFieldWidth, categoryHight);
    txtCategory_2.setSize(txtCategory_1_Dimension);  
    txtCategory_2.setMinimumSize(txtCategory_1_Dimension);
    txtCategory_2.setMaximumSize(txtCategory_1_Dimension);   
    panelCategory.add(txtCategory2);    
    this.add(panelCategory,  BorderLayout.NORTH);
}

But this is generally frowned upon, since the layout manager should know best. Usually strict sizing inside a layout manager means poor design and using the layout managers incorrectly. The Java Tutorial gives some excellent advice on layout managers. It can be frustrating to get a swing layout manager to give you the results you want, but you will usually be thankful in the end that you have a program that properly resizes with the wishes of the user without pesky pixel requirements on sizes, especially when you consider that your program could render wildly differently on a different system and all of your hard work on pixel adjustments goes down the drain in an instant.

Outras dicas

There are 2 ways to control the screen elements location and size:

  1. directly using setSize() and setLocation()
  2. using layout manager.

Once layout manager is defined (as in your case) all your attempts to set size will be overridden by layout manager that is the boss. You can set layout manager to null and try to manipulate sizes yourself, but I do not recommend you to do this. You should better learn how to leave with layout manager and make it to do the job as you want.

For making a text field bigger, give it lots of columns and a big font. For making a button bigger, use a large font or a large icon.

For either of those, we can add more space around the text or icon by calling setMargin(Insets).


See also Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)

General tip

Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement or sizing of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top