Jump to content

Why does this JButton take up the entire screen?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Nickburris

Nickburris

    Newbie

  • Members
  • Pip
  • 1 posts
When I run this code, the JButton b1 appears on the screen but the button is taking up the entire screen, instead of the set size 50x50..

Icon sb = new ImageIcon(getClass().getResource("Start.png"));

	b1 = new JButton("", sb);

	b1.setPreferredSize(new Dimension(50, 50));

	add(b1);
("private JButton b1;" is written at the top of my code)

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Did you set a layout in the parent container? There isn't much code, this is what I wrote as a test and the JButton goes down to it's preferred size as expected:

import javax.swing.*;

import java.awt.*;


public class JButtonTest

{

    public JButtonTest()

    {

        JFrame frame = new JFrame();

        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS));

        Icon sb = new ImageIcon(getClass().getResource("Start.png"));

        JButton b1 = new JButton("", sb);

        b1.setPreferredSize(new Dimension(50, 50));

        frame.add(b1);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }


    public static void main(String[] args)

    {

        new JButtonTest();

    }

}

Wow I changed my sig!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users