Java 如何设置 jlabel 的固定大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22920046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:01:00  来源:igfitidea点击:

How to set fix size of jlabel?

javaswingjlabelnull-layout-manager

提问by user3506824

I am trying to make a java desktop application where I am using multiple JLabelI want to set fix height and width of JLabel. How can I achieve this?

我正在尝试制作一个 java 桌面应用程序,我在其中使用多个JLabel我想设置JLabel. 我怎样才能做到这一点?

Here is my code

这是我的代码

public class Second extends JFrame
{
    JPanel panel1,panel2;
    JLabel label=new JLabel();
    ArrayList<JLabel> lbllist = new ArrayList<>();

    public Second()  
    {
        super("Simple Timer");
        {
            getContentPane().setBackground(new java.awt.Color(255,255,255));
        } 
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        Container c = getContentPane();
        c.setLayout( new FlowLayout());
        setUndecorated(true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(0,0,screenSize.width, screenSize.height);

        panel2=new JPanel();
        panel2.setBounds(360, 180, 360, 1020);
        panel2.setBackground(new java.awt.Color(255,153,51));
        c.add(panel2);
        JPanel panel3 = createPanel();
        panel1.setLayout(new GridBagLayout());
        panel1.add(panel3);

        JPanel panel4 = createPanel(); 
        panel2.setLayout(new GridBagLayout());
    //  jPanel2.setLayout(null);
        panel2.add(panel4);

        setLayout(null);
   }

   private JPanel createPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 1, 10, 5));
        EmptyBorder panelBorder = new EmptyBorder(10, 5, 10, 10);
        panel.setBorder(panelBorder);
        panel.setBackground(new java.awt.Color(255, 153, 51));
        panel.setOpaque(true);
        EmptyBorder border1 = new EmptyBorder(5, 20, 15, 18);

        Border border = BorderFactory.createLineBorder(Color.BLUE, 2);
        for (int i = 0; i <11; i++) {
            label = new JLabel("<html>Case &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CaseNum<br><font color=yellow>Party1<br>Party2</font></html>");
            label.setFont(new java.awt.Font("Times New Roman", 1, 18));
            label.setBorder(border);
            label.setSize(300, 100);
            label.setBorder(border1);
            Dimension d = label.getPreferredSize();
            label.setBackground(Color.GRAY);
            label.setForeground(new java.awt.Color(255, 255,255 ));
            label.setOpaque(true);
            lbllist.add(label);
            panel.add(label);
        }
        return panel;
    }

When I reduce text size or text from JLabelthen size of JLabelreduced but I want that when i reduced or remove any text from JLabelbut size of Jlabel should not reduced it should fix

当我减少文本大小或文本JLabel大小时,JLabel我希望当我减少或删除任何文本时,JLabel但 Jlabel 的大小不应该减少它应该修复

How can I get this?

我怎样才能得到这个?

Thanks in advance

提前致谢

采纳答案by Salah

You can set a fixed the size by setting the minimum, preferred and maximum size:

您可以通过设置最小、首选和最大大小来设置固定大小:

setMinimumSize(width, height);
setPreferredSize(width, height);
setMaximumSize(width, height);

As the Linkfrom MadProgrammer, you need to override these methods, not using them from outside, based on the reasons mentioned in this link.

作为来自MadProgrammer链接,基于此链接中提到的原因,您需要覆盖这些方法,而不是从外部使用它们。

回答by PotatoPhil

Use JLabel.setPreferredSize(width, height);The jpanel does not have a layout, try Jpanel pane =new JPanel(new FlowLayout());

使用JLabel.setPreferredSize(width, height);jpanel 没有布局,试试 Jpanel pane =new JPanel(new FlowLayout());

回答by Avanish Kumar

if this method not work

如果这个方法不起作用

setMinimumSize(width, height);
setPreferredSize(width, height);
setMaximumSize(width, height);

then use it

然后使用它

setMinimumSize(new Dimension(width, height));
setPreferredSize(new Dimension(width, height));
setMaximumSize(new Dimension(width, height));

回答by Wako

An easy work around (without overriding existing classes) is to;

一个简单的解决方法(不覆盖现有类)是;

  • Create a JPanel specifically for the component

  • Set the size for the JPanel

  • Place the component within the JPanel

  • Place the JPanel where you would have originally placed the component

  • 专门为组件创建一个JPanel

  • 设置 JPanel 的大小

  • 将组件放置在 JPanel 中

  • 将 JPanel 放置在您最初放置组件的位置

e.g.

例如

JPanel mainPanel = new JPanel(); //Assume this is the panel you are placing things into.

JLabel nameLabel = new JLabel("Name:");
JPanel nameLabelPanel = new JPanel();
nameLabelPanel.setPreferredSize(new Dimension(100, 25));
JPanel.add(nameLabel);
mainPanel.add(nameLabelPanel);