2012-05-09 9 views
2

私はJavaを使い慣れていません。私は自分で解決策を見つけようとしましたが、進める方法がわかりません。1つのJFrameから別のJFrameに変数を渡すには

I 2 Jframes(JF1、JF2)を有し、そしてIはJF2

にJF1から変数を渡す必要

これはJF1のコードです。注意:iが値xを必要とする、値Xからそれを得ますJTextfeild。

は、いずれかが私を

/* 
* JF1.java 
* 
* Created on 04 مايو, 2012, 09:01 م 
*/ 

package finalpro; 

import java.io.BufferedInputStream; 
import javax.swing.JOptionPane; 

/** 
* 
* @author OmaR 
*/ 
public class JF1 extends javax.swing.JFrame { 

    /** Creates new form JF1 */ 
    public JF1() { 
     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. 
    */ 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jLabel1 = new javax.swing.JLabel(); 
     JB1 = new javax.swing.JButton(); 
     JB3 = new javax.swing.JButton(); 
     JB2 = new javax.swing.JButton(); 
     jLabel2 = new javax.swing.JLabel(); 
     jTextField1 = new javax.swing.JTextField(); 
     jButton1 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jLabel1.setText("Online Test Creater"); 

     JB1.setText("Make"); 
     JB1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       JB1ActionPerformed(evt); 
      } 
     }); 

     JB3.setText("Quit"); 
     JB3.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       JB3ActionPerformed(evt); 
      } 
     }); 

     JB2.setText("Reset"); 
     JB2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       JB2ActionPerformed(evt); 
      } 
     }); 

     jLabel2.setText("Number Of Question"); 

     jTextField1.addMouseListener(new java.awt.event.MouseAdapter() { 
      public void mouseClicked(java.awt.event.MouseEvent evt) { 
       jTextField1MouseClicked(evt); 
      } 
     }); 

     jButton1.setText("Main"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(43, 43, 43) 
         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)) 
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
         .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(34, 34, 34))) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(JB2) 
        .addComponent(JB3) 
        .addComponent(JB1) 
        .addComponent(jButton1)) 
       .addGap(30, 30, 30)) 
     ); 

     layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {JB1, JB2, JB3, jButton1}); 

     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(19, 19, 19) 
         .addComponent(jLabel1)) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(31, 31, 31) 
         .addComponent(JB1))) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(jLabel2) 
        .addGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
          .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(jButton1)) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addComponent(JB2) 
         .addGap(18, 18, 18) 
         .addComponent(JB3))) 
       .addContainerGap(37, Short.MAX_VALUE)) 
     ); 

     layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {JB1, JB2, JB3, jButton1}); 

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

    private void JB3ActionPerformed(java.awt.event.ActionEvent evt) {          
     // TODO add your handling code here: 
     System.exit(0); 
    }         

    private void JB2ActionPerformed(java.awt.event.ActionEvent evt) {          
     // TODO add your handling code here: 
     jTextField1.setText(""); 

    }         

    private void JB1ActionPerformed(java.awt.event.ActionEvent evt) {          
     // TODO add your handling code here: 

      JF1 JFBU = new JF1() ; 
      JF2 JCreate = new JF2(); 

      this.setVisible(false); 
     int z = Integer.parseInt(jTextField1.getText()); 


      if (z == 0) 
      { 
       JOptionPane.showMessageDialog(null, " You Enter 0 Please write anumber "); 

       } 
      else 
      { 
      JCreate.setVisible(true); 
      } 


    }         

    private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {           
     // TODO add your handling code here: 
     JOptionPane.showMessageDialog(null, "The Max Number of Questions Is 10"); 
    }           

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     JFmain JFm = new JFmain(); 
     JFm.setVisible(true); 
     this.setVisible(false); 
    }           

    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new JF1().setVisible(true); 

      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton JB1; 
    private javax.swing.JButton JB2; 
    private javax.swing.JButton JB3; 
    private javax.swing.JButton jButton1; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JTextField jTextField1; 
    // End of variables declaration     


} 

私はJF2を持ってください助けることができる、と私はそれにヴァールzを渡す必要があります。パラメータint zとコンストラクタを作成します

+3

(offtopic)>JF2 jCreate = new JF2(z);または使用してセッターを 'lowercaseStartingCamelCase '。したがって、 'JFmain Jfm = new JFmain()'の宣言は、 'JFmain jfm = new JFmain()'の方が良いでしょう。同様にあなたの 'JButton'変数 –

+0

コメントありがとうございます。私はそれをやるようになります –

答えて

2

public class JF2 extends JFrame { 
    private int z; 

    public void setZ(int z) { 
     this.z = z; 
    } 

    public int getZ() { 
     return z; 
    } 

    public JF2(int z) { 
     this.z = z; 
    } 
} 

そして、あなたがJF2作成するときに変数zを渡す - それは使用してJavaの変数名に名前を付けるのが通例だjCreate.setZ(z);

+0

助けてくれてありがとう、本当に私はそれを作る方法は分かりません....私はconstractorを渡そうとしますが、間違ってしまいます>>>私はJF1(int z)>>>>またはJF2を作成しますか?JF1のメインフレーム....本当にありがとうございました。 –

+1

例外のスタックトレースで質問を更新できますか?またはあなたが持っているエラー? – Crazenezz

+0

エラーはありませんが、Jframeから別の変数に変数を渡すことはできません。 –

関連する問題