package gui;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/*
* 姓名:黄柯翔评讲模拟考试
*/
public class MyFrame extends JFrame implements ActionListener {
JLabel firstInputLab;
JTextField firstInputTxt;
JLabel secondInputLab;
JTextField secondInputTxt;
JButton btnZhouchang;
JButton btnMianji;
JButton btnRest;
JLabel outputLab;
JTextField outputTxt;
Container container;
// 属性声明组件
public MyFrame() {
this.setTitle("我也是标题");
this.setSize(450, 350);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
container = this.getContentPane();
container.setLayout(null);
firstInputLab = new JLabel("输入矩形的长:");
firstInputLab.setBounds(100, 50, 100, 30);
container.add(firstInputLab);
firstInputTxt = new JTextField();
firstInputTxt.setBounds(200, 50, 150, 30);
container.add(firstInputTxt);
firstInputLab = new JLabel("输入矩形的宽:");
firstInputLab.setBounds(100, 100, 100, 30);
container.add(firstInputLab);
secondInputTxt = new JTextField();
secondInputTxt.setBounds(200, 100, 150, 30);
container.add(secondInputTxt);
btnZhouchang = new JButton("求周长");
btnZhouchang.setBounds(50, 150, 80, 30);
container.add(btnZhouchang);
btnMianji = new JButton("求面积");
btnMianji.setBounds(150, 150, 80, 30);
container.add(btnMianji);
btnRest = new JButton("重置");
btnRest.setBounds(250, 150, 80, 30);
container.add(btnRest);
outputTxt = new JTextField();
outputTxt.setBounds(150, 200, 200, 30);
container.add(outputTxt);
outputLab = new JLabel("结果输出:");
outputLab.setBounds(80, 200, 100, 30);
container.add(outputLab);
btnZhouchang.addActionListener(this);
btnMianji.addActionListener(this);
btnRest.addActionListener(this);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnZhouchang) {
// 周长]
try {
int firstNum = Integer.parseInt(firstInputTxt.getText().trim());// "55" --> 55
int secondNum = Integer.parseInt(secondInputTxt.getText().trim());// "55" --> 55
outputTxt.setText("" + ((firstNum + secondNum) * 2));
} catch (NumberFormatException e1) {
outputTxt.setText("输入有误或未输入");
}
} else if (e.getSource() == btnMianji) {
// 周长
try {
int firstNum = Integer.parseInt(firstInputTxt.getText().trim());// "55" --> 55
int secondNum = Integer.parseInt(secondInputTxt.getText().trim());// "55" --> 55
outputTxt.setText("" + ((firstNum * secondNum)));
} catch (NumberFormatException e1) {
outputTxt.setText("输入有误或未输入");
}
} else if (e.getSource() == btnRest) {
// 重置
firstInputTxt.setText("");
secondInputTxt.setText("");
}
}
public static void main(String[] args) {
new MyFrame();
}
}
// 工具eclipse :
//alt + / 代码提示.不要手工一个字母一个字母
// 注释: ctrl + /