我需要一些帮助,使计算器按钮能够输入多位数字。 谢谢。它已接近完成,适用于学校项目。我一直无法找到问题的答案。如果有人知道我可以添加什么来使用按钮在文本字段中允许多位数字,那将非常感激。
//These are the imports that are used in the code
import java.awt.Button;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
public class calculatorfinal implements ActionListener{
JFrame guiFrame;
JPanel buttonPanel;
JTextField tf1;
int calcOperation = 0;
int currentCalc;
int p = 0;
public static void main(String[] args) {
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new calculatorfinal();
}
});
}
public calculatorfinal()
{
guiFrame = new JFrame();
//This exits the program when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("calculator");
guiFrame.setSize(300,350);
//This will centre the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
//This sets up tf1 (text field 1), it allows it to be editable aligns the text to the right
tf1 = new JTextField();
tf1.setHorizontalAlignment(JTextField.RIGHT);
tf1.setEditable(true);
guiFrame.add(tf1, BorderLayout.NORTH);
buttonPanel = new JPanel();
//Grid that has five rows and three columns
buttonPanel.setLayout(new GridLayout(6,3));
guiFrame.add(buttonPanel, BorderLayout.CENTER);
//Adding the number buttons
for (int i=1;i<10;i++)
{
addButton(buttonPanel, String.valueOf(tf1.getText()+i));
}
{
addButton(buttonPanel, String.valueOf(tf1.getText()+p));
}
JButton buttonDecimal = new JButton(".");
buttonDecimal.addActionListener(this);
buttonPanel.add(buttonDecimal);
JButton buttonPlus = new JButton("+");
buttonPlus.setFont(new Font("Verdana", Font.BOLD, 20));
buttonPlus.setActionCommand("+");
OperatorAction subAction = new OperatorAction(1);
buttonPlus.addActionListener(subAction);
JButton buttonMinus = new JButton("-");
buttonMinus.setFont(new Font("Verdana", Font.BOLD, 20));
buttonMinus.setActionCommand("-");
OperatorAction addAction = new OperatorAction(2);
buttonMinus.addActionListener(addAction);
JButton buttonTimes = new JButton("*");
buttonTimes.setFont(new Font("Verdana", Font.BOLD, 20));
buttonTimes.setActionCommand("*");
OperatorAction multiplyAction = new OperatorAction(3);
buttonTimes.addActionListener(multiplyAction);
JButton buttonDivide = new JButton("/");
buttonDivide.setFont(new Font("Verdana", Font.BOLD, 20));
buttonDivide.setActionCommand("/");
OperatorAction ActionDivide = new OperatorAction(4);
buttonDivide.addActionListener(ActionDivide);
JButton buttonClear = new JButton("Clear");
buttonClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf1.setText("");
//textfield.setText(null);
}
});
JButton buttonEquals = new JButton("=");
buttonEquals.setFont(new Font("Verdana", Font.BOLD, 20));
buttonEquals.setActionCommand("=");
buttonEquals.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
if (!tf1.getText().isEmpty())
{
double number = Double.parseDouble(tf1.getText());
if (calcOperation == 1)
{
double calculate = currentCalc + number;
tf1.setText(Double.toString(calculate));
}
else if (calcOperation == 2)
{
double calculate = currentCalc - number;
tf1.setText(Double.toString(calculate));
}
else if (calcOperation == 3)
{
double calculate = currentCalc * number;
tf1.setText(Double.toString(calculate));
}
else if (calcOperation == 4)
{
double calculate = currentCalc / number;
tf1.setText(Double.toString(calculate));
}
}}});
buttonPanel.add(buttonPlus);
buttonPanel.add(buttonMinus);
buttonPanel.add(buttonEquals);
buttonPanel.add(buttonTimes);
buttonPanel.add(buttonDivide);
buttonPanel.add(buttonClear);
guiFrame.setVisible(true);
}
//All the buttons are following the same pattern
//so create them all in one place.
private void addButton(Container parent, String name)
{
JButton but = new JButton(name);
but.setActionCommand(name);
but.addActionListener(this);
parent.add(but);
}
//As all the buttons are doing the same thing it's
//easier to make the class implement the ActionListener
//interface and control the button clicks from one place
@Override
public void actionPerformed(ActionEvent event)
{
//get the Action Command text from the button
String action = event.getActionCommand();
//set the text using the Action Command text
tf1.setText(action);
}
private class OperatorAction implements ActionListener
{
private int operator;
public OperatorAction(int operation)
{
operator = operation;
}
public void actionPerformed(ActionEvent event)
{
currentCalc = Integer.parseInt(tf1.getText());
calcOperation = operator;
}
}
}
答案 0 :(得分:0)
您要将值设置为操作,而不是之前的值和操作
tf1.setText(action)
应该是
tf1.setText(tf1.getText() + action)