如何更改所有私有JLabel的字体大小 [英] How to change font size of all private JLabels

查看:145
本文介绍了如何更改所有私有JLabel的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经见过类似的问题,比如这个,但是在我的例子中,我有25+私人JLabels,当我宣布它时,我给了它的价值。我在我的构造函数中使用GridBagLayout将这些JLabel添加到JPanel。如果我沿着链接给出的答案使用某些东西,它只会改变构造函数中添加的JLabel。所以在我看来,如果我想改变我的私人JLabels的字体大小,我将不得不单独定义他们的字体大小,或者更改默认在我的构造函数和然后添加我的JLabels。但是,我相信肯定还有另外一个更简单的方法,但我没有经验或理解去搞清楚,或者从其他类似问题的答案中作出适当的调整。

我不认为这真的需要我显示我的代码,但是如果有帮助的话,我可以这样做。

  public class LaborRecordPanel实现
可打印,ActionListener {

私有颜色shade = new Color(201,201,201);

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(这是为了证明上面的劳动已经被执行了。

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 labourRateDescription = new JComboBox();
// ...

JPanel rp = new JPanel();
JScrollPane scrollPane = new JScrollPane(rp);
LaborRecordPanel(){
//字体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); //增加滚动速度

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.setHorizo​​ntalAlignment(JLabel.CENTER);
laborRecordNO.setFont(new Font(Dialog,Font.PLAIN,18));
gridbag.setConstraints(laborRecordNO,gbc);
rp.add(laborRecordNO,gbc);

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

//第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);
// ...更多设置约束和添加
}
}


解决方案

您可以更改标签的 UIManager 默认值。在现有的默认情况下使用 deriveFont()可能更美观。使用 EventQueue.invokeLater()

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


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
 }
 }

解决方案

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() {…}

这篇关于如何更改所有私有JLabel的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆