Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 按下按钮后,打开另一个面板内的另一个面板_Java_User Interface_Jpanel_Action_Panel - Fatal编程技术网

Java 按下按钮后,打开另一个面板内的另一个面板

Java 按下按钮后,打开另一个面板内的另一个面板,java,user-interface,jpanel,action,panel,Java,User Interface,Jpanel,Action,Panel,我得到一个错误,说我没有添加一些方法(执行的操作),但我已经添加了。我打开配电盘有困难 public class panel1 extends JPanel implements ActionListener(){ private panel2 p2=new panel2(); private JButton button; public panel1(){ button=new JButton("open panel2"); add(button,Bo

我得到一个错误,说我没有添加一些方法(执行的操作),但我已经添加了。我打开配电盘有困难

public class panel1 extends JPanel implements ActionListener(){
    private panel2 p2=new panel2();
    private JButton button;

    public panel1(){
    button=new JButton("open panel2");
    add(button,BorderLayout.BEFORE_FIRST_LINE);
    button.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ae) {

            add(p2);

        }

    });

  }

}考虑这些变化:

public class panel1 extends JPanel implements ActionListener(){
    private panel2 p2=new panel2();
    private JButton button;

    public panel1(){
    button=new JButton("open panel2");
    add(button,BorderLayout.BEFORE_FIRST_LINE);
    button.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ae) {
            //Add readability: Where to add?
            panel1.this.add(p2);
        }

    });

  }

  //THIS HERE makes your panel1 implment ActionListener
  @Override
  public void actionPerformed(ActionEvent ae) {

  }
}

还请注意常见的Java命名约定-类名应以大写字母开头(
Panel1扩展了JPanel

Panel1需要实现ActionListener接口所描述的actionPerformed方法,但还不是已经具有@Override public void actionPerformed(ActionEvent ae)的方法{add(p2);}?很抱歉,我是新来编程的。不,这是匿名类的实现。谢谢!这消除了错误。虽然它仍然没有打开第二个面板。也许你应该添加(panel2,BorderLayout.CENTER)或其他什么?
You can check the below code for replacing jpanel without switching jframe.  



contentPanel.removeAll();
        contentPanel.repaint();
        contentPanel.revalidate();
        contentPanel.add(//add your panel here);
        contentPanel.repaint();
        contentPanel.revalidate();