Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一个JFrame和两个JPanel_Java_Swing - Fatal编程技术网

Java 一个JFrame和两个JPanel

Java 一个JFrame和两个JPanel,java,swing,Java,Swing,我是Java新手,在面板方面有问题。我的程序中有一个JFrame和两个jpanel 当我单击按钮1,框架中将显示面板1 当我单击按钮2时,panel2将显示在框架中,panel1将消失/隐藏 问题是panel1不能仅显示panel2。如何以这种方式显示两个面板 这是我的代码: public class test{ public static void main(String args[]){ JButton b1 = new JButton("show p1"); J

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

  • 当我单击
    按钮1
    ,框架中将显示
    面板1
  • 当我单击
    按钮2
    时,
    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);
 }
}

BorderLayout每个约束只能处理一个组件,即在中心添加p2时,p1将被忽略。因此,要么在actionListeners中进行删除/添加,要么使用另一个LayoutManager,如f.i.

我认为问题在于,您只能在中间使用一个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);
    }

}

要实现这些目标,你需要使用

此外,一旦从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();
            }
        });
    }
}

您可以使用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);

我将使用选项卡式窗格来实现此功能。有关更多详细信息,请参阅


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