How to change font size of all private JLabels

2019-07-06 02:09发布

问题:

I have seen similar questions such as this, but in my case I have 25+ private JLabels that I give a value for when I declare it. I use a GridBagLayout in my constructor to add these JLabels to a JPanel. If I use something along the lines of the answer given in the link, it will only change JLabels that were added in the constructor.

So it seems to me that if I want to change the font size of my private JLabels, I would have to either define the font size for them individually, or change the default in my constructor and then add my JLabels. However, I am sure that there must be another, simpler way, but I just don't have the experience or understanding to figure it out, or to make proper adjustments from answers to other similar questions.

I don' think that this really requires me to show my code, but if it helps I can do so.

public class LaborRecordPanel implements
Printable, ActionListener {

private Color shade = new Color(201,201,201);

private ImageIcon logoIcon = new ImageIcon("Images/fici_logo1.jpg");
private JLabel logoLabel = new JLabel(logoIcon);

private JLabel authorizedBy = new JLabel("AUTHORIZED BY:");
private JLabel toCertify = new JLabel("THIS IS TO CERTIFY THAT THE ABOVE LABOR HAS BEEN PERFORMED.");

private JLabel laborRecordNO = new JLabel("NO.");
private JLabel nameOfJob = new JLabel("NAME OF JOB:");
private JLabel customerPO = new JLabel("CUSTOMER PO #:");
private JLabel contractNO = new JLabel("CONTRACT NO.");
private JLabel weekEnding = new JLabel("WEEK ENDING");
private JComboBox laborRateDescription = new JComboBox();
//...

JPanel rp = new JPanel();
JScrollPane scrollPane = new JScrollPane(rp);
LaborRecordPanel(){
    //Font oldLabelFont = UIManager.getFont("Label.font");
    //UIManager.put("Label.font",oldLabelFont.deriveFont(Font.PLAIN));
    //UIManager.put("Label.font", UIManager.getFont("Label.font").deriveFont(40));
    //SwingUtilities.updateComponentTreeUI(rp);
    rp.setPreferredSize(new Dimension(1295,1830 ));
    scrollPane.getVerticalScrollBar().setUnitIncrement(16); //increase the scroll speed

    GridBagLayout gridbag = new GridBagLayout();
    rp.setBackground(Color.WHITE);
    rp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    GridBagConstraints gbc = new GridBagConstraints();
    rp.setLayout(gridbag);
    //gbc.insets = new Insets(5, 5, 5, 5);

    //row 0////////////////////////////////////////////////////////////
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridx = 10;
    gbc.gridy = 0;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    laborRecordNO.setHorizontalAlignment(JLabel.CENTER);
    laborRecordNO.setFont(new Font("Dialog", Font.PLAIN, 18));
    gridbag.setConstraints(laborRecordNO, gbc);
    rp.add(laborRecordNO, gbc);

    //row 1////////////////////////////////////////////////////////////
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 13;
    gridbag.setConstraints(logoLabel, gbc);
    rp.add(logoLabel, gbc);

    //row 2////////////////////////////////////////////////////////////
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gridbag.setConstraints(nameOfJob, gbc);
    rp.add(nameOfJob, gbc);

    gbc.gridx = 6;
    gbc.gridy = 2;
    gbc.gridheight = 1;
    gbc.gridwidth = 3;
    gridbag.setConstraints(contractNO, gbc);
    rp.add(contractNO, gbc);
 //... more setting constraints and adding
 }
 }

回答1:

You can change the UIManager default for labels. It may be more aesthetic to use deriveFont() on the existing default. The change should be made before constructing the GUI using EventQueue.invokeLater().

Font f = (Font) UIManager.get("Label.font");
UIManager.put("Label.font", f.deriveFont(36f));
EventQueue.invokeLater(new Runnable() {…}


回答2:

If you want to change the size of some of your JLabels to a variable select size during GUI creation, then you could use a factory method of some sort.

private JLabel createLabel(String text, float points) {
  JLabel label = new JLabel(text);
  label.setFont(label.getFont().deriveFont(points));
  return label;
}

If you want to change them on the fly as the program is running, then put them in a List<JLabel> and give the GUI class a public method that allows the labels font sizes to be changed in a for loop.



回答3:

Considering that you only have JLables in your code:

This will do the trick!

setUIFont(new FontUIResource(new Font("Arial", 0, 20)));

This will set Arial as a font, 20 as a size for all the Elements!