一个视窗设计的很菜的问题。
小弟开始学JAVA的视窗设计,遇到一个问题,由于现实在没人可问,所以只能上来请大家指导指导。问题是,共有两个JPanel,运行一个jPanel1后,按下一个按钮后如何弹出另个一个JPanel呢,以下是程序的全部代码,事件部分是空的(注释的的那两行),请高手帮忙填一下,不尽感激。(情况就类似于,运行一个程序,在程序界面上有一个按钮,叫“配置程序的参数”,按下去后弹出一个面板,可以进行参数的配置,完了按确定返回。)
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test extends JFrame{
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
BorderLayout borderLayout1 = new BorderLayout();
BorderLayout borderLayout2 = new BorderLayout();
public test() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1=(JPanel)this.getContentPane();
jLabel1.setText("this is panel1");
jLabel2.setText("this is panel2 ");
jButton1.setText("确定并返回panel1");
jPanel2.setLayout(borderLayout2);
jPanel1.setLayout(borderLayout1);
jButton2.setToolTipText("");
jButton2.setText("运行panel2设置程序运行参数");
jButton2.addActionListener(new test_jButton2_actionAdapter(this));
jPanel2.add(jLabel2, BorderLayout.SOUTH);
jPanel2.add(jButton1, BorderLayout.SOUTH);
jPanel1.add(jButton2, BorderLayout.SOUTH);
jPanel1.add(jLabel1, BorderLayout.NORTH);
jButton1.addActionListener(new test_jButton1_actionAdapter(this));
}
public static void main(String []args){
test t1=new test();
t1.setSize(new Dimension (300,400));
t1.setVisible(true);
}
void jButton2_actionPerformed(ActionEvent e) {
//运行panel2,进行系统参数的设置,并且panel1不可操作(就是鼠标点不到panel1了),只能由panel返回后才能继续操作
/*下面是我自己以为的代码,是错误的
jPanel2=(JPanel)this.getContentPane();
jPanel2.setSize(new Dimension (300, 400));
jPanel2.setVisible(true);*/
}
void jButton1_actionPerformed(ActionEvent e) {
//设置完毕,返回panel1
}
}
class test_jButton2_actionAdapter implements java.awt.event.ActionListener {
test adaptee;
test_jButton2_actionAdapter(test adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class test_jButton1_actionAdapter implements java.awt.event.ActionListener {
test adaptee;
test_jButton1_actionAdapter(test adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}