写一个简单的2048游戏时,本来思路是想对于每个游戏界面进行一次new JFrame,并在新窗口中添加组件,将旧窗口用setVisible(false)进行隐藏。但是这样写出现了一个问题,就是打开其他窗口界面会导致之前的部分界面出现无法关闭或者隐藏的情况,因为本来的设想是在新窗口打开后,对之前旧窗口隐藏,但是还是在某些界面切换时出现无法将旧界面全部隐藏,改了也改不掉。后来,经过老师看了一下代码,说对于一个需要界面的程序,只创建一个JFrame窗口,对于其他的组件和界面写成一个JPanel面板,写好之后把面板往这个窗口中添加,需要新界面时,将原来的面板隐藏,天添加新的面板。
按照这个思路找了个样例,又发现了一个问题:如果只向一个窗口中不断添加面板,但是事件监听在当前面板上,添加新面板得话还得new一个新的窗口,然后问了问别人,得到了一个新的思路,就是将JFrame窗口传递到其他面板之中,这样就能一直在一个窗口中添加不同的面板。
以下是根据该思路写的四段测试代码,这种方法只创建一个窗口,通过按钮时间在两个面板之间切换:
public class JavaApplication13 {
public static void main(String[] args) {
NewJFrame n2=new NewJFrame();//创建一个窗口
NewJPanel p2=new NewJPanel(n2);//创建一个面板,并将窗口传入
n2.add(p2);//窗口中添加面板p2
n2.setVisible(true);//显示窗口
}
}
//窗口
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JavaApplication13 {
public static void main(String[] args) {
NewJFrame n2=new NewJFrame();//创建一个窗口
NewJPanel p2=new NewJPanel(n2);//创建一个面板,并将窗口传入
n2.add(p2);//窗口中添加面板p2
n2.setVisible(true);//显示窗口
}
}
//面板2中除了按钮名称和面板1不一样,其他基本功能相同
//面板1
public class NewJPanel extends javax.swing.JPanel {
/**
* Creates new form NewJPanel
*/
NewJFrame n2=null;
public NewJPanel(NewJFrame n2) {
initComponents();
this.n2=n2;//通过构造函数接收之前传过来的面板
}
@SuppressWarnings("unchecked")
//
private void initComponents() {
jButton1 = new javax.swing.JButton();//添加按钮
jButton1.setText("测试1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
NewJPanel2 p1=new NewJPanel2(n2);//新建面板NewJPanel2,并将窗口n2传入
setVisible(false);//隐藏当前面板
n2.add(p1);//在窗口中添加面板p1
n2.setVisible(true);//显示面板
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(115, 115, 115)
.addComponent(jButton1)
.addContainerGap(188, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(84, 84, 84)
.addComponent(jButton1)
.addContainerGap(187, Short.MAX_VALUE))
);
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
//面板2
public class NewJPanel2 extends javax.swing.JPanel {
/**
* Creates new form NewJPanel
*/
NewJFrame n2=null;
public NewJPanel2(NewJFrame n2) {
initComponents();
this.n2=n2;
}
@SuppressWarnings("unchecked")
//
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton1.setText("测试2");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
NewJPanel p=new NewJPanel(n2);
setVisible(false);
n2.add(p);
n2.setVisible(true);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(115, 115, 115)
.addComponent(jButton1)
.addContainerGap(188, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(84, 84, 84)
.addComponent(jButton1)
.addContainerGap(187, Short.MAX_VALUE))
);
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}