packagecom.bj.jsq;importjava.awt.EventQueue;importjavax.swing.JFrame;importjavax.swing.JPanel;importjava.awt.Dimension;importjava.awt.TextField;importjava.awt.Label;importjava.awt.Button;importjava.awt.Toolkit;importjava.awt.event.ActionListener;importjava.awt.event.ActionEvent;importjava.awt.Color;importjava.awt.SystemColor;importjavax.swing.JMenuBar;importjavax.swing.JMenu;importjavax.swing.JMenuItem;importjavax.swing.JOptionPane;public class calculator extendsJFrame {private static final long serialVersionUID = 1L;privateJPanel contentPane;public static doublen;public static voidmain(String[] args) {
EventQueue.invokeLater(newRunnable() {public voidrun() {try{
calculator frame= newcalculator();
frame.setVisible(true);
}catch(Exception e) {
e.printStackTrace();
}
}
});
}/*** Create the frame.*/
publiccalculator() {
setResizable(false);
setBackground(SystemColor.inactiveCaptionBorder);// setTitle("\u7B80\u5355\u8BA1\u7B97\u5668");
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();int x=(int)screen.getWidth();int y=(int)screen.getHeight();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds((x-293)/2, (y-333)/2, 293, 315);
contentPane= newJPanel();
setContentPane(contentPane);
contentPane.setLayout(null);final TextField txtInput = newTextField();
txtInput.setBounds(62, 10, 205, 23);
contentPane.add(txtInput);final TextField txtOutput = newTextField();
txtOutput.setEditable(false);
txtOutput.setBounds(62, 49, 205, 23);
contentPane.add(txtOutput);
Label label= new Label("input"); //
//label.setBackground(UIManager.getColor("window"));
label.setForeground(new Color(153, 50, 204));
label.setBounds(15, 10, 41, 23);
contentPane.add(label);
Label label_1= new Label("result"); // label_1.setForeground(new Color(153, 50, 204));
label_1.setBounds(15, 49, 41, 23);
contentPane.add(label_1);
Button btn7= new Button("7"); // btn7.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+"7");
}
});
btn7.setBounds(20, 92, 49, 23);
contentPane.add(btn7);
Button btn8= new Button("8"); btn8.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+"8");
}
});
btn8.setBounds(79, 92, 49, 23);
contentPane.add(btn8);
Button button_2= new Button("+");
button_2.setForeground(new Color(153, 50, 204));
button_2.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+" + ");
}
});
button_2.setBounds(218, 92, 49, 23);
contentPane.add(button_2);
Button btn9= new Button("9");
btn9.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+"9");
}
});
btn9.setBounds(147, 92, 49, 23);
contentPane.add(btn9);
Button btn5= new Button("5");
btn5.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+"5");
}
});
btn5.setBounds(79, 136, 49, 23);
contentPane.add(btn5);
Button btn4= new Button("4");
btn4.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+"4");
}
});
btn4.setBounds(20, 136, 49, 23);
contentPane.add(btn4);
Button button_6= new Button("-");
button_6.setForeground(new Color(153, 50, 204));
button_6.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+" - ");
}
});
button_6.setBounds(218, 136, 49, 23);
contentPane.add(button_6);
Button btn6= new Button("6");
btn6.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+"6");
}
});
btn6.setBounds(147, 136, 49, 23);
contentPane.add(btn6);
Button btn2= new Button("2");
btn2.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+"2");
}
});
btn2.setBounds(79, 183, 49, 23);
contentPane.add(btn2);
Button btn1= new Button("1");
btn1.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+"1");
}
});
btn1.setBounds(20, 183, 49, 23);
contentPane.add(btn1);
Button button_10= new Button("cls");
button_10.setForeground(new Color(153, 50, 204));
button_10.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText("");
txtOutput.setText("");
}
});
button_10.setBounds(218, 183, 49, 23);
contentPane.add(button_10);
Button btn3= new Button("3");
btn3.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+"3");
}
});
btn3.setBounds(147, 183, 49, 23);
contentPane.add(btn3);
Button btndel= new Button(".");
btndel.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+".");
}
});
btndel.setBounds(79, 230, 49, 23);
contentPane.add(btndel);
Button btn0= new Button("0");
btn0.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
txtInput.setText(txtInput.getText()+"0");
}
});
btn0.setBounds(20, 230, 49, 23);
contentPane.add(btn0);
Button btne= new Button("="); // btne.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
String str=txtInput.getText();
String[]ss;
String regex1="( \\p{Punct} )\\1++";//有若干个标点符号重叠的正则式
String regex2="( \\p{Punct} )$";//标点符号在行末的正则表达式
str=str.replaceAll(regex1, "$1");
str=str.replaceAll(regex2, "");inti;
ss=str.split(" ");try{
n=Double.parseDouble(ss[0]);for(i=0;i
{if(ss[i].equals("+"))
{
n=n+Double.parseDouble(ss[i+1]);
}if(ss[i].equals("-"))
{
n=n-Double.parseDouble(ss[i+1]);
}
}
txtOutput.setText(String.valueOf(n));
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "请检查 输入数的格式!!重新输入");
}
}
});
btne.setForeground(new Color(153, 50, 204));
btne.setBounds(147, 230, 49, 23);
contentPane.add(btne);
Button btnSqrt= new Button("sqrt");
btnSqrt.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
String sqrtValue;if(txtInput.getText().contains("+")||txtInput.getText().contains("+"))
sqrtValue=txtOutput.getText();elsesqrtValue=txtInput.getText();try{
txtOutput.setText(String.valueOf(Math.sqrt(Double.parseDouble(sqrtValue))));
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "该数不能开方,请检查数据是否错误!");
}
}
});
btnSqrt.setForeground(new Color(148, 0, 211));
btnSqrt.setBounds(218, 230, 49, 23);
contentPane.add(btnSqrt);
JMenuBar menuBar= newJMenuBar();
menuBar.setBounds(0,267,281, 21);
contentPane.add(menuBar);
JMenu mnNewMenu= new JMenu("帮助");//\u67E5\u770B
menuBar.add(mnNewMenu);
JMenuItem mntmh= new JMenuItem("帮助");//\u5E2E\u52A9
mntmh.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
calHelp ch=newcalHelp();//ch.show(false);
ch.setVisible(true);
}
});
mnNewMenu.add(mntmh);
JMenuItem mntmNewMenuItem= new JMenuItem("关于计算器");
mntmNewMenuItem.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
calAbout ca=newcalAbout();
ca.setVisible(true);
}
});
mnNewMenu.add(mntmNewMenuItem);
setVisible(true);
}
}