How to set fix size of jlabel?

后端 未结 4 2060
感动是毒
感动是毒 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 05:54

    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.

    0 讨论(0)
  • 2020-12-31 05:55

    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));
    
    0 讨论(0)
  • 2020-12-31 05:59

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

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题