How to set fix size of jlabel?

后端 未结 4 2150
感动是毒
感动是毒 2020-12-31 05:45

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 thi

4条回答
  •  执笔经年
    2020-12-31 06:01

    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

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

提交回复
热议问题