中南林业科技大学Java实验报告十一:Swing组件

实验11 Swing组件

11.1 实验目的

  1. 了解Swing组件。
  2. 熟练掌握Swing组件的使用方法。

11.2 实验内容

实现一个可以进行两个数加减乘除的计算器。有两个输入框用以输入数字,屏幕有四个按钮分别为+、-、×、÷,在界面中显示结果。以加法为例如果输入的数字为1和3,则在界面中的输出结果为:1+3=4。在输入时要进行输入检查和异常处理;如果用户未输入两个数,要提示请输入数据,可以用文本提示也可以弹出对话框提示。(注意所有组件采用Swing的组件,控件可以根据需求和设计的不同增加)

11.2.1 代码

11.2.1.1 ScreenUtils类
import java.awt.*;

public class ScreenUtils {


    /*
        获取当前电脑屏幕的宽度
     */
    public static int getScreenWidth(){
        return Toolkit.getDefaultToolkit().getScreenSize().width;
    }

    /*
        获取当前电脑屏幕的高度
     */

    public static int getScreenHeight(){
        return Toolkit.getDefaultToolkit().getScreenSize().height;
    }
}
11.2.1.2 CalView类
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalView {
    JFrame jf = new JFrame("简易计算器");

    /**
     * 定义窗口的宽度
     */
    final int WIDTH = 600;
    /**
     * 定义窗口的高度
     */
    final int HEIGHT = 350;

    /**
     * 计算符号,计算值,计算结果
     */
    String method;
    double calValue1;
    double calValue2;
    double result;

    JTextField valueField1;
    JTextField valueField2;
    JTextField resField;

    /**
     * 组装视图
     */
    public void init() {
        //设置关闭窗口为结束程序
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //设置窗口相关的属性
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);

        //设置窗口大小不可变
        jf.setResizable(false);


        /*
            组装 value1 和 text1
         */
        Box valueBox1 = Box.createHorizontalBox();
        JLabel valueLabel1 = new JLabel("value1");
        //设置为10个长度大小
        valueField1 = new JTextField(10);
        valueBox1.add(valueLabel1);
        //设置水平间隔为20px
        valueBox1.add(Box.createHorizontalStrut(20));
        valueBox1.add(valueField1);

        /*
            组装 value2 和 text2
         */
        Box valueBox2 = Box.createHorizontalBox();
        JLabel valueLabel2 = new JLabel("value2");
        //设置为10个长度大小
        valueField2 = new JTextField(10);
        valueBox2.add(valueLabel2);
        //设置水平间隔为20px
        valueBox2.add(Box.createHorizontalStrut(20));
        valueBox2.add(valueField2);

        /*
            组装 valueBox1 和 valueBox2
         */
        Box valueBox12 = Box.createHorizontalBox();
        valueBox12.add(valueBox1);
        //设置水平间隔为40px
        valueBox12.add(Box.createHorizontalStrut(40));
        valueBox12.add(valueBox2);

        /*
           组装计算方式
         */
        Box methodBox = Box.createHorizontalBox();
        JLabel methodLabel = new JLabel("计算方式");
        JRadioButton addButton = new JRadioButton("+");
        JRadioButton subButton = new JRadioButton("-");
        JRadioButton mulButton = new JRadioButton("×");
        JRadioButton divButton = new JRadioButton("÷");

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String actionCommand = e.getActionCommand();
                //得到当前选择的计算方式
                method = actionCommand;
            }
        };

        addButton.addActionListener(actionListener);
        subButton.addActionListener(actionListener);
        mulButton.addActionListener(actionListener);
        divButton.addActionListener(actionListener);

        //添加到同一个按钮组,实现单选效果
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(addButton);
        buttonGroup.add(subButton);
        buttonGroup.add(mulButton);
        buttonGroup.add(divButton);

        methodBox.add(methodLabel);
        methodBox.add(Box.createHorizontalStrut(30));
        methodBox.add(addButton);
        methodBox.add(Box.createHorizontalStrut(20));
        methodBox.add(subButton);
        methodBox.add(Box.createHorizontalStrut(20));
        methodBox.add(mulButton);
        methodBox.add(Box.createHorizontalStrut(20));
        methodBox.add(divButton);

        /*
         * 组装运算组件
         */
        JButton runButton = new JButton("开始运算");
        runButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //清空结果文本域
                resField.setText("");

                if ("".equals(valueField1.getText()) || "".equals(valueField2.getText())) {
                    //说明有文本为空
                    JOptionPane.showMessageDialog(jf, "请输入数据", "提醒", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                try {
                    calValue1 = Double.parseDouble(valueField1.getText().trim());
                    calValue2 = Double.parseDouble(valueField2.getText().trim());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(jf, "检测到非正常数据", "提醒", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (method == null) {
                    JOptionPane.showMessageDialog(jf, "请选择运算方式", "提醒", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                switch (method) {
                    case "+":
                        result = calValue1 + calValue2;
                        break;
                    case "-":
                        result = calValue1 - calValue2;
                        break;
                    case "×":
                        result = calValue1 * calValue2;
                        break;
                    case "÷":
                        if (calValue2 == 0) {
                            JOptionPane.showMessageDialog(jf, "检测到非法运算(0作为分母)", "提醒", JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        result = calValue1 / calValue2;
                        break;
                }



                //在文本域中显示运算结果
                if(((result+"").matches(".*(\\.0)$"))){
                    resField.setText(valueField1.getText() + " " + method + " " + valueField2.getText() + " = " + (int)result);
                }else{
                    resField.setText(valueField1.getText() + " " + method + " " + valueField2.getText() + " = " + result);
                }

            }
        });

        Box runBox = Box.createHorizontalBox();
        runBox.add(runButton);


        /*
          组装计算结果
         */
        Box resBox = Box.createHorizontalBox();
        JLabel resLabel = new JLabel("计算结果");
        resField = new JTextField(40);

        resBox.add(resLabel);
        resBox.add(Box.createHorizontalStrut(30));
        resBox.add(resField);

        /*
         * 组装 valueBox12 methodBox resBox
         */
        Box viewBox = Box.createVerticalBox();
        viewBox.add(Box.createVerticalStrut(60));
        viewBox.add(valueBox12);
        viewBox.add(Box.createVerticalStrut(50));
        viewBox.add(methodBox);
        viewBox.add(Box.createVerticalStrut(50));
        viewBox.add(runBox);
        viewBox.add(Box.createVerticalStrut(50));
        viewBox.add(resBox);

        /*
         * 组装 viewBox 到面板
         */
        JPanel viewPanel = new JPanel();
        //设置背景颜色为青蓝色
        viewPanel.setBackground(new Color(153, 255, 255));

        //为panel设置边框 创建凸起的斜边框,分别设置四条边的颜色
        viewPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.RED, Color.GREEN, Color.BLUE, Color.GRAY));
        viewPanel.add(viewBox);

        jf.add(viewPanel);

        //显示窗口可见
        jf.setVisible(true);

    }
}
11.2.1.3 CalApplication类
/**
 *主启动类
 */
public class CalApplication {
    public static void main(String[] args) {
        new CalView().init();
    }
}

11.2.2 演示

image-20221108032126115

image-20221108032329192

image-20221108032146528

image-20221108032228882

image-20221108032257898

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是谢添啊

感谢你的支持,我会继续加油的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值