简体   繁体   中英

How can I make a 'Play ' button in a JFrame go to another class without adding another JFrame?

I did this code where Once the user clicked on the button play it shows another class called JFrame4 where they show another JFrame

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
   JOptionPane.showMessageDialog(null, "this is help .....","Aide",JOptionPane.INFORMATION_MESSAGE);
}                                     

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    dispose();
    JFrame4 play= new JFrame4(); 
    play.setVisible(true);
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    dispose();
}         

and here's the class JFrame4:

public class JFrame4 extends javax.swing.JFrame {

/**
 * Creates new form JFrame4
 */
public JFrame4() {
    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();
    jLabel2 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setLayout(null);

    jLabel2.setFont(new java.awt.Font("Tahoma", 1, 48)); // NOI18N
    jLabel2.setForeground(new java.awt.Color(0, 51, 51));
    jLabel2.setText("WELCOME");
    jPanel1.add(jLabel2);
    jLabel2.setBounds(130, 10, 760, 70);

    jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jButton1.setText("Back to menu ");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });
    jPanel1.add(jButton1);
    jButton1.setBounds(720, 480, 170, 25);

    jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\asus\\Desktop\\Files\\40639a0054b339d12685d654878daa25.jpg")); // NOI18N
    jPanel1.add(jLabel1);
    jLabel1.setBounds(0, 0, 910, 530);

    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, 915, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
    );

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    dispose();
    NewJFrame play= new NewJFrame(); 
    play.setVisible(true);
}                                        

/**
 * @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(JFrame4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(JFrame4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(JFrame4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(JFrame4.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 JFrame4().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   
}

But I want it to stay on the same frame (like usual games).

Simply swap contents:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    dispose();
    JPanel play= new JFrame4().getContentPane(); 
    myFrame.setContentPane(play);
}

Note, you heavily violate Swing's conventions, by not directly passing the event to an action listener, with the signature:

public void actionPerformed(ActionEvent evt)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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