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)
Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

Why does this JButton take up the entire screen?
Started by Nickburris, Feb 03 2011 06:03 PM
jbutton setpreferredsize
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(); } }
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?
Also tagged with one or more of these keywords: jbutton, setpreferredsize
Language Forums →
Java →
Java JButton issueStarted by Jib7312, 02 Oct 2013 ![]() |
|
![]() |
||
Language Forums →
Java →
JButton event handlingStarted by Animus, 21 Jan 2013 ![]() |
|
![]() |
||
Tutorial Forums →
Java Tutorials →
JButton apocalypse: Death of the JButton - next gen guiStarted by UniBrain, 08 Jan 2013 ![]() |
|
![]() |
||
Language Forums →
Java →
Grid With MysqlStarted by Apprentice123, 30 Jul 2012 ![]() |
|
![]() |
||
Language Forums →
Java →
Really Easy Paintcomponent QuestionStarted by Cruel Hand, 07 Jul 2012 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download