如何在JFrame中创建“播放”按钮转到另一个类而不添加另一个JFrame?

时间:2015-06-03 18:54:47

标签: java netbeans jframe jbutton

我做了这段代码,一旦用户点击按钮播放,就会显示另一个名为JFrame4的类,其中显示另一个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();
}         

这里是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                   
}

但我希望它能保持同一帧(就像通常的游戏一样)。

1 个答案:

答案 0 :(得分:0)

只需交换内容:

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

注意,你严重违反了Swing的约定,没有通过签名直接将事件传递给动作监听器:

public void actionPerformed(ActionEvent evt)