Reputation: 1
Everytime I change the font type of the JLabels, JTextAreas, JRadioButtons, etc. in my program it changes the size of the panels that all of these things are in and I have to pack() the frame to make them all fit again which changes the size of the main frame which I obviously do not want. Is there a way to change the font type without changing the size of anything else?
Upvotes: 0
Views: 99
Reputation: 36743
Not easily, the default size of a JLabel, JTextArea etc. is delegated to the "look and feel", this determines the size using the font metrics of the font, the size and the text.
Thefore bigger font, bigger buttons.
I'd advise you to just accept this, and use a layout manager that deals with the nicely...
i.e. don't assume precise layout, instead use something grid-based with enough free-space so that font size can vary.
If you're still desperate to do this you could override the preferred size of the controls with the following, although this pre-supposes you're using a layout manager that respects these hints.
public static void main(String [] args) {
JFrame frame = new JFrame();
JTextField small = new JTextField("small");
JTextField big = new JTextField("big");
big.setFont(big.getFont().deriveFont(Font.BOLD, 40));
big.setPreferredSize(new Dimension(123, 20));
big.setMaximumSize(new Dimension(123, 20));
frame.setLayout(new FlowLayout());
frame.add(small);
frame.add(big);
frame.setVisible(true);
}
Upvotes: 1