java button size

java button size

To change the size of a button in Java, you can use the 'setPreferredSize' or 'setSize' methods of the 'JButton' class, depending on the layout you are using. The 'JButton' class is a Java graphical user interface (GUI) class that represents a button on the screen.

Here is an example of how to change the size of a button using the 'setPreferredSize' method:

refer ‮ot‬:lautturi.com
import java.awt.Dimension;
import javax.swing.JButton;

public class Main {
  public static void main(String[] args) {
    // Creates a button
    JButton botao = new JButton("Clique aqui");

    // Sets the preferred button size
    botao.setPreferredSize(new Dimension(200, 50));
  }
}

This code creates a button 200 pixels wide and 50 pixels high. Note that the actual size of the button can be affected by the layout of the container that contains it and the other components of the container.

If you want to set the exact button size, regardless of layout, you can use the 'setSize' method instead of 'setPreferredSize'. Here's an example of how to do this:

import java.awt.Dimension;
import javax.swing.JButton;

public class Main {
  public static void main(String[] args) {
    // Creates a button
    JButton botao = new JButton("Clique aqui");

    // Sets the exact size of the button
    botao.setSize(new Dimension(200, 50));
  }
}

Note that in this example, we are using the 'JButton' class of the 'javax.swing' library, which is a graphical user interface class.

Created Time:2017-11-03 00:14:46  Author:lautturi