Java:Action Handler

时间:2015-06-17 14:34:08

标签: java button event-handling

我正在为学校解决问题,我遇到了CalculateButtonHandler的问题。我也在使用ExitButtonHandler,但这并没有给我任何问题。我曾尝试重新阅读我的教科书,并在网上搜索过。在我解决了这个问题之后,我无法弄清楚为什么这不起作用。这是我的第一次GUI尝试,我相信我将不得不搞乱程序,以我想要的方式。我只想知道如何修复这个CalculateButtonHandler问题,因为如果需要我可以处理其余的问题。以下是该项目的代码。

问题: 第38,76和77行:CalculateButtonHandler无法解析为某种类型。

这是什么意思,我该如何解决它?

//This program calculates the weighted average of four test scores.

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

public class SNHU6_4 extends JFrame
{

private static final int WIDTH = 400;
private static final int LENGTH = 300;

private JLabel testscore1L;
private JLabel weight1L;
private JLabel testscore2L;
private JLabel weight2L;
private JLabel testscore3L;
private JLabel weight3L;
private JLabel testscore4L;
private JLabel weight4L;
private JLabel scoreL;

private JTextField testscore1TF;
private JTextField weight1TF;
private JTextField testscore2TF;
private JTextField weight2TF;
private JTextField testscore3TF;
private JTextField weight3TF;
private JTextField testscore4TF;
private JTextField weight4TF;
private JTextField scoreTF;

private JButton calculateB;
private JButton exitB;

private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;

public SNHU6_4()
{
    //Creating the labels
    testscore1L = new JLabel ("Enter first test score: ",  SwingConstants.RIGHT);
    testscore2L = new JLabel ("Enter second test score: ", SwingConstants.RIGHT);
    testscore3L = new JLabel ("Enter third test score: ", SwingConstants.RIGHT);
    testscore4L = new JLabel ("Enter fourth test score: ", SwingConstants.RIGHT);
    weight1L = new JLabel ("Enter first test score weight : ", SwingConstants.RIGHT);
    weight2L = new JLabel ("Enter second test score weight :", SwingConstants.RIGHT);
    weight3L = new JLabel ("Enter third test score weight :", SwingConstants.RIGHT);
    weight4L = new JLabel ("Enter fourth test score weight :",     SwingConstants.RIGHT);
    scoreL = new JLabel ("Final score: ", SwingConstants.RIGHT);

    //Creating the text fields
    testscore1TF = new JTextField ("0",5);
    testscore1TF.setHorizontalAlignment(JTextField.CENTER);
    testscore2TF = new JTextField ("0",5);
    testscore1TF.setHorizontalAlignment(JTextField.CENTER);
    testscore3TF = new JTextField ("0",5);
    testscore3TF.setHorizontalAlignment(JTextField.CENTER);
    testscore4TF = new JTextField ("0",5);
    testscore4TF.setHorizontalAlignment(JTextField.CENTER);
    weight1TF = new JTextField ("0",5);
    weight1TF.setHorizontalAlignment(JTextField.CENTER);
    weight2TF = new JTextField ("0",5);
    weight2TF.setHorizontalAlignment(JTextField.CENTER);        
    weight3TF = new JTextField ("0",5);
    weight3TF.setHorizontalAlignment(JTextField.CENTER);
    weight4TF = new JTextField ("0",5);
    weight4TF.setHorizontalAlignment(JTextField.CENTER);
    scoreTF = new JTextField ("0",5);
    scoreTF.setHorizontalAlignment(JTextField.CENTER);

    //Creating the calculate button
    calculateB = new JButton("Calculate");
    cbHandler = new CalculateButtonHandler();
    calculateB.addActionListener(cbHandler);

    //Creating the exit button
    exitB = new JButton("Exit");
    ebHandler = new ExitButtonHandler();
    exitB.addActionListener(ebHandler);

    //Creating the window title
    setTitle ("Weighted Average of Test Scores");

    //Get the container
    Container pane = getContentPane();

    //Set the layout
    pane.setLayout(new GridLayout(5, 4));

    //Placing components in the pane
    pane.add(testscore1L);
    pane.add(testscore1TF);
    pane.add(testscore2L);
    pane.add(testscore2TF);
    pane.add(testscore3L);
    pane.add(testscore3TF);
    pane.add(testscore4L);
    pane.add(testscore4TF);
    pane.add(weight1L);
    pane.add(weight1TF);
    pane.add(weight2L);
    pane.add(weight2TF);
    pane.add(weight3L);
    pane.add(weight3TF);
    pane.add(weight4L);
    pane.add(weight4TF);
    pane.add(calculateB);
    pane.add(exitB);

    //Set the window size
    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private class CalculateButtonHanlder implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        double testscore1, testscore2, testscore3, testscore4;
        double weight1, weight2, weight3, weight4;
        double average1, average2, average3, average4;
        double totalAverage;

        testscore1 = Double.parseDouble(testscore1TF.getText());
        testscore2 = Double.parseDouble(testscore2TF.getText());
        testscore3 = Double.parseDouble(testscore3TF.getText());
        testscore4 = Double.parseDouble(testscore4TF.getText());

        weight1 = Double.parseDouble(weight1TF.getText());
        weight2 = Double.parseDouble(weight2TF.getText());
        weight3 = Double.parseDouble(weight3TF.getText());
        weight4 = Double.parseDouble(weight4TF.getText());

        average1 = testscore1 * weight1;
        average2 = testscore2 * weight2;
        average3 = testscore3 * weight3;
        average4 = testscore4 * weight4;

        totalAverage = average1 + average2 + average3 + average4;

        scoreTF.setText("" + String.format("%,2f", totalAverage));
    }
}

private class ExitButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        System.exit(0);
    }
}

public static void main(String[] args)
{
    SNHU6_4 rectObject = new SNHU6_4();



}

} 

2 个答案:

答案 0 :(得分:1)

  

CalculateButtonHandler无法解析为类型

告诉你它无法通过该名称找到一个班级:

private class CalculateButtonHanlder implements ActionListener

你有一个拼写错误" Hanlder"

private class CalculateButtonHandler implements ActionListener

答案 1 :(得分:0)

当您在第120行声明该类时,您将`CalculateButtonHandler'拼写为'CalculateButtonHanlder'。修复它并且它将起作用。