Java var在新帧上显示时返回0

时间:2016-08-05 00:43:27

标签: java swing events awt

好的,所以它不是动作方法中的问题,而是当它显示在新帧上时。这是新代码,显示计数和平均分数两次,一次在第一帧(正确值),然后在第二帧(由于未知原因设置回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);

}

}

1 个答案:

答案 0 :(得分:2)

您的问题是由于滥用继承造成的。您正在尝试将信息从一个对象传递到另一个对象,这是编程中的常见问题,解决方案就是这样做,创建两个对象,然后通过调用setter方法或传递在需要的地方和时间传递信息它成为一个构造函数。你试图通过让第二个对象扩展第一个对象来做到这一点,这是行不通的,因为第二个对象是完全唯一的,并且与第一个对象分开,具有自己的不可转移状态。解决方案:摆脱滥用继承。

例如:

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());

    }
}

请注意,此代码不能解决代码中的许多其他问题。