【问题标题】:How to make JLabel consume space when not visible?如何让 JLabel 在不可见时占用空间?
【发布时间】:2013-01-04 06:17:03
【问题描述】:

我有一个程序可以创建 2 个面板,然后在其中放置一个标签和两个按钮。标签设置为不可见setVisible(false),然后添加两个按钮并打包框架。当我单击第一个按钮时,显示标签setVisible(true),第二个按钮再次隐藏它,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);
        }
    });
  }

【问题讨论】:

    标签: java swing


    【解决方案1】:
    this.setPreferredSize(new Dimension(400,200));
    this.setMinimumSize(new Dimension(400,200));
    

    所以pack() 不能干涉。

    【讨论】:

    • 感谢您的帮助。我从没想过要设置最小尺寸。
    【解决方案2】:

    使用CardLayout。添加JLabel 并清空JPanel。而不是设置它可见/不可见交换显示JLabelJPanel的卡片。

    【讨论】:

      【解决方案3】:

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

      你需要学习如何使用SwingUtilities.invokeLater():看看example你应该是什么样子

      你需要了解LayoutTutorial

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

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

      【讨论】:

      • 我不能用MainFrame frame = new MainFrame();吗?
      【解决方案4】:

      默认情况下,您使用的框架是BorderLayout。你可以试试:

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

      而不是在中心。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-13
        • 2018-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多