2

The button takes up the whole screen, and I try using setSize() but that doesn't appear to be doing anything. Here's my code so far:

JButton start = new JButton("PLAY");
start.setSize(new Dimension(100, 100));
myFrame.add(start);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
lehermj
  • 916
  • 4
  • 9
  • 20

2 Answers2

2

By default, JFrame has BorderLayout with CENTER alignment. Thats why single component will takes full screen. So add a suitable Layout Manager to JFrame.

For details, go through, How to Use Various Layout Managers.

Masudul
  • 21,823
  • 5
  • 43
  • 58
  • thank you! I'll accept your answer whenever it will alow me to. (11-ish minutes). – lehermj May 24 '14 at 16:12
  • 1
    +1, for suggesting to use layout managers. -1 for suggestion a null layout!!! There is absolutely no reason to use a null layout in this case and the OP will not learn how to use Swing the way it was designed to be used. – camickr May 24 '14 at 16:23
  • @camickr, I used null layout as an example to view the Dimension size, not suggest to used it. – Masudul May 24 '14 at 16:25
1

you can try using GridBagLayout to set it's size within a Container.

img import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel;

public class test extends JPanel{
    private static final long serialVersionUID = 1L;
    JButton b1, b2, b3, b4, b5;
    GridBagConstraints g = new GridBagConstraints();
    public test() {
        setLayout(new GridBagLayout());
        g.insets = new Insets(1, 1, 1, 1);
        b1 = new JButton("Button 1");
        g.gridx = 0;
        g.gridy = 6;
        g.gridwidth = 2;
        g.gridheight = 1;
        g.fill = GridBagConstraints.HORIZONTAL;
        g.fill = GridBagConstraints.VERTICAL;
        add(b1, g);
        b2 = new JButton("Button 2");
        g.gridx = 0;
        g.gridy = 0;
        g.gridwidth = 3;
        g.gridheight = 1;
        g.fill = GridBagConstraints.HORIZONTAL;
        g.fill = GridBagConstraints.VERTICAL;
        add(b2, g);
        b3 = new JButton("Button 3");
        g.gridx = 2;
        g.gridy = 2;
        g.gridwidth = 1;
        g.gridheight = 1;
        g.fill = GridBagConstraints.HORIZONTAL;
        g.fill = GridBagConstraints.VERTICAL;
        add(b3, g);
        b4 = new JButton("Button 4");
        g.gridx = 6;
        g.gridy = 0;
        g.gridheight = 3;
        g.gridwidth = 1;
        g.fill = GridBagConstraints.HORIZONTAL;
        g.fill = GridBagConstraints.VERTICAL;
        add(b4, g);
        b5 = new JButton("Button 5");
        g.gridx = 1;
        g.gridy = 3;
        g.gridheight = 1;
        g.gridwidth = 2;
        g.fill = GridBagConstraints.HORIZONTAL;
        g.fill = GridBagConstraints.VERTICAL;
        add(b5, g);
    }
    public static void main(String[] args) {
    test t = new test();
    JFrame frame = new JFrame("test");
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.add(t);
    }
}
Bryce Hahn
  • 1,585
  • 4
  • 16
  • 26