题目:
编程题: 设计一个简单的四则运算工具,有一个标题为“计算”的窗口,窗口布局为FlowLayout。设计四个按钮,分别命名为“加”、“减”、“乘”、“除”,此外,窗口中还有三个文本框,前两个可以输入数字。单击相应的计算按钮,在第三个文本框中显示计算结果。要求处理NumberFormatException异常和除数为0的算术异常。 要求提交源代码或源代码截图、运行截图。 界面设计可自由发挥。 |
代码:
这里我一个包中分了两个java类,这算是一个习惯吧。
代码里有各种注解,还请详细观看,仔细揣摩:
package 图形化界面;
import java.awt.*;
public class Test {
public static void main(String[] args){
new useJFrame();
//这里是new一个咱们自己创建的另一个类,另一个类里面才是各种窗口及实现图形化的代码
}
}
package 图形化界面;
import javax.imageio.ImageIO;
import javax.print.DocFlavor;
import javax.swing.*;
import javax.swing.text.html.ImageView;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
//这里有个继承很重要不要忘记了,少了这个继承会出大问题(下面的有些代码就不会运行了)
public class useJFrame extends JFrame implements MouseListener {
//创建文字和文本框的对象,这里呢建议把创建的对象放在全局变量的位置
JLabel jLabel1=new JLabel("数1:");//JLabel创建一个标签,括号里面是标签名字(可不加)
JTextField jTextField1=new JTextField();//JTextField创建一个可供输入的文本框
JLabel jLabel2=new JLabel("数2:");
JTextField jTextField2=new JTextField();
JLabel jLabel3=new JLabel("答案:");
JTextField jTextField3=new JTextField();
//按钮
JButton addButton=new JButton("加");//JButton 创建按钮对象
JButton minusButton=new JButton("减");
JButton multipleButton=new JButton("乘");
JButton divideButton=new JButton("除");
public useJFrame() {
//这里我分为了三步,每一步执行对应的板块,方便后期查找修改
//初始化界面
initJFrame();
//初始化内容
initView();
//让窗口显示
//(显示的这一步放在最后面,这也是最重要的一步,少了该语句,则看不到图形化界面)
this.setVisible(true);
}
//初始化内容
private void initView() {
//先说一下setBounds的四个参数,前两个是x,y轴。
//从左上角开始向右为x轴,向下为y轴,后面两个是宽高
//添加文字 和 文本框
jLabel1.setBounds(100,100,50,50);//设置位置与宽高
this.getContentPane().add(jLabel1);//将组件添加到容器当中,下同
jTextField1.setBounds(200,100,150,50);
this.getContentPane().add(jTextField1);
jLabel2.setBounds(100,200,50,50);
this.getContentPane().add(jLabel2);
jTextField2.setBounds(200,200,150,50);
this.getContentPane().add(jTextField2);
jLabel3.setBounds(100,300,50,50);
this.getContentPane().add(jLabel3);
jTextField3.setBounds(200,300,150,50);
this.getContentPane().add(jTextField3);
//addMouseListener为添加“鼠标”监听事件,需要实现接口MouseListener
//添加按钮
addButton.setBounds(100,400,80,80);
addButton.addMouseListener(this);//添加监听事件,下同
this.getContentPane().add(addButton);
minusButton.setBounds(300,400,80,80);
minusButton.addMouseListener(this);
this.getContentPane().add(minusButton);
multipleButton.setBounds(100,500,80,80);
multipleButton.addMouseListener(this);
this.getContentPane().add(multipleButton);
divideButton.setBounds(300,500,80,80);
divideButton.addMouseListener(this);
this.getContentPane().add(divideButton);
//7.添加背景图片"http://pic.netbian.com/uploads/allimg/230405/000245-168062416593ad.jpg"
try {
URL url = new URL("http://pic.netbian.com/uploads/allimg/230405/000245-168062416593ad.jpg");
BufferedImage image = ImageIO.read(url);
JLabel background = new JLabel(new ImageIcon(image));
background.setBounds(0, 0, 500, 650);
this.getContentPane().add(background);
} catch (IOException e) {
e.printStackTrace();
}
}
//初始化界面
private void initJFrame() {
//创建一个窗口
//JFrame jFrame=new JFrame("计算");
this.setTitle("计算");
//设置宽高
this.setSize(500,700);
//设置单击窗口“关闭”按钮时,关闭窗口
this.setDefaultCloseOperation(3);
//居中
this.setLocationRelativeTo(null);
//窗口布局方式
this.setLayout(null);//取消内部默认布局(自定义布局)
}
//鼠标点击事件
@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource()==addButton){
System.out.println("点击了加法");
try {
double num1 = Double.parseDouble(jTextField1.getText());//getText()获取文本框的内容
double num2 = Double.parseDouble(jTextField2.getText());
double result = num1 + num2;
jTextField3.setText(Double.toString(result));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "请输入正确的数字格式!", "错误", JOptionPane.ERROR_MESSAGE);//出现异常则显示弹窗
}
}else if(e.getSource()==minusButton){
System.out.println("点击了减法");
try {
double num1 = Double.parseDouble(jTextField1.getText());
double num2 = Double.parseDouble(jTextField2.getText());
double result = num1 - num2;
jTextField3.setText(Double.toString(result));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "请输入正确的数字格式!", "错误", JOptionPane.ERROR_MESSAGE);
}
}else if(e.getSource()==multipleButton){
System.out.println("点击了乘法");
try {
double num1 = Double.parseDouble(jTextField1.getText());
double num2 = Double.parseDouble(jTextField2.getText());
double result = num1 * num2;
jTextField3.setText(Double.toString(result));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "请输入正确的数字格式!", "错误", JOptionPane.ERROR_MESSAGE);
}
}else if(e.getSource()==divideButton){
System.out.println("点击了除法");
try {
double num1 = Double.parseDouble(jTextField1.getText());
double num2 = Double.parseDouble(jTextField2.getText());
if (num2==0) {
JOptionPane.showMessageDialog(this, "除数不能为 0 !", "错误", JOptionPane.ERROR_MESSAGE);
} else {
double result = num1 / num2;
jTextField3.setText(Double.toString(result));
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "请输入正确的数字格式!", "错误", JOptionPane.ERROR_MESSAGE);
}catch(ArithmeticException ex){
JOptionPane.showMessageDialog(this, "除数不能为 0 !", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
完成收工,good!
虽然“天不生无用之人,地不长无名之草”。但仍谨记“冰冻三尺,非一日之寒”。
执长剑纵马,执妙笔生花,我王某人在此邀请诸位与我共身。