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)
1 reply to this topic
#1
Posted 03 February 2011 - 06:03 PM
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..
|
|
|
#2
Posted 04 February 2011 - 01:30 AM
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


Sign In
Create Account

Back to top









