简体   繁体   English

如何更改所有私有JLabel的字体大小

[英]How to change font size of all private JLabels

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. 我也有类似的问题,比如这个 ,但对我来说我有25+私人Jlabel之下,我给了,当我宣布它的值。 I use a GridBagLayout in my constructor to add these JLabels to a JPanel. 我在构造函数中使用GridBagLayout将这些JLabel添加到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. 如果我按照链接中给出的答案行使用某些内容,则只会更改构造函数中添加的JLabel。

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. 所以在我看来,如果我想改变我的私有JLabel的字体大小,我将不得不单独定义它们的字体大小,或者更改构造函数中的默认值, 然后添加我的JLabel。 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
 }
 }

You can change the UIManager default for labels. 您可以更改标签的UIManager默认值。 It may be more aesthetic to use deriveFont() on the existing default. 在现有默认值上使用deriveFont()可能更美观。 The change should be made before constructing the GUI using EventQueue.invokeLater() . 应在使用EventQueue.invokeLater()构建GUI 之前进行更改。

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

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. 如果要在GUI创建期间将某些JLabel的大小更改为变量选择大小,则可以使用某种工厂方法。

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. 如果要在程序运行时动态更改它们,则将它们放在List<JLabel>并为GUI类提供一个公共方法,允许在for循环中更改标签字体大小。

Considering that you only have JLable s in your code: 考虑到您的代码中只有JLable

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! 这会将Arial设置为字体,将20设置为所有Elements的大小!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM