用Java设计一个简易加法计算器

用Java设计一个简易加法计算器

主要代码:

package title;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.awt.event.ActionListener;

public class Mycomputer extends JFrame implements ActionListener {
JTextField JT1, JT2, JT3;
JLabel JL1;
JButton JB;

Mycomputer() {
setTitle("简易加法计算器");
Container cp = this.getContentPane();
cp.setLayout(new FlowLayout());
JT1 = new JTextField(5);
JL1 = new JLabel("+");
JT2 = new JTextField(5);
JB = new JButton("=");
JT3 = new JTextField(5);
cp.add(JT1);
cp.add(JL1);
cp.add(JT2);
cp.add(JB);
cp.add(JT3);
JT3.setEditable(false);
JB.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
String add1 = JT1.getText();
String add2 = JT2.getText();
Double sum = Double.parseDouble(add1) + Double.parseDouble(add2);
JT3.setText(" " + sum);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Mycomputer t = new Mycomputer();
int width = 500;
int height = 500;
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension d = tool.getScreenSize();
t.setResizable(false);
t.setBounds((d.width - width) / 2, (d.height - height) / 2, width,
height);
t.setVisible(true);
}

}

结果如图

用Java设计一个简易加法计算器_第1张图片

你可能感兴趣的:(Java)