JAVA对于单窗口和多个JPanel面板的切换

写一个简单的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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    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))
        );
    }// </editor-fold>                        

    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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    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))
        );
    }// </editor-fold>                        

    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                   
}
  • 11
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值