2016-08-05 6 views
1

OKなので、アクションメソッド内では問題ありませんが、新しいフレームに表示されています。ここでは、最初のフレーム(正しい値)に1回、次に2回目(それは未知の理由で0に戻されている)にカウントと平均スコアを2回表示する新しいコードを示します。新しいフレームに表示されたときにJava varが0に戻る

package testing2; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Testing2 extends JFrame implements ActionListener { 

    private double score; 
    protected double totalScore; 
    protected double averageScore; 
    protected int counter = 0; 
    JButton next = new JButton("Next"); 
    JButton display = new JButton("Display"); 
    JTextField scoreField = new JTextField("0", 3); 
    JTextField commentField = new JTextField(30); 
    JLabel explainationLabel = new JLabel("Please Score your overall     saticfaction with our app"); 
    JLabel explainationLabel2 = new JLabel("Enter any comments or suggestions"); 
    JLabel testDisplay = new JLabel(); 

    public Testing2() { 
     super("App Survey"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new FlowLayout()); 
     add(explainationLabel); 
     add(scoreField); 
     add(explainationLabel2); 
     add(commentField); 
     add(next); 
     add(display); 
     add(testDisplay); 
     next.addActionListener(this); 
     display.addActionListener(this); 
    } 

public Testing2 (String t){ 
    super(t); 
} 


public void actionPerformed(ActionEvent e) { 

    Object source = e.getSource(); 
    if(source == next) { 
     score = Double.parseDouble(scoreField.getText()); 
     if(score < 11 && score > 0) 
     { 
       totalScore += score; 
       counter ++; 
       scoreField.setText("0"); 
       commentField.setText(""); 
     } 
     else 
       JOptionPane.showMessageDialog(null, "Please enter a score bettween 1 and 10", "Error", JOptionPane.ERROR_MESSAGE);  

    } 
    else { 
     averageScore = totalScore/counter; 
     testDisplay.setText("The average score is: " + averageScore + " out of " + counter + " votes."); 
     viewLoginFrame(); 
    } 
} 

public static void main(String[] args) { 
    Testing2 testFrame = new Testing2(); 
    final int WIDTH = 375; 
    final int HEIGHT = 200; 
    testFrame.setSize(WIDTH, HEIGHT); 
    testFrame.setVisible(true); 
} 

public void viewLoginFrame() { 
    secondframe loginFrame = new secondframe(); 
    final int WIDTH = 300; 
    final int HEIGHT = 200; 
    loginFrame.setSize(WIDTH, HEIGHT); 
    loginFrame.setVisible(true); 
} 

}

package testing2; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 


public class secondframe extends Testing2 { 
    JLabel displayScore = new JLabel(); 
    JLabel displayScore2 = new JLabel(); 
    JLabel displayCommentCounter = new JLabel(); 

public secondframe() { 
    super("Survey Results"); 
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLayout(new FlowLayout()); 
    add(displayScore); 
    add(displayScore2); 
    add(displayCommentCounter); 
    displayScore.setText("The average overall saticfaction"); 
    displayScore2.setText("score for the day is: " + averageScore); 
    displayCommentCounter.setText("The total number of Surveys taken was: " + counter); 

} 

}

+0

数学の部分が機能していないとの結論にどうでしたか? – sascha10000

+0

'if(score <11 && score> 0)ブロックが入力されたが、カウンタがインクリメントされていないと言っていますか? 'コメント'とは何か定義されていますか?このコードが実行されていますか?あなたはそれをデバッグするのに疲れましたか? –

+0

結果ボタンが押された後、別のフレームに表示されるはずです。このコードでは、averageScoreとsurveyCounterの両方に0が表示されます。しかし、surveyCounter =を5と設定すると(varが宣言されている場合)、surveyCounterのために5が表示されます。 –

答えて

2

あなたの問題は、相続の誤用によるものです。あなたはあるオブジェクトから別のオブジェクトに情報を渡すことを試みています。プログラミングの共通の問題です。解決策は、2つのオブジェクトを作成し、必要なときに情報を渡すことです。それをコンストラクタに入れます。 2番目のオブジェクトが完全にユニークで、1番目のオブジェクトとは別の2番目のオブジェクトが譲渡不可能な状態であるため、2番目のオブジェクトが最初に拡張され、2番目のオブジェクトが機能しなくなるため、これを実行しようとしています。解決策:継承の悪用を取り除く。例えば

:このコードはコード内の他の多くの問題が修正されないことを

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Testing2 extends JFrame implements ActionListener { 

    private double score; 
    protected double totalScore; 
    protected double averageScore; 
    protected int counter = 0; 
    JButton next = new JButton("Next"); 
    JButton display = new JButton("Display"); 
    JTextField scoreField = new JTextField("0", 3); 
    JTextField commentField = new JTextField(30); 
    JLabel explainationLabel = new JLabel(
      "Please Score your overall     saticfaction with our app"); 
    JLabel explainationLabel2 = new JLabel("Enter any comments or suggestions"); 
    JLabel testDisplay = new JLabel(); 

    public Testing2() { 
     super("App Survey"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new FlowLayout()); 
     add(explainationLabel); 
     add(scoreField); 
     add(explainationLabel2); 
     add(commentField); 
     add(next); 
     add(display); 
     add(testDisplay); 
     next.addActionListener(this); 
     display.addActionListener(this); 
    } 

    public Testing2(String t) { 
     super(t); 
    } 

    public void actionPerformed(ActionEvent e) { 

     Object source = e.getSource(); 
     if (source == next) { 
      score = Double.parseDouble(scoreField.getText()); 
      if (score < 11 && score > 0) { 
       totalScore += score; 
       counter++; 
       scoreField.setText("0"); 
       commentField.setText(""); 
      } else 
       JOptionPane.showMessageDialog(null, "Please enter a score bettween 1 and 10", 
         "Error", JOptionPane.ERROR_MESSAGE); 

     } else { 
      averageScore = totalScore/counter; 
      testDisplay.setText("The average score is: " + averageScore + " out of " + counter 
        + " votes."); 
      viewLoginFrame(); 
     } 
    } 

    public static void main(String[] args) { 
     Testing2 testFrame = new Testing2(); 
     final int WIDTH = 375; 
     final int HEIGHT = 200; 
     testFrame.setSize(WIDTH, HEIGHT); 
     testFrame.setVisible(true); 
    } 

    public void viewLoginFrame() { 
     // !! SecondFrame2 loginFrame = new SecondFrame2(); 
     SecondFrame2 loginFrame = new SecondFrame2(this); // !! 
     final int WIDTH = 300; 
     final int HEIGHT = 200; 
     loginFrame.setSize(WIDTH, HEIGHT); 
     loginFrame.setVisible(true); 
    } 

    public double getAverageScore() { 
     return averageScore; 
    } 

    public int getCounter() { 
     return counter; 
    } 
} 

class SecondFrame2 extends JFrame { 
    JLabel displayScore = new JLabel(); 
    JLabel displayScore2 = new JLabel(); 
    JLabel displayCommentCounter = new JLabel(); 
    private Testing2 testing2; // set up field if we need it 

    // !! accept a Testing2 object in parameter 
    public SecondFrame2(Testing2 testing2) { 
     super("Survey Results"); 
     this.testing2 = testing2; // use the parameter to set the reference 
     // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new FlowLayout()); 
     add(displayScore); 
     add(displayScore2); 
     add(displayCommentCounter); 
     displayScore.setText("The average overall saticfaction"); 

     // !! extract the information we need by calling methods on testing2: 

     // displayScore2.setText("score for the day is: " + averageScore); 
     displayScore2.setText("score for the day is: " + testing2.getAverageScore()); 
     // displayCommentCounter.setText("The total number of Surveys taken was: " + counter); 
     displayCommentCounter.setText("The total number of Surveys taken was: " + testing2.getCounter()); 

    } 
} 

注意。

+0

ありがとう、私は完全にそれを逃した。 –

+0

@AllenH:上記のコードの変更例 –

関連する問題