2012-02-22 58 views
2

我是Java的新手,我对面板有问题。我的程序中有一个JFrame和两个JPanels一个JFrame和两个JPanels

  • 当我点击button1,panel1将显示在框架中。
  • 当我点击button2时,panel2将显示在框架中,panel1将消失/隐藏。

问题是panel1只能显示panel2。如何以这种方式显示两个面板?

这是我的代码:

public class test{ 

public static void main(String args[]){ 

    JButton b1 = new JButton("show p1"); 
    JButton b2 = new JButton("show p2"); 
    JLabel l1 = new JLabel("This is p1"); 
    JLabel l2 = new JLabel("This is p2"); 

    final JPanel p1 = new JPanel(new FlowLayout()); 
    p1.add(l1); 
    final JPanel p2 = new JPanel(new FlowLayout()); 
    p2.add(l2); 
    JPanel buttonPNL = new JPanel(new FlowLayout()); 
    buttonPNL.add(b1); 
    buttonPNL.add(b2); 

    b1.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
       p1.setVisible(true); 
       p2.setVisible(false); 
     } 
    }); 

    b2.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
        p1.setVisible(false); 
        p2.setVisible(true); 
      } 
     }); 

    JFrame frm = new JFrame(); 
    frm.setLayout(new BorderLayout()); 
    frm.add(p1,BorderLayout.CENTER); 
    frm.add(p2,BorderLayout.CENTER); 
    frm.add(buttonPNL,BorderLayout.SOUTH); 
    frm.setVisible(true); 
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frm.setSize(300,300); 
} 
} 
+0

请提供我们可以编译的代码片段。至少要描述你的当前代码到底是什么问题。 – ARRG 2012-02-22 11:48:06

+2

请了解java的命名约定并坚持他们 – kleopatra 2012-02-22 14:00:52

回答

6

BorderLayout这只能处理每一个约束的组成部分,那就是你在中心加入P2的时刻,P1被遗忘。因此,要么删除/添加您的actionListeners或使用另一个布局管理器,如f.i. CardLayout

+0

这真的很有帮助。谢谢.. – jcom 2012-03-09 13:44:18

2

我认为问题在于你只能在CENTER中有一个JComponent。所以换两个面板在面板中只具有在中心:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class JFrameProblem { 

    public static void main(String[] args){ 

     JButton b1 = new JButton("show p1"); JButton b2 = new JButton("show p2"); 

     JLabel l1 = new JLabel("This is p1"); 
     JLabel l2 = new JLabel("This is p2"); 

     final JPanel p1 = new JPanel(new FlowLayout()); 
     p1.add(l1); 
     final JPanel p2 = new JPanel(new FlowLayout()); 
     p2.add(l2); 
     p2.setVisible(false); 

     JPanel panelPNL = new JPanel(new FlowLayout()); 
     panelPNL.add(p1); 
     panelPNL.add(p2); 

     JPanel buttonPNL = new JPanel(new FlowLayout()); 
     buttonPNL.add(b1); 
     buttonPNL.add(b2); 

     b1.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       p1.setVisible(true); 
       p2.setVisible(false); 
      } 
     }); 

     b2.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       p1.setVisible(false); 
       p2.setVisible(true); 
      } 
     }); 

     JFrame frm = new JFrame(); 
     frm.setLayout(new BorderLayout()); 
     frm.add(panelPNL,BorderLayout.CENTER); 
     frm.add(buttonPNL, BorderLayout.SOUTH); 
     frm.setVisible(true); 
     frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frm.setSize(300,300); 
     frm.pack(); 
     frm.setVisible(true); 
    } 

} 
3

达到你需要使用CardLayout这样的事情。此外,一旦从JFrame中删除旧的JPanel以添加新的JPanel,则始终在JFrame上执行revalidate()和repaint()以实现更改。请记住,在任何给定位置的任何给定时间只能添加一个组件。试试这个代码的修改:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class PanelTest 
{ 
    public PanelTest() 
    { 
     JButton b1 = new JButton("show p1"); 
     JButton b2 = new JButton("show p2"); 
     JLabel l1 = new JLabel("This is p1"); 
     JLabel l2 = new JLabel("This is p2"); 

     final JPanel p1 = new JPanel(new FlowLayout()); 
     p1.add(l1); 
     final JPanel p2 = new JPanel(new FlowLayout()); 
     p2.add(l2); 
     JPanel buttonPNL = new JPanel(new FlowLayout()); 
     buttonPNL.add(b1); 
     buttonPNL.add(b2); 

     final JFrame frm = new JFrame(); // shifted this line here. 

     b1.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
        // Added by me , these three lines 
        if (p2.isShowing()) 
        { 
         frm.remove(p2); 
         frm.add(p1, BorderLayout.CENTER);     
         frm.revalidate(); // for JDK 1.7+ 
         //frm.getRootPane().revalidate(); // for JDK 1.6 or lower 
         frm.repaint(); 
        } 
      } 
     }); 

     b2.addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent e){ 
         if (p1.isShowing()) 
         { 
          frm.remove(p1); 
          frm.add(p2, BorderLayout.CENTER);     
          // Added by me , these three lines 
          frm.revalidate(); // for JDK 1.7+ 
          //frm.getRootPane().revalidate(); // for JDK 1.6 or lower 
          frm.repaint(); 
         } 
       } 
     }); 


     frm.setLayout(new BorderLayout()); 
     frm.add(p1,BorderLayout.CENTER); 
     frm.add(buttonPNL,BorderLayout.SOUTH); 
     frm.setVisible(true); 
     frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frm.setSize(300,300); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new PanelTest(); 
      } 
     }); 
    } 
} 
1

你可以用BorderLayout的做到这一点,你只需要记住重新添加你要显示的面板。由于您使用setVisible(),您实际上已经解决了重估问题。

这基本上就是克列奥帕特拉说 - 这里是代码:

JButton b1 = new JButton("show p1"); 
JButton b2 = new JButton("show p2"); 
JLabel l1 = new JLabel("This is p1"); 
JLabel l2 = new JLabel("This is p2"); 

final JPanel p1 = new JPanel(new FlowLayout()); 
p1.add(l1); 
final JPanel p2 = new JPanel(new FlowLayout()); 
p2.add(l2); 
JPanel buttonPNL = new JPanel(new FlowLayout()); 
buttonPNL.add(b1); 
buttonPNL.add(b2); 

final JFrame frm = new JFrame(); 
b1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     p1.setVisible(true); 
     p2.setVisible(false); 
     frm.add(p1, BorderLayout.CENTER); 
    } 
}); 

b2.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     p1.setVisible(false); 
     p2.setVisible(true); 
     frm.add(p2, BorderLayout.CENTER); 
    } 
}); 

frm.setLayout(new BorderLayout()); 
frm.add(p1, BorderLayout.CENTER); 
p2.setVisible(false); // initial state 
frm.add(buttonPNL, BorderLayout.SOUTH); 
frm.setVisible(true); 
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frm.setSize(300, 300);