2016-08-05 76 views
1

OK, es ist also kein Problem innerhalb der Aktionsmethode, aber wenn es auf dem neuen Frame angezeigt wird. Hier ist der neue Code, der die Anzahl und die durchschnittliche Punktzahl zweimal anzeigt, einmal auf dem ersten Bild (die korrekten Werte) und dann auf dem zweiten (es wurde aus einem unbekannten Grund auf 0 zurückgesetzt).Java var kehrt zurück zu 0, wenn es in einem neuen Frame angezeigt wird

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

Wie sind Sie zu dem Schluss gekommen, dass der Matheteil nicht funktioniert? – sascha10000

+0

Willst du sagen, dass der Block 'if (score <11 && score> 0)' eingegeben wurde, aber der Zähler nicht inkrementiert wird? Was ist "Kommentare" wo ist es definiert? Sind Sie sicher, dass dieser Code ausgeführt wird? Hast du es satt, es zu debuggen? –

+0

Es sollte nach dem Drücken der Ergebnistaste in einem anderen Rahmen angezeigt werden. Mit diesem Code wird 0 sowohl für durationScore als auch für surveyCounter angezeigt. Aber wenn ich surveyCounter = auf 5 setze (wenn die Variable deklariert ist), wird 5 angezeigt für surveyCounter –

Antwort

2

Ihr Problem ist, aufgrund eines Missbrauchs der Vererbung. Sie versuchen, Informationen von einem Objekt an ein anderes zu übergeben, ein häufiges Problem beim Programmieren, und die Lösung besteht darin, genau diese beiden Objekte zu erstellen und die Informationen dann an Ort und Stelle entweder durch Aufruf einer Setter-Methode oder durch Übergabe weiterzuleiten es in einen Konstruktor. Sie versuchen, dies zu tun, indem das 2. Objekt das erste erweitert, und das wird nicht funktionieren, weil das 2. Objekt vollständig einzigartig und von dem ersten mit seinem eigenen nicht übertragbaren Zustand getrennt ist. Lösung: Den Missbrauch der Vererbung loswerden.

Zum Beispiel:

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

    } 
} 

Beachten Sie, dass dieser Code nicht viele andere Probleme in Ihrem Code nicht korrigiert.

+0

Danke, das habe ich komplett vermisst. –

+0

@AllenH: siehe Code oben –