2011-06-25 109 views
7

我有一個擴展了JFrame的類,它有一個BorderLayout。它有兩個JPanel類型的私有實例變量。它們代表按鈕面板,被稱爲flipButton和confidenceButtons。當你點擊按鈕時,按鈕面板將被另一個按鈕面板替換。也就是說,如果你點擊了flipButton中的一個按鈕,flipButton被confidenceButtons取代。我試圖做這樣的:在JFrame中用JPanel替換JPanel

 
    private class FlipListener implements ActionListener{ 
    public void actionPerformed(ActionEvent e){ 
     remove(flipButton); 
     add(confidenceButtons,BorderLayout.SOUTH); 
     validate(); 
     ... 
    } 
    } 
    private class ColorListener implements ActionListener{ 
    ... 
    public void actionPerformed(ActionEvent e){ 
     ... 
     remove(confidenceButtons); 
     add(flipButton,BorderLayout.SOUTH); 
     validate(); 
    } 
    } 

在flipButton按鈕具有FlipListeners和confidenceButtons的那些有ColorListeners。當程序運行時,點擊一個按鈕將刪除面板,但沒有任何東西被添加來替換它。我究竟做錯了什麼?

EDIT

CardLayout原來是簡單和容易的解決方案。事實證明,上面的代碼確實有效;問題是我的代碼的另一部分有一個錯字。 >。 <但是,我一直在使用這些方法時遇到問題,我發現CardLayout爲我簡化了它。謝謝。

回答

6

使用外觀極好例子,如圖所示here

Game viewHigh Scores view

+0

也是我的第一個想法。 Cardlayout專爲此用途而設計,是最佳的可維護選擇。 – extraneon

+0

簡單的解決方案,像魅力一樣工作。謝謝! – Shelley

5

重新驗證()+重繪()應招,例如here

編輯:

覺得你有問題,對於該herehere,並再次example by trashgod例子,隨意建基於代碼的問題又來

另一種方式是看由安德魯·湯普森加入:-) +1

+0

我看了一個例子,但不太明白。爲什麼重繪必要,因爲我不使用圖形?我會在confidenceButtons上調用所有三種方法嗎?這個例子看起來非常具體,我真的不知道如何適應我的代碼。 – Shelley

+0

+1另請參閱此[示例](http://stackoverflow.com/questions/5812002/removeall-not-removing-at-next-validate/5812981#5812981)。 – trashgod

+0

@Shelley示例具體avourt revalidate +驗證+ repaint,回到關於重繪()的questin,在大多數情況下GUI工作正常,並不需要repaint();嗯,並沒有存在任何Swing教程或有關的詳細說明:-) – mKorbel

1

嘗試使用的getContentPane()調用刪除(),加()方法等..:

getContentPane().remove(flipButton); 
getContentPane().add(confidenceButtons,BorderLayout.SOUTH); 
getContentPane().revalidate(); 
getContentPane().repaint(); 

編輯: 這段代碼波紋管,我的工作:

import java.awt.BorderLayout; 
import java.awt.HeadlessException; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 


public class Frame extends JFrame { 
JPanel flipButton =new JPanel(); 
JPanel confidenceButtons =new JPanel(); 



    public Frame() throws HeadlessException { 
    super(); 
    this.setLayout(new BorderLayout()); 
    JButton b1=new JButton("flip"); 
    b1.addActionListener(new FlipListener()); 
    flipButton.add(b1); 

    JButton b2=new JButton("color"); 
    b2.addActionListener(new ColorListener()); 
    confidenceButtons.add(b2); 
    this.getContentPane().add(flipButton,BorderLayout.SOUTH); 
    this.setSize(250,250); 
    this.pack(); 
    this.setVisible(true); 

} 
    private class FlipListener implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      remove(flipButton); 
      add(confidenceButtons,BorderLayout.SOUTH); 
      validate(); 
      repaint(); 

     } 
     } 
     private class ColorListener implements ActionListener{ 

     public void actionPerformed(ActionEvent e){ 

      remove(confidenceButtons); 
      add(flipButton,BorderLayout.SOUTH); 
      validate(); 
      repaint(); 
     } 
     } 
    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     new Frame(); 

    } 

} 
+0

如果您可以發佈完整的JFrame類代碼,我可以嘗試修復它。也許嘗試使用另一個佈局,而不是Borederlayout。 b/c BorderLayout有時會引起一些奇怪的行爲 – othman

+0

我認爲這應該不重要。我讀過添加,刪除等方法自動添加到內容窗格時單獨調用,所以getContentPane()是不必要的。 – Shelley

+0

我試過使用CardLayout,它現在完美的工作(CardLayout原來是一個簡單的解決方案)。感謝您的幫助。 – Shelley