使Calc按钮在GUI中工作

时间:2015-12-08 23:41:03

标签: java user-interface

我正在为学校做一个项目,无法获得用户输入以适应我目前的公式。我收到一条错误消息,上面写着"错误:不兼容的类型"。我需要将用户输入并将其放入公式中以最终获得总成本。

import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class Program11 extends JFrame{

final private int MAX_WIDTH = 550;
final private int MAX_HEIGHT = 400;
final double TAX_RATE = 0.07; //Sales Tax

private JLabel lblItem1;
private JLabel lblItem2;
private JLabel lblItem3;
private JTextField output;

private JTextField txtItem1;
private JTextField txtItem2;
private JTextField txtItem3;

private JButton calc;
private JButton clear;
private JButton exit;

private JPanel panel;


public Program11() {
    setTitle("CCAC Dollar Store");
    setSize(MAX_WIDTH, MAX_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildPanel();
    setVisible(true);
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Program11 ms = new Program11();
}

private void buildPanel(){
    lblItem1 = new JLabel("Pencils");
    lblItem2 = new JLabel("Pens");
    lblItem3 = new JLabel("Markers");

    txtItem1 = new JTextField(10);
    txtItem2 = new JTextField(10);
    txtItem3 = new JTextField(10);

    calc = new JButton("Calculate");
    clear = new JButton("Clear");
    exit = new JButton("Exit");
    calc.addActionListener(new CalcButtonListener());
    clear.addActionListener(new ClearButtonListener());
    exit.addActionListener(new ExitButtonListener());

    output = new JTextField(30);
    output.setEditable(false);

    panel = new JPanel();
    panel.add(lblItem1);
    panel.add(txtItem1);
    panel.add(lblItem2);
    panel.add(txtItem2);
    panel.add(lblItem3);
    panel.add(txtItem3);
    panel.add(calc);
    panel.add(clear);
    panel.add(exit);
    panel.add(output);

    add(panel);
}

private class CalcButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        double subtotal, total, tax;
        string input1, input2, input3;
            input1 = txtItem1.getText();
        input2 = txtItem2.getText();
        input3 = txtItem3.getText(); 

        subtotal = input1 + input2 + input3;    

        tax = subtotal * TAX_RATE;

        total = subtotal + tax;

        output.setText("Your total is:  " + total);
    }
 }


private class ClearButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        txtItem1.setText("");
        txtItem2.setText("");
        txtItem3.setText("");
        output.setText("");
    }
 }

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

}

1 个答案:

答案 0 :(得分:0)

您可以使用Double.parseDouble(jTextField.getText()将字符串转换为double。 请参阅下面的修改代码,(修改过的区域 - 类CalcButtonListener)

private class CalcButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        double subtotal, total, tax;
        double input1, input2, input3;

        /* assign the text field inputs to the variables */
        input1 = Double.parseDouble(txtItem1.getText());                
        input2 = Double.parseDouble(txtItem2.getText());
        input3 = Double.parseDouble(txtItem3.getText());

        subtotal = input1 + input2 + input3;                                // this is what you need don't you?

        tax = subtotal * TAX_RATE;

        total = subtotal + tax;

        output.setText("Your total is:  " + total);
    }
}