好的 - 这是我第一次使用小程序。我想把这个程序转换成一个小程序。我已经修复(注释掉)了小程序不需要的项目,最后一步是将退出按钮替换为重置按钮。这里是否缺少一些明显的东西?
错误
> PropertyTax99.java:43: error: cannot find symbol
> ResetHandler rbHandler = new ResetHandler();
> ^ symbol: class ResetHandler location: class PropertyTax99 PropertyTax99.java:43: error: cannot find symbol
> ResetHandler rbHandler = new ResetHandler();
> ^ symbol: class ResetHandler location: class PropertyTax99 PropertyTax99.java:93: error: cannot
> find symbol
> reset();
> ^ symbol: method reset() location: class PropertyTax99.resetHandler 3 errors
代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JApplet;
public class PropertyTax99 extends JFrame
{
// set parameters to define extent of the window
//changed width to 500 since the words were getting cut off
private static final int WIDTH = 500, HEIGHT = 300;
//Declare and initialize 6 JLabels
JLabel assessL = new JLabel("Assessment Home Value: ", SwingConstants.RIGHT);
JLabel schoolTaxL = new JLabel("Decimal Value of School Tax Rate: ", SwingConstants.RIGHT);
JLabel countyTaxL = new JLabel("Decimal Value of County Tax Rate: ", SwingConstants.RIGHT);
JLabel totalSchoolTaxL = new JLabel("School Taxes: ", SwingConstants.RIGHT);
JLabel totalCountyTaxL = new JLabel("County Taxes: ", SwingConstants.RIGHT);
JLabel totalTaxesL = new JLabel("Total Taxes: ", SwingConstants.RIGHT);
//Declate and initialize 5 JTextFields
JTextField assessTF = new JTextField(10);
JTextField schoolrateTF = new JTextField(10);
JTextField countyrateTF = new JTextField(10);
JTextField schooltaxTF = new JTextField(10);
JTextField countytaxTF = new JTextField(10);
JTextField totaltaxTF = new JTextField(10);
//Declare and initialize reset button
JButton reset = new JButton("Reset");
ResetHandler rbHandler = new ResetHandler();
//Declare and initialize Calculate button
JButton calculate = new JButton("Calculate");
CalculateHandler cbHandler = new CalculateHandler();
public PropertyTax99()
{
//Declare and initialize a container
Container pane = getContentPane();
//Set the container layout
pane.setLayout(new GridLayout(7,2));
//Set GUI objects in the container
pane.add(assessL);
pane.add(assessTF);
pane.add(schoolTaxL);
pane.add(schoolrateTF);
pane.add(countyTaxL);
pane.add(countyrateTF);
pane.add(totalSchoolTaxL);
pane.add(schooltaxTF);
pane.add(totalCountyTaxL);
pane.add(countytaxTF);
pane.add(totalTaxesL);
pane.add(totaltaxTF);
pane.add(reset);
pane.add(calculate);
// set title, size and visibility aspects of window
//setTitle("Calculation of Property Taxes");
//setSize(WIDTH, HEIGHT);
//setVisible(true);
//setDefaultCloseOperation(EXIT_ON_CLOSE);
//reset Button
reset.addActionListener(rbHandler);
//Calculate Button
calculate.addActionListener(cbHandler);
}
//Handler for reset
public class resetHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
reset();
}
}
//Handler for calculate
public class CalculateHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double countyRate, schoolRate, assessment, schoolTax, countyTax, totalTax;
assessment = Double.parseDouble(assessTF.getText());
schoolRate = Double.parseDouble(schoolrateTF.getText());
countyRate = Double.parseDouble(countyrateTF.getText());
schoolTax = assessment * schoolRate * .01;
countyTax = assessment * countyRate * .01;
totalTax = schoolTax + countyTax;
schooltaxTF.setText(""+ String.format("%.2f", schoolTax));
countytaxTF.setText(""+ String.format("%.2f", countyTax));
totaltaxTF.setText(""+ String.format("%.2f", totalTax));
}
}
public void init()
{
// main program to invoke constructor
PropertyTax99 proptax = new PropertyTax99();
}
}
最佳答案
你有两个基本问题......
一个
您使用...声明您的rbHandler
ResetHandler rbHandler = new ResetHandler();
但是您的 ResetHandler
实际上声明为
public class resetHandler implements ActionListener {
不是名称上的差异。将类声明更改为 public class ResetHandler Implements ActionListener {
两个
代码中的任何位置都没有声明 reset
方法。
仅供引用:小程序不应该创建框架。如果您确实想让程序可移植,请将核心 UI 移至 JPanel
,然后根据需要将其添加到 JFrame
或 JApplet
关于java - 将退出按钮转换为重置按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23645530/