【问题标题】:One JFrame and Two JPanels一个 JFrame 和两个 JPanel
【发布时间】:2012-02-22 11:45:10
【问题描述】:

我是 Java 新手,我对面板有疑问。我的程序中有一个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);
 }
}

【问题讨论】:

  • 请提供我们可以编译的代码sn-p。至少准确描述您当前代码的问题所在。
  • 请学习java命名约定并遵守它们

标签: java swing


【解决方案1】:

BorderLayout 每个约束只能处理一个组件,即你在 CENTER 中添加 p2 的那一刻,p1 被遗忘。所以要么在你的 actionListeners 中删除/添加,要么使用另一个 LayoutManager,比如 f.i. CardLayout.

【讨论】:

    【解决方案2】:

    要实现这些目标,您需要使用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();
                }
            });
        }
    }
    

    【讨论】:

      【解决方案3】:

      我认为问题在于 CENTER 中只能有一个 JComponent。因此,将两个面板包装在一个面板中,并且只在 CENTER 中:

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

      【讨论】:

        【解决方案4】:

        我会为此功能使用选项卡式窗格。详情请见How to Use Tabbed Panes

        【讨论】:

          【解决方案5】:

          您可以使用 BorderLayout 来做到这一点,您只需要记住重新添加要显示的面板即可。您实际上已经解决了重新验证问题,因为您使用的是 setVisible()。

          这基本上就是 kleopatra 所说的 - 这是代码:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-06-13
            • 2014-04-12
            • 2014-06-05
            • 2014-01-26
            • 1970-01-01
            • 2014-04-27
            • 2013-06-01
            相关资源
            最近更新 更多