【问题标题】:Java: Action HandlerJava:动作处理程序
【发布时间】:2015-06-17 14:34:08
【问题描述】:

我正在为学校解决一个问题,但我遇到了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();



}

} 

【问题讨论】:

标签: java button event-handling


【解决方案1】:

CalculateButtonHandler 无法解析为类型

告诉你它找不到那个名字的类:

private class CalculateButtonHanlder implements ActionListener

你有一个错字“汉德”

private class CalculateButtonHandler implements ActionListener

【讨论】:

  • 我想我永远不会发现那个错字。这清除了它。非常感谢。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多