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);
}
}
数学の部分が機能していないとの結論にどうでしたか? – sascha10000
'if(score <11 && score> 0)ブロックが入力されたが、カウンタがインクリメントされていないと言っていますか? 'コメント'とは何か定義されていますか?このコードが実行されていますか?あなたはそれをデバッグするのに疲れましたか? –
結果ボタンが押された後、別のフレームに表示されるはずです。このコードでは、averageScoreとsurveyCounterの両方に0が表示されます。しかし、surveyCounter =を5と設定すると(varが宣言されている場合)、surveyCounterのために5が表示されます。 –