简体   繁体   English

Java清除netbeans中的jFrame

[英]Java clearing a jFrame in netbeans

I am creating a game currently and ran into a large question how do you clear jFrames then add new content i have seen all the other questions and answers but they don't work heres my code.我目前正在创建一个游戏并遇到一个大问题,你如何清除 jFrames 然后添加新内容我已经看到了所有其他问题和答案,但它们在我的代码中不起作用。 i want my single player button to clear the screen then add new content我希望我的单人播放器按钮清除屏幕然后添加新内容

 *
 * @author Kyle
 */
public class mainscreen extends javax.swing.JFrame {

    /**
     * Creates new form main screen
     */
    public mainscreen() {
        setTitle("My, World");

        initComponents();

    }




    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jcredit2 = new javax.swing.JLabel();
        jcredit1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jcredit3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));

        jcredit2.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14)); // NOI18N
        jcredit2.setForeground(new java.awt.Color(255, 255, 255));

        jcredit1.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14)); // NOI18N
        jcredit1.setForeground(new java.awt.Color(255, 255, 255));

        jButton1.setBackground(new java.awt.Color(0, 0, 0));
        jButton1.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 24)); // NOI18N
        jButton1.setForeground(new java.awt.Color(255, 255, 255));
        jButton1.setText("Singleplayer");
        jButton1.setOpaque(true);
        jButton1.setBorder(null);
        jButton1.setContentAreaFilled(false);
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mouseCsingleplayer(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                mouseentersingleplayer(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                mouseexitsingleplayer(evt);
            }
        });

        jButton2.setBackground(new java.awt.Color(0, 0, 0));
        jButton2.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 24)); // NOI18N
        jButton2.setForeground(new java.awt.Color(255, 255, 255));
        jButton2.setText("Multiplayer");
        jButton2.setBorder(null);
        jButton2.setName(""); // NOI18N
        jButton2.setContentAreaFilled(false);
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                mouseentermultiplayer(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                mouseexitmultiplayer(evt);
            }
        });

        jButton3.setBackground(new java.awt.Color(0, 0, 0));
        jButton3.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 24)); // NOI18N
        jButton3.setForeground(new java.awt.Color(255, 255, 255));
        jButton3.setText("Options");
        jButton3.setBorder(null);
        jButton3.setOpaque(false);

        jButton3.setContentAreaFilled(false);
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                mousenteroptions(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                Mouseexitoptions(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 70)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("My, World");

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));

        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                mouseclickfrog(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                mouseexitfrog(evt);
            }
        });

        jButton4.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 24)); // NOI18N
        jButton4.setForeground(new java.awt.Color(255, 255, 255));
        jButton4.setText("Credits");
        jButton4.setBorder(null);
        jButton4.setOpaque(false);
        jButton3.setOpaque(false);

        jButton4.setContentAreaFilled(false);
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Jbutton4CLicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                Jbutton4hover1(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButton4Mouseexitoptions(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 187, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jcredit1, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                            .addComponent(jcredit2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jcredit3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(22, 22, 22))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jcredit1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcredit2, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcredit3, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(54, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void mouseentersingleplayer(java.awt.event.MouseEvent evt) {                                        
 jButton1.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 27));
 jLabel2.setText(" Create a new world or load a old one!");
 jButton1.setText("<Singleplayer>");

    }                                       

    private void mouseexitsingleplayer(java.awt.event.MouseEvent evt) {                                       
 jButton1.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 25));
  jLabel2.setText("");
  jButton1.setText("Singleplayer");
    }                                      

    private void mouseentermultiplayer(java.awt.event.MouseEvent evt) {                                       
 jButton2.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 27));
  jLabel2.setText(" Play LAN or online with friends!");
  jButton2.setText("<Multiplayer>");
    }                                      

    private void mouseexitmultiplayer(java.awt.event.MouseEvent evt) {                                      
 jButton2.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 24));
  jLabel2.setText("");
  jButton2.setText("Multiplayer");
    }                                     

    private void mousenteroptions(java.awt.event.MouseEvent evt) {                                  
 jButton3.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 27));
  jLabel2.setText(" Configure your life!");
  jButton3.setText("<Options>");
    }                                 

    private void Mouseexitoptions(java.awt.event.MouseEvent evt) {                                  
 jButton3.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 24));
  jLabel2.setText("");
  jButton3.setText("Options");

    }                                 

    private void mouseclickfrog(java.awt.event.MouseEvent evt) {                                
  jLabel2.setText("Copyright, Applacation Amphibian 2013");
    }                               

    private void mouseexitfrog(java.awt.event.MouseEvent evt) {                               
jLabel2.setText("");
    }                              

    private void Jbutton4CLicked(java.awt.event.MouseEvent evt) {                                 
jButton4.setText("<credits>");

    }                                

    private void Jbutton4hover1(java.awt.event.MouseEvent evt) {                                
jButton4.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 27));
jcredit1.setText("Head of project: Kyle Jackson");
jcredit2.setText("Graphics: Lachlan Hatfeild");
jButton4.setText("<Credits>");

    }                               

    private void jButton4Mouseexitoptions(java.awt.event.MouseEvent evt) {                                          
jButton4.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 24));
jcredit1.setText("");
jcredit2.setText("");
jButton4.setText("Credits");
    }                                         

    private void mouseCsingleplayer(java.awt.event.MouseEvent evt) {                                    

    }                                   

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(mainscreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(mainscreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(mainscreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(mainscreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new mainscreen().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel jcredit1;
    private javax.swing.JLabel jcredit2;
    private javax.swing.JLabel jcredit3;
    // End of variables declaration                   

    private void clear() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

Break your interface into separate panels (such JPanel), place everything you need into these individual panels将您的界面分成单独的面板(例如 JPanel),将您需要的所有内容放入这些单独的面板中

On your main frame, set the layout as a CardLayout在主框架上,将布局设置为 CardLayout

Add each of previous panels to the frame and use the CardLayout API to switch between them将前面的每个面板添加到框架中并使用 CardLayout API 在它们之间切换

Check outHow to use Card Layout查看如何使用卡片布局

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM