2013-01-04 38 views
3

我有一個程序創建2個面板,然後在其中放置一個標籤和兩個按鈕。該標籤設置爲不可見setVisible(false),然後添加兩個按鈕並打包該框架。當我點擊第一個按鈕時,標籤顯示爲setVisible(true),第二個標籤再次隱藏,setVisible(false)。當我點擊每個按鈕時,它們會移動以填充標籤隱藏處的空間,並再次移動以避免顯示標籤。我想阻止這種情況的發生,並且即使標籤被隱藏,也可以讓按鈕保持在同一個地方。

下面是代碼:如何讓JLabel在不可見時消耗空間?

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

回答

3
this.setPreferredSize(new Dimension(400,200)); 
this.setMinimumSize(new Dimension(400,200)); 

所以pack()不能干涉。

+0

感謝您的幫助。我從來沒有想過要設定最小尺寸。 – TheHarrisonCrafter

2

使用CardLayout。添加JLabel並清空JPanel。當需要時,不要將其看成可見/不可見的交換顯示JLabelJPanel的牌。

2

擴展JFrame中是不可取的,更好的擴展JPanel把所有的組件內,然後將其添加到一個JFrame

你需要學習如何使用SwingUtilities.invokeLater():見example你應該怎麼看起來像

你需要了解LayoutTutorial

在你的代碼非常愚蠢和簡單的方法是:

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

我不能使用'MainFrame frame = new MainFrame();'? – TheHarrisonCrafter

2

默認情況下,您使用的是BorderLayout。你可以嘗試像這樣:

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

而不是在中心。

或者,您可以創建這些2個面板的中間容器板,或考慮其他佈局管理器一樣BoxLayout