使用java制作计算器
使用eclipse软件,在打开方式中选择WindowsBuilder Editor,以此为工具进行编程。
代码如下:
package test4;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
public class MyFrame extends JFrame {
private JTextField textField;
public static void main(String[] args) {
MyFrame mf = new MyFrame();
}
public MyFrame() {
getContentPane().setFont(new Font("宋体", Font.PLAIN, 20));
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("\u67F4\u9E21\u8BA1\u7B97\u5668"); // 设置窗体的标题
this.setSize(400, 320); // 设置窗体的大小
this.setVisible(true); // 设置窗体为可见,即显示窗体
this.setResizable(false);
this.setLocationRelativeTo(null);//窗口居中
JButton button_3 = new JButton("1");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + 1);
}
});
JButton button_4 = new JButton("2");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText() + 2);
}
});
JButton button_5 = new JButton("3");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + 3);
}
});
JButton button_6 = new JButton("4");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + 4);
}
});
JButton button_7 = new JButton("5");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + 5);
}
});
JButton button_8 = new JButton("6");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + 6);
}
});
JButton button_9 = new JButton("7");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + 7);
}
});
JButton button_10 = new JButton("8");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + 8);
}
});
JButton button_11 = new JButton("9");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + 9);
}
});
JButton button_12 = new JButton("0");
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + 0);
}
});
JButton button_13 = new JButton("+");
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "+");
}
});
JButton button_14 = new JButton("-");
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "-");
}
});
JButton button_15 = new JButton("×");
button_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "×");
}
});
JButton button_16 = new JButton("÷");
button_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "÷");
}
});
JButton button_17 = new JButton("=");
button_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = null;
if ((textField.getText().indexOf("=")) != -1) { //确定是第几步运算
int i = textField.getText().lastIndexOf("=");
String j = textField.getText();
s = textField.getText().subSequence(i+1, j.length()).toString();
}else{
s = textField.getText();
}
int a ; //实现计算过程并输出结果
if((a = s.indexOf("+")) != -1){
float b = Float.parseFloat(s.subSequence(0, a).toString());
float c = Float.parseFloat(s.subSequence(a+1, s.length()).toString());
textField.setText(textField.getText() + "=" + (b+c));
}else if((a = s.indexOf("-")) != -1){
float b = Float.parseFloat(s.subSequence(0, a).toString());
float c = Float.parseFloat(s.subSequence(a+1, s.length()).toString());
textField.setText(textField.getText() + "=" + (b-c));
}else if((a = s.indexOf("×")) != -1){
float b = Float.parseFloat(s.subSequence(0, a).toString());
float c = Float.parseFloat(s.subSequence(a+1, s.length()).toString());
textField.setText(textField.getText() + "=" + (b*c));
}else if((a = s.indexOf("÷")) != -1){
double b = Float.parseFloat(s.subSequence(0, a).toString());
double c = Float.parseFloat(s.subSequence(a+1, s.length()).toString());
textField.setText(textField.getText() + "=" + (b/c));
}
}
});
JButton btnDel = new JButton("Del");
btnDel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
textField = new JTextField();
textField.setFont(new Font("宋体", Font.PLAIN, 18));
textField.setColumns(10);
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(28)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button_3, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_7, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_11, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_15, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE))
.addGap(30)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button_4, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_8, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_12, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_16, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE))
.addGap(28)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button_5, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_9, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_13, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_17, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE))
.addGap(28)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(btnDel, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_14, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_10, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
.addComponent(button_6, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)))
.addGroup(groupLayout.createSequentialGroup()
.addGap(45)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 281, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(56, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(26)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button_3)
.addComponent(button_4)
.addComponent(button_5)
.addComponent(button_6))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button_7)
.addComponent(button_8)
.addComponent(button_9)
.addComponent(button_10))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button_11)
.addComponent(button_12)
.addComponent(button_13)
.addComponent(button_14))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button_15)
.addComponent(button_16)
.addComponent(button_17)
.addComponent(btnDel))
.addContainerGap(65, Short.MAX_VALUE))
);
getContentPane().setLayout(groupLayout);
}
}