我见过类似的问题,例如this https://stackoverflow.com/questions/1966296/changing-default-jlabel-font,但就我而言,我有 25 个以上的私有 JLabels,我在声明它时为其赋予了值。我在构造函数中使用 GridBagLayout 将这些 JLabels 添加到 JPanel。如果我使用链接中给出的答案的某些内容,它只会更改构造函数中添加的 JLabels。
因此,在我看来,如果我想更改私有 JLabels 的字体大小,我必须单独为它们定义字体大小,或者更改构造函数中的默认值,then添加我的 JLabels。但是,我确信一定有另一种更简单的方法,但我只是没有经验或理解来弄清楚它,或者根据其他类似问题的答案进行适当的调整。
我认为这确实需要我展示我的代码,但如果有帮助,我可以这样做。
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
}
}