2012-09-30 106 views
0

我有三個類。從另一個類傳遞值

Input.java(這是GUI類) 這個類發生在3個值通文本字段和具有按鈕,觸發事件和最後一個標籤,以顯示值的結果(雙精度值)

package javalearning; 

import javax.swing.JOptionPane; 


public class Input extends javax.swing.JFrame { 

    /** 
    * Creates new form Input 
    */ 
    public Input() { 
     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(); 
     result_lbl = new javax.swing.JLabel(); 
     value1 = new javax.swing.JTextField(); 
     value2 = new javax.swing.JTextField(); 
     value3 = new javax.swing.JTextField(); 
     btn1 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     result_lbl.setText("jLabel1"); 

     btn1.setText("jButton1"); 
     btn1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       btn1ActionPerformed(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() 
       .addGap(99, 99, 99) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(btn1) 
        .addComponent(result_lbl) 
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
         .addComponent(value1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE) 
         .addComponent(value2, javax.swing.GroupLayout.Alignment.TRAILING) 
         .addComponent(value3, javax.swing.GroupLayout.Alignment.TRAILING))) 
       .addContainerGap(100, Short.MAX_VALUE)) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
       .addGap(44, 44, 44) 
       .addComponent(value1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(18, 18, 18) 
       .addComponent(value2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(18, 18, 18) 
       .addComponent(value3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(btn1) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(result_lbl) 
       .addGap(23, 23, 23)) 
     ); 

     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() 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(15, Short.MAX_VALUE)) 
     ); 

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

    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) { 
     // TODO add your handling code here: 
     JOptionPane.showMessageDialog(null, "hello"); 
     Computation actual_triangle = new Computation(Double.parseDouble(value1.getText()),Double.parseDouble(value2.getText()),Double.parseDouble(value3.getText())); 

    } 

    public static void main(String args[]) { 

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

     /* 
     * Create and display the form 
     */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Input().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JButton btn1; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JLabel result_lbl; 
    private javax.swing.JTextField value1; 
    private javax.swing.JTextField value2; 
    private javax.swing.JTextField value3; 
    // End of variables declaration 
} 

Computation.java 這是所有的值都將被計算以產生結果

package javalearning; 
public class Computation { 

    private double side_a, side_b, side_c,result; 


    public Computation(double side_a, double side_b, double side_c) 
    { 
     this.side_a = side_a; 
     this.side_b = side_b; 
     this.side_c = side_c; 
     valueException ive = new valueException(); 
     //here if there is a message being sent from the valueException method then the message have to be relayed back to the Input.result_lbl.setText() 
     ive.checkvalidation(side_a, side_b, side_c); 

    } 

    public double getsSide_A() 
    { 
     return side_a; 
    } 

    public void setsSide_A(double side_a) 
    { 
     this.side_a = side_a; 
    } 

    public double getsSide_B() 
    { 
     return side_b; 
    } 

    public void setsSide_B(double side_b) 
    { 
     this.side_b = side_b; 
    } 

    public double getsSide_C() 
    { 
     return side_c; 
    } 

    public void setsSide_C(double side_c) 
    { 
     this.side_c = side_c; 
    } 

    public double calculateArea() 
    { 
     result = Math.sqrt(side_a*side_b*side_c); 
     return result; 
    } 

} 

valueException.java

的地方

這是根據用戶輸入輸出各種錯誤的類, 示例1:用戶鍵入字符串中的一個或所有文本字段,然後消息將返回到標籤類。 不過這是扭曲,這個類必須從Computation.java不Input.java

package javalearning; 
public class valueException { 

    String message; 
    double a_side, b_side,c_side,result; 


    public String checkvalidation(double a_side, double b_side, double c_side) 
    { 
     if((a_side + b_side > c_side)||(a_side + c_side > b_side)||(c_side + b_side > a_side)) 
     { 

      //NOTHING TO DO JUST PROCEED BACK TO COMPUTATION and go the Computation.calculatearea() method 

     } 

     else 
     { 
      message = "The values you entered cannot form a shape"; 
     } 
     return message; 
    } 

} 

問題1被稱爲: 所以我有困難搞清楚如何中繼回來自valueException類的消息返回到Computation類並從那裏到Input類?

問題2: 如果在Input.java中輸入的值沒有任何問題,則將過程返回到Computation類以執行程序的其餘部分,因爲這是唯一調用valueException類的類,我不確定要輸入的確切代碼。

我已經評論了我面臨問題的問題,我將這些意見放在了我認爲他們應該考慮的地方。

+0

你有沒有嘗試使用與這兩個模塊通信的類?這個類可以將模塊作爲成員變量,並通過公共方法使用它們並存儲計算結果。 –

+0

@JanKoester我認爲這個可憐的傢伙已經在爲自己的課程苦苦掙扎。 – JackyBoi

回答

1

嗯,我想你感到困惑的原因是因爲,你是不知道如何類之間傳遞值。 既然你說你開始學習編程(欣賞你)。但是,你已經抓住每一個機會學習和探索新事物,但不要太多,因爲這可能會使你偏離目標。所以,你可以爲你的 問題1做什麼: 您可以從異常類來計算類型的中繼回消息再重新轉發到輸入類

問題2: 一個簡單的突破;將打破語句,並返回控制到原來的代碼點,在這個循環/或在你的情況下,它是從它調用的類。

我假設你知道如何編碼其餘的。

+0

這是一個簡單的解決方案,我將異常消息傳遞給了計算,但是在這裏我不能使用ur break,並且在while循環中使用了一些研究。 TKS – noobprogrammer

1

您有一個視圖(Input)和一個模型(Computation)。你需要一個Controller坐在兩者之間,並知道如何處理下一步該做什麼。這將完成MVC模式。

Controller將接受活動從View,驗證和封裝的輸入值,元帥服務和Model對象實現用例,並決定什麼樣的未來View應根據用例的結果。

想想一個面向服務的體系結構:控制器可以調用以實現用例的基於接口的服務。我認爲最好是將用例封裝在服務中而不是控制器中,因爲控制器往往與特定的View密切相關。如果您決定寧願使用基於Web的View,則必須從頭開始不用服務,但如果您擁有這些服務,新的網絡(或手機或平板電腦)控制器可以簡單地訪問您的服務層並繼續哼唱。

UPDATE:

好,讀你的抗議爲「簡單」後,這裏就是我會建議。我認爲這個問題是你的異常類。你已經將它寫成了一個驗證器類。我認爲你應該拋出這個異常,如果我推薦的Controller執行它的工作並發現無效的情況。

package controller; 

// Making the Controller the ActionListener is the key. Take it away from the View. 
public class ShapeController implements ActionListener { 
    private Input inputView; 

    public Controller() { 
     this.inputView = new Input(); 
     this.addActionListener(this); // Make the Controller respond to action events from the View. 
    } 

    public void actionPerformed(ActionEvent e) { 
     double a_side, b_side, c_side; 
     // Get the input values from the input view 
     if((a_side + b_side > c_side)||(a_side + c_side > b_side)||(c_side + b_side > a_side)) { 
      Computation comp = new Computation() 
      // perform your calculation; all is well  
     } else { 
      // Problem; throw an exception 
      throw new InvalidShapeException(); 
     } 
     // figure out where to go next 
    } 
} 

我認爲你的value_exception類是錯誤的。使用它作爲我上面顯示,它這樣寫:

package controller; 

public class InvalidShapeException extends RuntimeException { 
    // Add whatever you want for messages and details. 
    public InvalidShapeException() { super(); } 
    public InvalidShapeException(String s) { super(s); } 
    public InvalidShapeException(Throwable t) { super(t); } 
    public InvalidShapeExceptionI(String s, Throwable t) { super(s, t); } 
} 
+0

不,MVC這是java 101非常基本我剛開始學習編程,更不用說JAVA了,所以一個非常簡單的解決方案就可以做到,tks! – noobprogrammer

+0

我認爲這*很簡單。我們正在討論增加一個班級。那有多難? – duffymo

+0

但是這不是一個基於網絡的,這是一個獨立的,儘管MVC的概念看起來更有趣,但還沒有到那裏,我可以決定下一個視圖,這意味着改變整個屏幕,我的主要問題仍然是通過來自不同類別的值 – noobprogrammer

相關問題