我正在尝试制作一个Infix到Postfix GUI计算器。我需要将正常的计算器按钮添加到计算器中,我希望有" PI"保存pi值的按钮。当动作被调用时,我想要" Pi"出现在input.setText字段中。但是,当我尝试这样做时,我遇到了将double转换为textField接受的类型的问题。
import acm.program.*;
import acm.graphics.*; // GCanvas and G-friends
import javax.swing.*; // JButton and J-friends
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;
public class Calc extends Program
{
JTextField input;
JTextField output;
JTextField result;
public Calc()
{
start();
setSize(500, 500);
}
public void init()
{
GCanvas canvas = new GCanvas();
add(canvas);
canvas.setBackground(Color.lightGray);
JButton conversionButton = new JButton(" DEG ");
canvas.add(conversionButton, 85, 60);
JButton conversionButton2 = new JButton("RAD");
canvas.add(conversionButton2, 170, 60);
conversionButton2.setSize(conversionButton.getSize());
JButton goButton = new JButton("Go!");
canvas.add(goButton, 280, 350);
goButton.setBackground(Color.green);
goButton.setSize(conversionButton.getSize());
JButton clearButton = new JButton("Clear");
canvas.add(clearButton, 380, 350);
clearButton.setBackground(Color.red);
clearButton.setSize(conversionButton.getSize());
JButton graphButton = new JButton("Graph");
canvas.add(graphButton, 0, 120);
graphButton.setSize(conversionButton.getSize());
JButton factorialButton = new JButton("!");
canvas.add(factorialButton, 85, 120);
factorialButton.setSize(conversionButton.getSize());
JButton ANSButton = new JButton("ANS");
canvas.add(ANSButton, 0, 90);
ANSButton.setSize(conversionButton.getSize());
JButton LnButton = new JButton("Ln");
canvas.add(LnButton, 85, 90);
LnButton.setSize(conversionButton.getSize());
JButton AbsButton = new JButton("| |");
canvas.add(AbsButton, 170, 90);
AbsButton.setSize(conversionButton.getSize());
JButton PIButton = new JButton("PI");
canvas.add(PIButton, 170, 120);
PIButton.setSize(conversionButton.getSize());
JLabel infixLabel = new JLabel("Infix");
canvas.add(infixLabel, 275, 40);
JLabel postfixLabel = new JLabel("Postfix");
canvas.add(postfixLabel, 275, 160);
JLabel resultLabel = new JLabel("Result");
canvas.add(resultLabel, 275, 280);
JLabel messageLabel = new JLabel("Note: this calculator
can only graph functions of the 3rd degree or lower");
canvas.add(messageLabel, 25, 10);
input = new JTextField();
canvas.add(input, 275, 60);
input.setSize(200, 30);
output = new JTextField();
canvas.add(output, 275, 180);
output.setSize(200, 30);
result = new JTextField();
canvas.add(result, 275, 300);
result.setSize(200, 30);
addActionListeners();
}
public void actionPerformed(ActionEvent ae)
{
String action = ae.getActionCommand();
if (action.equals("Clear"))
{
input.setText("");
output.setText("");
result.setText("");
}
else if (action.equals("Go!"))
{
// Get input from user
String c = input.getText();
result.setText(input.getText());
}
if (action.equals("PI"))
{
double Pi = 3.145926;
Double.toString(Pi);
input.setText(String.Pi);
}
}
}