Java图形用户界面实验

306 阅读5分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

要求:

1.能够编程实现java Swing组件的调用,调试窗口、框架、对话框、布局方式、面板、文本编辑器、按钮、组合框等,合理利用委托事件处理模型; 2.能够开发程序实现对不同组件,不同事件的事件处理,设计出能够响应事件的java图形用户界面。 3.业务功能或场景由学员自已选择: (要在此处写明完成的功能和业务场景) 小明同学经常使用计算器,然而他的ipad pro中没有自带的计算器,这让他十分苦恼,请帮助他写一个计算器程序以辅助其日常计算中所需要的计算器功能的使用,实现一般的加减乘除,取余,n次幂,三角函数和对数的计算功能。要求:调用java Swing组件,调试窗口、框架、对话框、布局方式、面板、文本编辑器、按钮、组合框等,合理利用委托事件处理模型,实现对不同组件,不同事件的事件处理,设计出能够响应事件的java图形用户界面。

实现代码:

 package com.zhangyufan.gui;
 ​
 import java.awt.*;
 import java.awt.event.*;
 ​
 import javax.swing.*;
 ​
 public class TestCalculator {
     public static void main(String args[]) {
         Calculator calculator = new Calculator();
     }
 }
 class Calculator extends Frame implements ActionListener, WindowListener{
     private Container container;
     private GridBagLayout layout;
     private GridBagConstraints constraints;
     private JTextField displayField;
     private String lastCommand;
     private double result;
     private boolean start;
     private JMenuBar menubar;
     private JMenuItem m_exit;
     private JMenuItem m2_ejz;
     private JMenuItem m2_bjz;
     private Dialog dialog;
     private Label label_dialog;
     private JButton button_sqrt;
     private JButton button_remainder;
     private JButton button_CE;
     private JButton button_cancel;
     private JButton button_1;
     private JButton button_2;
     private JButton button_3;
     private JButton button_4;
     private JButton button_5;
     private JButton button_6;
     private JButton button_7;
     private JButton button_8;
     private JButton button_9;
     private JButton button_0;
     private JButton button_plus;
     private JButton button_minus;
     private JButton button_multiply;
     private JButton button_divide;
     private JButton button_point;
     private JButton button_equal;
     private JButton button_log;
     private JButton button_tan;
     private JButton button_cos;
     private JButton button_sin;
     private JButton button_exp;
     public Calculator() {
         super("小明同学的Swing版计算器");
         this.setLocation(350, 150);
         this.setSize(450, 400);
         this.setResizable(true);
         this.setLayout(new GridLayout(7, 1));
         this.addmyMenu();
         displayField = new JTextField(30);
         this.add(displayField);
         displayField.setEditable(true);
         start = true;
         result = 0;
         lastCommand = "=";
         JPanel panel0 = new JPanel();
         panel0.setLayout(new GridLayout(1, 4, 4, 4));
         JPanel panel1 = new JPanel();
         panel1.setLayout(new GridLayout(1, 5, 4, 4));
         this.add(panel1);
         button_sqrt = new JButton("sqrt");
         button_remainder = new JButton("%");
         button_exp = new JButton("exp");
         button_CE = new JButton("退位");
         button_cancel = new JButton("c");
         JPanel panel2 = new JPanel();
         panel2.setLayout(new GridLayout(1, 5, 4, 4));
         this.add(panel2);
         button_7 = new JButton("7");
         button_8 = new JButton("8");
         button_9 = new JButton("9");
         button_log = new JButton("log");
         button_divide = new JButton("/");
         JPanel panel3 = new JPanel();
         panel3.setLayout(new GridLayout(1, 5, 4, 4));
         this.add(panel3);
         button_4 = new JButton("4");
         button_5 = new JButton("5");
         button_6 = new JButton("6");
         button_tan = new JButton("tan");
         button_multiply = new JButton("*");
         JPanel panel4 = new JPanel();
         panel4.setLayout(new GridLayout(1, 5, 4, 4));
         this.add(panel4);
         button_1 = new JButton("1");
         button_2 = new JButton("2");
         button_3 = new JButton("3");
         button_cos = new JButton("cos");
         button_minus = new JButton("-");
         JPanel panel5 = new JPanel();
         panel5.setLayout(new GridLayout(1, 5, 4, 4));
         this.add(panel5);
         button_0 = new JButton("0");
         button_point = new JButton(".");
         button_equal = new JButton("=");
         button_sin = new JButton("sin");
         button_plus = new JButton("+");
         panel1.add(button_sqrt);
         panel1.add(button_remainder);
         panel1.add(button_exp);
         panel1.add(button_CE);
         panel1.add(button_cancel);
         panel2.add(button_7);
         panel2.add(button_8);
         panel2.add(button_9);
         panel2.add(button_log);
         panel2.add(button_divide);
         panel3.add(button_4);
         panel3.add(button_5);
         panel3.add(button_6);
         panel3.add(button_tan);
         panel3.add(button_multiply);
         panel4.add(button_1);
         panel4.add(button_2);
         panel4.add(button_3);
         panel4.add(button_cos);
         panel4.add(button_minus);
         panel5.add(button_0);
         panel5.add(button_point);
         panel5.add(button_equal);
         panel5.add(button_sin);
         panel5.add(button_plus);
         button_sqrt.addActionListener(this);
         button_remainder.addActionListener(this);
         button_exp.addActionListener(this);
         button_CE.addActionListener(this);
         button_cancel.addActionListener(this);
         button_7.addActionListener(this);
         button_8.addActionListener(this);
         button_9.addActionListener(this);
         button_log.addActionListener(this);
         button_divide.addActionListener(this);
         button_4.addActionListener(this);
         button_5.addActionListener(this);
         button_6.addActionListener(this);
         button_tan.addActionListener(this);
         button_multiply.addActionListener(this);
         button_1.addActionListener(this);
         button_2.addActionListener(this);
         button_3.addActionListener(this);
         button_cos.addActionListener(this);
         button_minus.addActionListener(this);
         button_0.addActionListener(this);
         button_point.addActionListener(this);
         button_equal.addActionListener(this);
         button_sin.addActionListener(this);
         button_plus.addActionListener(this);
         this.addWindowListener(new WinClose());
         this.setVisible(true);
     }
     private void addmyMenu() {
         JMenuBar menubar = new JMenuBar();
         this.add(menubar);
         JMenu m1 = new JMenu("选项");
         JMenu m2 = new JMenu("进制转换");
         JMenuItem m1_exit = new JMenuItem("退出");
         m1_exit.addActionListener(this);
         JMenuItem m2_ejz = new JMenuItem("二进制");
         m2_ejz.addActionListener(this);
         JMenuItem m2_bjz = new JMenuItem("八进制");
         m2_bjz.addActionListener(this);
         JMenuItem m2_sljz = new JMenuItem("十六进制");
         m2_sljz.addActionListener(this);
         JMenuItem m2_dao_shu = new JMenuItem("倒数");
         m2_dao_shu.addActionListener(this);
         JMenu m3 = new JMenu("帮助");
         JMenuItem m3_Help = new JMenuItem("用法");
         m3_Help.addActionListener(this);
         dialog = new Dialog(this, "提示", true);
         dialog.setSize(240, 80);
         label_dialog = new Label("", Label.CENTER);
         dialog.add(label_dialog);
         dialog.addWindowListener(this);
         m1.add(m1_exit);
         menubar.add(m1);
         m2.add(m2_ejz);
         m2.add(m2_bjz);
         m2.add(m2_sljz);
         m2.add(m2_dao_shu);
         menubar.add(m2);
         m3.add(m3_Help);
         menubar.add(m3);
     }
     public void actionPerformed(ActionEvent e) {
         if (e.getSource().equals(button_1) || e.getSource().equals(button_2) || e.getSource().equals(button_3)
                 || e.getSource().equals(button_4) || e.getSource().equals(button_5) || e.getSource().equals(button_6)
                 || e.getSource().equals(button_7) || e.getSource().equals(button_8) || e.getSource().equals(button_9)
                 || e.getSource().equals(button_0) || e.getSource().equals(button_point)
                 || e.getSource().equals(button_cancel) || e.getSource().equals(button_CE)) {
             String input = e.getActionCommand();
             if (start) {
                 displayField.setText("");
                 start = false;
                 if (input.equals("+/-"))
                     displayField.setText(displayField.getText() + "-");
             }
             if (!input.equals("+/-")) {
                 String str = displayField.getText();
                 if (input.equals("退位")) {
                     if (str.length() > 0)
                         displayField.setText(str.substring(0, str.length() - 1));
                 }else if (input.equals("c")) {
                     displayField.setText("");
                     start = true;
                 } else
                     displayField.setText(displayField.getText() + input);
             }
         }else if (e.getActionCommand() == "二进制") {
             int n = Integer.parseInt(displayField.getText());
             displayField.setText(Integer.toBinaryString(n));
         }else if (e.getActionCommand() == "八进制") {
             int n = Integer.parseInt(displayField.getText());
             displayField.setText(Integer.toOctalString(n));
         }else if (e.getActionCommand() == "十六进制") {
             int n = Integer.parseInt(displayField.getText());
             displayField.setText(Integer.toHexString(n));
         }else if (e.getActionCommand() == "倒数") {
             Double n = Double.parseDouble(displayField.getText());
             displayField.setText(String.valueOf(1 / n));
         }else if (e.getActionCommand() == "退出") {
             System.exit(0);
         }else if (e.getActionCommand() == "用法") {
             label_dialog.setText("欢迎使用小明同学的Swing版计算器,进制转换先录入数字最后再点击几进制选项,sqrt,exp等键是先输运算符再输数字。");
             dialog.setLocation(400, 250);
             dialog.setVisible(true);
         }else {
             String command = e.getActionCommand();
             if (start) {
                 lastCommand = command;
             } else {
                 calculate(Double.parseDouble(displayField.getText()));
                 lastCommand = command;
                 start = true;
             }
         }
     }
     public void calculate(double x) {
         double d = 0;
         if (lastCommand.equals("+"))
             result += x;
         else if (lastCommand.equals("-"))
             result -= x;
         else if (lastCommand.equals("*"))
             result *= x;
         else if (lastCommand.equals("/"))
             result /= x;
         else if (lastCommand.equals("%"))
             result %= x;
         else if (lastCommand.equals("="))
             result = x;
         else if (lastCommand.equals("sqrt")) {
             d = Math.sqrt(x);
             result = d;
         } else if (lastCommand.equals("exp")) {
             d = Math.exp(x);
             result = d;
         } else if (lastCommand.equals("log")) {
             d = Math.log(x);
             result = d;
         } else if (lastCommand.equals("tan")) {
             d = Math.tan(x);
             result = d;
         } else if (lastCommand.equals("cos")) {
             d = Math.cos(x);
             result = d;
         } else if (lastCommand.equals("sin")) {
             d = Math.sin(x);
             result = d;
         }
         displayField.setText("" + result);
     }
     public void windowClosing(WindowEvent e) {
         if (e.getSource() == dialog)
             dialog.setVisible(false);
         else
             System.exit(0);
     }
     public void windowOpened(WindowEvent e) {
     }
     public void windowActivated(WindowEvent e) {
     }
     public void windowDeactivated(WindowEvent e) {
     }
     public void windowClosed(WindowEvent e) {
     }
     public void windowIconified(WindowEvent e) {
     }
     public void windowDeiconified(WindowEvent e) {
     }
     public static void main(String args[]) {
         Calculator calculator = new Calculator();
     }
 }
 class WinClose implements WindowListener {
     public void windowClosing(WindowEvent e) {
         System.exit(0);
     }
     public void windowOpened(WindowEvent e) {
     }
     public void windowActivated(WindowEvent e) {
     }
     public void windowDeactivated(WindowEvent e) {
     }
     public void windowClosed(WindowEvent e) {
     }
     public void windowIconified(WindowEvent e) {
     }
     public void windowDeiconified(WindowEvent e) {
     }
 }

实验过程:

1.程序运行之后跳出来的计算器界面。 在这里插入图片描述

2.进行一般的计算,点击运算数和运算符,然后点等于,显示结果。如计算7+9,先点7,然后点+,然后点9,然后点等于,显示结果为16,正确。 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

3.菜单栏中的选项,进制转换和帮助的演示,其中点击帮助中的用法显示一句提示如何操作的话。 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 结果分析:

实验结果:

在实验过程中已贴出,此处不再展示。

分析:

该程序实现了一个计算器的图形用户界面和计算器的一些基础功能。调用了java Swing组件,调试了窗口、框架、对话框、布局方式、面板、文本编辑器、按钮、组合框等,合理利用委托事件处理模型,实现对不同组件,不同事件的事件处理,设计出了能够响应事件的java图形用户界面。通过该程序,完成了题目中的相关要求。程序整体无语法错误或逻辑错误。

思考题:

awt与swing的区别?

答:1.awt是基于本地方法的C/C++程序,其运行速度比较快;swing是基于awt的Java程序,其运行速度比较慢。 2.awt的控件在不同的平台可能表现不同,而swing在所有平台表现一致。 3.awt不是跨平台的,从外观到控制都依赖操作系统,是重型组件。swing组件是用纯java实现的轻型组件,没有本地代码,不依赖本地操作系统的支持,在不同的操作系统表现一致。swing库是awt库的拓展。