一、实验目的
1、掌握可视化程序的结构和设计方法;
2、熟练掌握JFrame、JDialog和JPanel等容器
3、熟练掌握Swing的常用组件
4、熟练掌握布局管理器的使用
5、掌握Swing常用事件、事件类型和监听器接口
二、实验内容
1、算数运算
编写一个应用程序,设计一个窗体,其中有4个按钮,分别命名为 “ 加”、“减”、“乘”、“除”,以及3个文本框,单击相应的按钮,将两个文本框的数字做算术运算,在第3个文本框中显示结果。要求处理NumberFormatException。
import java.awt.Button;
import java.awt.FlowLayout;
import java.math.BigInteger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Arithmetic extends JFrame {
// 文本框
JTextField text1, text2, text3;
// 按钮
JButton button1, button2, button3, button4;
// 标签
JLabel num1Label, num2Label, resultLabel;
// 面板
JPanel num1Panel, num2Panel, resultPanel, buttonPanel;
public Arithmetic() {
setTitle("算术运算");
setBounds(100, 100, 400, 260);
setVisible(true);
setLayout(new FlowLayout());
// 窗口居中
setLocationRelativeTo(null);
text1 = new JTextField(10);
text1.setBounds(100, 20, 165, 40);
text2 = new JTextField(10);
text2.setBounds(100, 50, 165, 40);
text3 = new JTextField(10);
text3.setBounds(100, 80, 165, 40);
button1 = new JButton("加");
button2 = new JButton("减");
button3 = new JButton("乘");
button4 = new JButton("除");
num1Label = new JLabel("请输入数字1:");
num1Label.setBounds(10, 50, 80, 25);
num2Label = new JLabel("请输入数字2");
num2Label.setBounds(10, 20, 80, 25);
resultLabel = new JLabel("结果:");
resultLabel.setBounds(10, 60, 80, 25);
num1Panel = new JPanel();
num2Panel = new JPanel();
resultPanel = new JPanel();
buttonPanel = new JPanel();
num1Panel.add(num1Label);
num1Panel.add(text1);
add(num1Panel);
num2Panel.add(num2Label);
num2Panel.add(text2);
add(num2Panel);
resultPanel.add(resultLabel);
resultPanel.add(text3);
add(resultPanel);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(button4);
add(buttonPanel);
// 重新布局
validate();
// 加
button1.addActionListener(e -> {
String s1 = text1.getText();
String s2 = text2.getText();
try {
BigInteger n1 = new BigInteger(s1);
BigInteger n2 = new BigInteger(s2);
n2 = n1.add(n2);
text3.setText(n2.toString());
} catch (NumberFormatException e2) {
text3.setText("请输入数字!");
text1.setText(null);
text2.setText(null);
}
});
// 减
button2.addActionListener(e -> {
String s1 = text1.getText();
String s2 = text2.getText();
try {
BigInteger n1 = new BigInteger(s1);
BigInteger n2 = new BigInteger(s2);
n2 = n1.subtract(n2);
text3.setText(n2.toString());
} catch (NumberFormatException e2) {
text3.setText("请输入数字!");
text1.setText(null);
text2.setText(null);
}
});
// 乘
button3.addActionListener(e -> {
String s1 = text1.getText();
String s2 = text2.getText();
try {
BigInteger n1 = new BigInteger(s1);
BigInteger n2 = new BigInteger(s2);
n2 = n1.multiply(n2);
text3.setText(n2.toString());
} catch (NumberFormatException e2) {
text3.setText("请输入数字!");
text1.setText(null);
text2.setText(null);
}
});
// 除
button4.addActionListener(e -> {
String s1 = text1.getText();
String s2 = text2.getText();
try {
BigInteger n1 = new BigInteger(s1);
BigInteger n2 = new BigInteger(s2);
if(n2.toString()=="0") {
text3.setText("除数不能为0");
}else {
n2=n1.divide(n2);
text3.setText(n2.toStr