2017-07-10(简易计算器)
import java.awt.BorderLayout;
public class Testgui extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
String f="123458";
String r = JOptionPane.showInputDialog("Please input a value");
if(f.equals(r)){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Testgui frame = new Testgui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
else{
JOptionPane.showMessageDialog(null, "密码输入错误", "alert", JOptionPane.ERROR_MESSAGE);
}
}
/**
* Create the frame.
*/
public Testgui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 497, 452);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JButton btnNewButton = new JButton("=");
btnNewButton.setBackground(Color.GREEN);
btnNewButton.setBounds(399, 339, 69, 68);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String str=textField.getText();
byte[] b=new byte[str.length()];
b=str.getBytes();
int endIndex=str.length();
for(int i=0;i<str.length();i++){
if(b[i]=='+'){
int index1 = str.indexOf("+");
String str1 = str.substring(0, index1);
double a = Double.parseDouble(str1);
//获取第二个数字
String str2 = str.substring(index1+1, endIndex);
double c = Double.parseDouble(str2);
//加法计算
double sum = a + c;
//System.out.println(a+b);
//输出结果
textField.setText(Double.toString(sum));
}
if(b[i]=='-'){
int index1 = str.indexOf("-");
String str1 = str.substring(0, index1);
double a = Double.parseDouble(str1);
//获取第二个数字
String str2 = str.substring(index1+1, endIndex);
double c = Double.parseDouble(str2);
//加法计算
double sum = a - c;
//System.out.println(a+b);
//输出结果
textField.setText(Double.toString(sum));
}
if(b[i]=='*'){
int index1 = str.indexOf("*");
String str1 = str.substring(0, index1);
double a = Double.parseDouble(str1);
//获取第二个数字
String str2 = str.substring(index1+1, endIndex);
double c = Double.parseDouble(str2);
//加法计算
double sum = a * c;
//System.out.println(a+b);
//输出结果
textField.setText(Double.toString(sum));
}
if(b[i]=='/'){
int index1 = str.indexOf("/");
String str1 = str.substring(0, index1);
double a = Double.parseDouble(str1);
//获取第二个数字
String str2 = str.substring(index1+1, endIndex);
double c = Double.parseDouble(str2);
//加法计算
double sum = a / c;
//System.out.println(a+b);
//输出结果
textField.setText(Double.toString(sum));
}
}
}
});
JButton btnNewButton_1 = new JButton(".");
btnNewButton_1.setBackground(Color.GREEN);
btnNewButton_1.setBounds(292, 342, 69, 63);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+".");
}
});
JButton button = new JButton("0");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("0");
textField.setText(textField.getText()+"0");
}
});
button.setBackground(Color.GREEN);
button.setBounds(202, 342, 69, 63);
JButton btnNewButton_2 = new JButton("%");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"%");
}
});
btnNewButton_2.setBackground(Color.GREEN);
btnNewButton_2.setBounds(113, 342, 69, 62);
contentPane.setLayout(null);
contentPane.add(button);
contentPane.add(btnNewButton_2);
contentPane.add(btnNewButton_1);
contentPane.add(btnNewButton);
JButton btnNewButton_3 = new JButton("C");
btnNewButton_3.setBackground(Color.GREEN);
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
btnNewButton_3.setBounds(22, 342, 69, 62);
contentPane.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("4");
btnNewButton_4.setBackground(Color.GREEN);
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+"4");
}
});
btnNewButton_4.setBounds(22, 251, 69, 68);
contentPane.add(btnNewButton_4);
JButton button_1 = new JButton("3");
button_1.setBackground(Color.GREEN);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"3");
}
});
button_1.setBounds(113, 251, 69, 68);
contentPane.add(button_1);
JButton button_2 = new JButton("2");
button_2.setBackground(Color.GREEN);
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"2");
}
});
button_2.setBounds(202, 251, 69, 68);
contentPane.add(button_2);
JButton button_3 = new JButton("1");
button_3.setBackground(Color.GREEN);
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"1");
}
});
button_3.setBounds(292, 251, 69, 68);
contentPane.add(button_3);
JButton button_4 = new JButton("8");
button_4.setBackground(Color.GREEN);
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+"8");
}
});
button_4.setBounds(22, 158, 69, 68);
contentPane.add(button_4);
JButton button_5 = new JButton("7");
button_5.setBackground(Color.GREEN);
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"7");
}
});
button_5.setBounds(113, 158, 69, 68);
contentPane.add(button_5);
JButton button_6 = new JButton("6");
button_6.setBackground(Color.GREEN);
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"6");
}
});
button_6.setBounds(202, 158, 69, 68);
contentPane.add(button_6);
JButton button_7 = new JButton("5");
button_7.setBackground(Color.GREEN);
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"5");
}
});
button_7.setBounds(292, 158, 69, 68);
contentPane.add(button_7);
JButton button_8 = new JButton("9");
button_8.setBackground(Color.GREEN);
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"9");
}
});
button_8.setBounds(22, 68, 69, 68);
contentPane.add(button_8);
JButton button_9 = new JButton("+");
button_9.setBackground(Color.GREEN);
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"+");
}
});
button_9.setBounds(113, 68, 69, 68);
contentPane.add(button_9);
JButton button_10 = new JButton("-");
button_10.setBackground(Color.GREEN);
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"-");
}
});
button_10.setBounds(202, 68, 69, 68);
contentPane.add(button_10);
JButton button_11 = new JButton("*");
button_11.setBackground(Color.GREEN);
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"*");
}
});
button_11.setBounds(292, 68, 69, 68);
contentPane.add(button_11);
JButton button_12 = new JButton("/");
button_12.setBackground(Color.GREEN);
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"/");
}
});
button_12.setBounds(399, 68, 69, 68);
contentPane.add(button_12);
JButton button_13 = new JButton("退格");
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s=textField.getText();
int a=s.length();
if(a==0){
JOptionPane.showMessageDialog(null, "脑残咯", "alert", JOptionPane.ERROR_MESSAGE);
}
else{
String str1=s.substring(0, a-1);
textField.setText(str1);
}
}
});
button_13.setBackground(Color.GREEN);
button_13.setBounds(402, 158, 69, 68);
contentPane.add(button_13);
JButton button_14 = new JButton("cos");
button_14.setBackground(Color.GREEN);
button_14.setBounds(402, 251, 69, 68);
contentPane.add(button_14);
textField = new JTextField();
textField.setBounds(22, 24, 446, 34);
contentPane.add(textField);
textField.setColumns(10);
}
}
浙公网安备 33010602011771号