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

时间:2015-12-05 04:53:48

标签: java user-interface jpanel action panel

我一直在收到错误,说我没有添加一些方法(已执行的操作),但我已经做了。我打开面板2时遇到了麻烦。

var addDigits = function(string) {
  newNum = string.split('');
  var sum = 0;
  for(var i = 0 ; i < newNum.length ; i++) sum += parseInt(newNum[i]);
  return sum;
};

console.log(addDigits("1234"));

}

2 个答案:

答案 0 :(得分:1)

考虑这些变化:

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

  }
}

还请注意commom Java命名约定 - 类名应以大写字母(Panel1 extends JPanel)开头

答案 1 :(得分:0)

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