This is my first time here, I'm not yet familiar with the site but I heard it was great. I'm currently trying to learn Java. I'm having problems with layouts. The one I'm specifically looking at right now is the FlowLayout. Would anyone be able to tell me how I can still change the size of a JButton for example when a FlowLayout it being used? Thanks :)
Asked
Active
Viewed 313 times
1
-
1Welcome to SO, post some code you will get plenty of help – Frank Jan 28 '13 at 21:33
-
You need a reference to the JButton. What have you tried? – Ted Hopp Jan 28 '13 at 21:33
-
It would help if you posted a brief code sample. – Aaron Kurtzhals Jan 28 '13 at 21:33
1 Answers
3
As FlowLayout
uses preferred sizes, you can override getPreferredSize
in that component:
JButton myButton = new JButton(" Click Me !") {
@Override
public Dimension getPreferredSize() {
return new Dimension(60, 25);
};
};
In case you're wondering why I didn't call setPreferredSize
, check out
Should I avoid the use of set[Preferred|Maximum|Minimum]Size methods in Java Swing?
-
Why extend it like that when you could just call myButton.setPreferedSize(new Dimension(60,25)) it seems unnecessary. – Adude11 Jan 28 '13 at 22:04