繁体   English   中英

如何使JLabel在不可见时消耗空间?

[英]How to make JLabel consume space when not visible?

我有一个程序,它创建2个面板,然后放置一个标签和两个按钮。 标签设置为不可见的setVisible(false) ,然后添加两个按钮并打包框架。 当我单击第一个按钮时,会显示标签, setVisible(true) ,而seccond再次隐藏它, setVisible(false) 当我点击每个按钮时,它们移动以填充标签隐藏的空间,然后再次移动以避开标签显示的方式。 我想阻止这种情况发生,即使标签被隐藏,按钮也会保持在同一个位置。

这是代码:

public class MainFrame extends JFrame{
  public JLabel statusLabel;
  public JButton show;
  public JButton hide;  

  public MainFrame(){
    super("MagicLabel");

    JPanel topPanel = new JPanel();  //Create Top Panel
    statusLabel = new JLabel("");    //Init label
    statusLabel.setVisible(false);   //Hide label at startup
    topPanel.setSize(400, 150);      //Set the size of the panel, Doesn't work
    topPanel.add(statusLabel);       //Add label to panel

    JPanel middlePanel = new JPanel(); //Create Middle Panel
    show= new JButton("Show");       //Create show button
    hide= new JButton("Hide");       //Create hide button
    middlePanel.setSize(400, 50);    //Set the size of the panel, Doesn't work
    middlePanel.add(show);           //Add show button
    middlePanel.add(hide);           //Add hide button

    this.add(topPanel, "North");     //Add Top Panel to North
    this.add(middlePanel, "Center"); //Add Middle Panel to Center
    addActionListeners();            //void:adds action listeners to buttons
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBounds(100, 100, 512, 400);
    this.setPreferredSize(new Dimension(400,200)); //Set size of frame, Does work
    this.pack();
    this.setVisible(true);
  }

  public void animateInstall(boolean var0){  //Void to show and hide label from action listeners
    statusLabel.setVisible(var0);
    sendWorkingMessage("Boo!");
  }
  public void sendWorkingMessage(String message){ //Void to set text of label
    this.statusLabel.setForeground(new Color(225, 225, 0));
    this.statusLabel.setText(message);
  }

  void addActionListeners(){
    show.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            animateInstall(true);
        }
    });
    hide.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            animateInstall(false);
        }
    });
  }
this.setPreferredSize(new Dimension(400,200));
this.setMinimumSize(new Dimension(400,200));

所以pack()不能干涉。

使用CardLayout 添加JLabel并清空JPanel 在必要时,不要将显示JLabelJPanel的卡交换为可见/不可见。

不建议扩展JFrame,更好地扩展JPanel将所有组件放入其中,然后将其添加到JFrame中

您需要学习如何使用SwingUtilities.invokeLater() :请参阅示例您应该如何使用

您需要了解Layout教程

代码中非常愚蠢和简单的方法是:

 this.statusLabel.setForeground(bgColor); //background color
 this.statusLabel.setText("           "); //some number of characters

默认情况下,您正在使用BorderLayout 您可以尝试使用:

this.add(topPanel, BorderLayout.NORTH);     //Add Top Panel to North
this.add(middlePanel, BorderLayout.SOUTH); //Add Middle Panel to South

而不是在中心。

或者您可以为这两个面板创建一个中间容器面板,或者考虑其他布局管理器,如BoxLayout

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM