2011-06-25 111 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