如何设置jlabel的修复大小

如何设置jlabel的修复大小

本文介绍了如何设置jlabel的修复大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个java桌面应用程序,我使用多个 JLabel 我想设置修复高度和宽度 JLabel 。我怎样才能做到这一点?

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

这是我的代码

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

当我从 JLabel 然后 JLabel 的大小减少但我想减少或删除 JLabel 中的任何文本但是Jlabel的大小不应该减少它应该修复

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

我怎么能得到这个?

提前致谢

推荐答案

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

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 Link from MadProgrammer, you need to override these methods, not using them from outside, based on the reasons mentioned in this link.

这篇关于如何设置jlabel的修复大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!