Daanish Daanish - 3 years ago 169
Java Question

JButton.setBounds(x,y,w,h) doesn't seem to work

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SimpleExample extends JFrame {

public SimpleExample() {

setTitle("Simple example");
setSize(500, 500);
JButton jb = new JButton("TEST");
jb.setBounds(5, 5, 1, 1); ---> This line


public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
SimpleExample ex = new SimpleExample();

Just creating a simple button of a preferred size. The
method doesn't seem to work. Where am I going wrong?

Answer Source

Your frame is under the control of a layout manager. It is making the decisions on how to layout your components, and is overriding the values you have specified using setBounds.

Modern GUI's need to run (even on the same OS) in a variety of different graphical environments, including different DPI; screen sizes; and font settings; for example.

The layout manager makes it possible for you to worry (less) about these issues and it is highly recommended that you make use of them

Take a look at

For more details

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download