新手,计算器的问题,请大神指点。
程序代码:
package Study; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.WindowConstants; /** * 简单计算器窗口 * * @author Sky。 2013-10-18 */ @SuppressWarnings("serial") public class ChuangKou extends JFrame implements ActionListener { static JTextField field = new JTextField();// 文本框 static String suanShi; public static void main(String[] args) { new ChuangKou();// 实例化 } /** * 窗口属性设置 */ public ChuangKou() { super("计算器"); this.setSize(220, 250);// 窗口大小 Container c=getContentPane(); JPanel panel = new JPanel();// 面板 panel. setLayout(new GridLayout(0,4, 5, 5));//流布局 String[] anNiu = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "×", ".", "0", "=", "÷","(",")","←","c" };// 按钮名字的组 // 添加按钮到面板 for (int i = 0; i < anNiu.length; i++) { JButton button = new JButton(anNiu[i]); button.addActionListener(this);// 为每个按钮注册监听器 panel.add(button); } c.add(panel);// 添加面板到 c.add(field, "North");// 添加文本框到窗口北部 setResizable(false);// 固定窗口 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// 点击关闭窗口结束程序 this.setVisible(true);// 窗口可见 } /*处理鼠标单击按钮的事件 * (non-Javadoc) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //处理单机数字按钮时的事件 if (e.getActionCommand().equals("1")) { field.setText(field.getText() + "1"); } if (e.getActionCommand().equals("2")) { field.setText(field.getText() + "2"); } if (e.getActionCommand().equals("3")) { field.setText(field.getText() + "3"); } if (e.getActionCommand().equals("4")) { field.setText(field.getText() + "4"); } if (e.getActionCommand().equals("5")) { field.setText(field.getText() + "5"); } if (e.getActionCommand().equals("6")) { field.setText(field.getText() + "6"); } if (e.getActionCommand().equals("7")) { field.setText(field.getText() + "7"); } if (e.getActionCommand().equals("8")) { field.setText(field.getText() + "8"); } if (e.getActionCommand().equals("9")) { field.setText(field.getText() + "9"); } if (e.getActionCommand().equals(".")) { field.setText(field.getText()+"."); } if (e.getActionCommand().equals("0")) { field.setText(field.getText() + "0"); } //处理单击加减乘除按钮的事件 if (e.getActionCommand().equals("+")) { field.setText(field.getText()+"+"); } if (e.getActionCommand().equals("-")) { field.setText(field.getText()+"-"); } if (e.getActionCommand().equals("×")) { field.setText(field.getText()+"×"); } if (e.getActionCommand().equals("÷")) { field.setText(field.getText()+"÷"); } if (e.getActionCommand().equals("(")) { field.setText(field.getText() + "("); } if (e.getActionCommand().equals(")")) { field.setText(field.getText() + ")"); } //处理单击退格按钮的事件 if (e.getActionCommand().equals("←")) { StringBuffer buffer=new StringBuffer(field.getText());//获取当前文本框的内容 buffer=buffer.deleteCharAt(buffer.length()-1);//去掉最后一位字符实现退格 String aString=buffer.toString(); //将StringBuffer转为String型 field.setText(aString); } //处理单击等于按钮的事件 if (e.getActionCommand().equals("=")) { suanShi=field.getText(); } //处理清除按钮的事件 if (e.getActionCommand().equals("c")) { field.setText(""); } } }
程序代码:
package Study; /** * 计算器算法 * * @author Sky。 2013-10-19 */ public class SuanFa { static String string = ChuangKou.suanShi;// 文本框中所输入的算式 static void result() { } }我想在这个类里面实现加减乘除括号优先级运算。想了很久都没头绪。。请大神指点指点。。