2012-09-30 67 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

相关问题