如何在java中向我的GUI计算器添加操作顺序?它已经可以使用任何运算符组合执行多个表达式,但不会按正确的顺序解决它们。 (它从左到右解决了这些问题)
更新:6/29结果在另一个问题中回答了这个问题。感谢所有帮助过的人!
代码在
之下import javax.swing.JFrame;
//import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main {
private static JTextField textField;
public static void main(String[] args) {
JFrame frame1 = new JFrame();
frame1.setVisible(true);
frame1.setSize(261, 409);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setTitle("Calculator");
frame1.setResizable(false);
frame1.getContentPane().setLayout(null);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setBounds(17, 9, 214, 34);
frame1.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("1");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + 1);
}
});
btnNewButton.setBounds(32, 56, 53, 43);
frame1.getContentPane().add(btnNewButton);
JButton button = new JButton("3");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e2) {
textField.setText(textField.getText() + 3);
}
});
button.setBounds(162, 56, 53, 43);
frame1.getContentPane().add(button);
JButton button_1 = new JButton("2");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1) {
textField.setText(textField.getText() + 2);
}
});
button_1.setBounds(97, 56, 53, 43);
frame1.getContentPane().add(button_1);
JButton button_2 = new JButton("4");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e3) {
textField.setText(textField.getText() + 4);
}
});
button_2.setBounds(32, 112, 53, 43);
frame1.getContentPane().add(button_2);
JButton button_3 = new JButton("7");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e6) {
textField.setText(textField.getText() + 7);
}
});
button_3.setBounds(32, 168, 53, 43);
frame1.getContentPane().add(button_3);
JButton button_4 = new JButton("6");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e5) {
textField.setText(textField.getText() + 6);
}
});
button_4.setBounds(162, 112, 53, 43);
frame1.getContentPane().add(button_4);
JButton button_5 = new JButton("9");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e8) {
textField.setText(textField.getText() + 9);
}
});
button_5.setBounds(162, 168, 53, 43);
frame1.getContentPane().add(button_5);
JButton button_6 = new JButton("5");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e4) {
textField.setText(textField.getText() + 5);
}
});
button_6.setBounds(97, 112, 53, 43);
frame1.getContentPane().add(button_6);
JButton button_7 = new JButton("8");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e7) {
textField.setText(textField.getText() + 8);
}
});
button_7.setBounds(97, 168, 53, 43);
frame1.getContentPane().add(button_7);
JButton btnClear = new JButton("0");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e10) {
textField.setText(textField.getText() + 0);
}
});
btnClear.setBounds(97, 224, 53, 43);
frame1.getContentPane().add(btnClear);
JButton btnClear_1 = new JButton("C");
btnClear_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e9) {
textField.setText("");
}
});
btnClear_1.setBounds(32, 224, 53, 43);
frame1.getContentPane().add(btnClear_1);
JButton btnEnter = new JButton("=");
btnEnter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e11) {
try {
int size = 0;
String string = textField.getText();
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == '+') {
size++;
}
if (string.charAt(i) == '-') {
size++;
}
if (string.charAt(i) == '*') {
size++;
}
if (string.charAt(i) == '/') {
size++;
}
}
/* what operators are there */char[] listChar = new char[size];
/* what is the index of those operators */int[] addchar = new int[size];
/* what are the number to be solved */int[] nums = new int[size + 1];
int count = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == '+') {
addchar[count] = i;
listChar[count] = '+';
count++;
}
if (string.charAt(i) == '-') {
addchar[count] = i;
listChar[count] = '-';
count++;
}
if (string.charAt(i) == '*') {
addchar[count] = i;
listChar[count] = '*';
count++;
}
if (string.charAt(i) == '/') {
addchar[count] = i;
listChar[count] = '/';
count++;
}
}
for (int i = 0; i < size; i++) {
String sub1 = string.substring(0, addchar[i]);
nums[i] = Integer.parseInt(sub1);
string = string.substring(addchar[i] + 1,
string.length());
for (int j = size - 1; j >= 0; j--) {
addchar[j] -= addchar[i] + 1;
}
}
nums[nums.length - 1] = Integer.parseInt(string);
int total = 0;
for (int i = 0; i < size - 1; i++) {
if (listChar[i] == '+') {
total = nums[0] + nums[1];
}
if (listChar[i] == '-') {
total = nums[0] - nums[1];
}
if (listChar[i] == '*') {
total = nums[0] * nums[1];
}
if (listChar[i] == '/') {
total = nums[0] / nums[1];
}
for (int j = 0; j < nums.length - 1; j++) {
nums[j] = nums[j + 1];
}
nums[0] = total;
}
if (listChar[listChar.length - 1] == '+') {
nums[0] += nums[1];
}
if (listChar[listChar.length - 1] == '-') {
nums[0] -= nums[1];
}
if (listChar[listChar.length - 1] == '*') {
nums[0] *= nums[1];
}
if (listChar[listChar.length - 1] == '/') {
nums[0] /= nums[1];
}
textField.setText(((Integer) nums[0]).toString());
} catch (Exception e) {
/*
* e.printStackTrace(); JOptionPane.showMessageDialog(null,
* "please enter correct statements");
*/
}
}
});
btnEnter.setBounds(162, 224, 53, 43);
frame1.getContentPane().add(btnEnter);
JButton btnNewButton_1 = new JButton("+");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e12) {
textField.setText(textField.getText() + "+");
}
});
btnNewButton_1.setBounds(32, 280, 70, 34);
frame1.getContentPane().add(btnNewButton_1);
JButton button_8 = new JButton("-");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e13) {
textField.setText(textField.getText() + "-");
}
});
button_8.setBounds(145, 280, 70, 34);
frame1.getContentPane().add(button_8);
JButton button_9 = new JButton("/");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e15) {
textField.setText(textField.getText() + "/");
}
});
button_9.setBounds(145, 327, 70, 34);
frame1.getContentPane().add(button_9);
JButton button_10 = new JButton("*");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e14) {
textField.setText(textField.getText() + "*");
}
});
button_10.setBounds(32, 327, 70, 34);
frame1.getContentPane().add(button_10);
}
}