如何设置jlabel的修复大小?

时间:2014-04-07 18:16:20

标签: java swing jlabel null-layout-manager

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

这是我的代码

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的大小不应减少它应该修复

我怎么能得到这个?

提前致谢

4 个答案:

答案 0 :(得分:13)

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

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

作为 MadProgrammer 中的Link,您需要根据此link中提到的原因覆盖这些方法,而不是从外部使用它们。

答案 1 :(得分:4)

如果此方法不起作用

URL url = new URL("https://www.google.com/");
    URLConnection con = url.openConnection();
    InputStream is = con.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line="";
    while (br.readLine() != null) {
        line = line + br.readLine();
    }
    //System.out.println(line);

Document doc = Jsoup.parse(line.replace("<!--","").replace("-->",""));

然后使用它

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

答案 2 :(得分:2)

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

  • 专门为组件创建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);

答案 3 :(得分:0)

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