NJUPT《JAVA程序设计》

作者: Du1in9 | 来源:发表于2022-05-26 08:30 被阅读0次

    1/3 课堂练习

    第一次作业 第二次作业 第三次作业 第四次作业 第五次作业 第六次作业 设计图 第六次作业_代码图 第七次作业_设计图 第七次作业_代码图
    https://wwb.lanzouh.com/iVkRQ05dr59a
    https://wwb.lanzouh.com/iGv9p05drbna
    链接:https://pan.baidu.com/s/1yEBHgGGm1D5OnEPguH5wBw 提取码:njup

    2/3 实验报告

    实验一:综合图形界面程序设计
    • 实验目的和要求

    学习和理解 JAVA SWING 中的容器,部件,布局管理器和部件事件处理方法。通过编写和调试程序,掌握 JAVA 图形界面程序设计的基本方法。

    • 实验环境

    PC 微型计算机系统,Microsoft Windows 操作系统,SUN Java Development Kit开发工具包,NetBeans 开发工具。

    • 实验原理及内容

    设计和编写一个用于将人民币转换为等值的美元的程序,界面要求可以输入人民币的金额并可以得到转换后的结果。
    1、软件工具的选择
    MyEclipse 10
    首先,导入需要用到的包(界面功能+按钮功能)

    import java.awt.*;                      //引入 AWT 包
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    
    import javax.swing.*;                   //引入 swing 包
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.EtchedBorder;
    

    2、分析,设计,编写
    人民币转换程序主要分为四个部分,第一个部分是界面的初始化,第二个部分是输入金额区域,第三个部分是输出金额区域,第四个部分是按钮事件区域。



    第一部分,利用 setBounds()函数设置界面布局的位置及宽高,利用 setBorder(new EmptyBorder) 函数设置边框,利用 setLayout(new BorderLayout) 设置内间距,利用 setLayout(new GridLayout)设置界面网格的布局。其他设置还包括:窗体标题、退出操作。

        private JPanel contentPane;         //总面板
        private JTextField textField1;      //单行文本域 1
        private JTextField textField2;      //单行文本域 2
        private String money;               //人民币字符串 
    
            setTitle("转换器");                                        //设置窗体标题
            setBounds(500, 200, 450, 350);                          //布局设置(x,y,宽,高)
            setDefaultCloseOperation(EXIT_ON_CLOSE);                //设置用户在发起 “close” 时执行退出操作
            contentPane = new JPanel();                             //面板对象
            contentPane.setBorder(new EmptyBorder(5,5,5,5));        //线边框(上厚,左厚,下厚,右厚)
            contentPane.setLayout(new BorderLayout(0,0));           //边界布局(横距,纵距)
            setContentPane(contentPane);                            //设置 Content Pane   
            JPanel panel = new JPanel();    
            panel.setLayout(new GridLayout(3,1,5,5));               //总网格布局(行数,列数,横距,纵距)
            contentPane.add(panel);
    

    第二、三部分,利用 setBorder (new EtchedBorder) 设置边框类型,利用 setFont(new Font) 设置字体类型,利用 setLayout(new GridLayout)设置子网格的布局,利用 new JLabel()设置提示文本,利用 setColumns()设置输入输出文本长度。利用 addFocusListener(new FocusListener() ) 设置事件监听器,从而传入输入文本。

            JPanel panel1 = new JPanel();   
            panel.add(panel1);
            panel1.setBorder(new EtchedBorder(EtchedBorder.LOWERED,null,null));     //蚀刻边框
            panel1.setLayout(new GridLayout(1,2,5,5));              //网格布局  
            JLabel label = new JLabel("转换前 RMB(¥):");           //提示文本设置
            label.setFont(new Font("微软雅黑", Font.PLAIN, 16));        //提示字体设置
            panel1.add(label);  
            textField1 = new JTextField();      
            textField1.setFont(new Font("微软雅黑",Font.PLAIN,16)); //输入字体设置
            panel1.add(textField1);
            textField1.setColumns(10);                              //输入长度设置
            textField1.addFocusListener(new FocusListener() {
                public void focusLost(FocusEvent e) {
                    money = textField1.getText();                   //输入文本设置
                }
                public void focusGained(FocusEvent e) { }
            });
            
            JPanel panel2 = new JPanel();       
            panel.add(panel2);
            panel2.setBorder(new EtchedBorder(EtchedBorder.LOWERED,null,null));     //蚀刻边框
            panel2.setLayout(new GridLayout(1,2,5,5));              //网格布局  
            JLabel label1 = new JLabel("转换后 dollor($):");       //提示文本设置
            label1.setFont(new Font("微软雅黑", Font.PLAIN, 16));   //提示字体设置
            panel2.add(label1);
            textField2 = new JTextField();
            textField2.setFont(new Font("微软雅黑",Font.PLAIN,16)); //输出字体设置
            panel2.add(textField2);
            textField1.setColumns(10);                              //输出长度设置
    
    

    第四部分,利用 setBorder (new EtchedBorder) 设置边框类型,利用 setFont(new Font) 设置字体类型,利用 new JButton () 设置按钮文本。再利用 addActionListener(new ActionListener()) 设置按钮事件,从而输出转换后的文本。

            JPanel panel3 = new JPanel();
            panel.add(panel3);
            panel3.setBorder(new EtchedBorder(EtchedBorder.LOWERED,null,null));     //蚀刻边框
            JButton jb1 = new JButton("转换");                        //按钮文本设置
            jb1.setFont(new Font("微软雅黑",Font.PLAIN,16));            //按钮字体设置
            panel3.add(jb1);
            jb1.addActionListener(new ActionListener() {            //输出文本设置
                public void actionPerformed(ActionEvent e) {
                    textField2.setText(Double.toString(Double.parseDouble(money)*0.1518));      
                }
            }); 
            setVisible(true);
    

    3、调试


    实验二:指针式时钟程序
    • 实验目的和要求

    本实验旨在通过实验,培养学生将 JAVA 线程的相关知识点(包括线程调度,线程同步等)有机结合并加以综合应用,在实验中设计多线程程序的能力。

    • 实验环境

    PC 微型计算机系统,Microsoft Windows 操作系统,SUN Java Development Kit开发工具包,NetBeans 开发工具。

    • 实验原理及内容

    设计和编写一个编写一个指针式时钟程序,应用线程实现时钟的走动。


    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    

    一、Clock() 主函数
    继承 JFrame 类,创建一个 Layout 对象

    public class Clock extends JFrame{
        public static void main(String[]s) {
            new Layout();
        }
    }
    

    二、Layout() 添加窗口和组件
    继承 JFrame 类,创建一个 Clockpaint 对象(坐标:20,20,半径 170)
    添加对象到容器,并初始化组件和时钟窗口。

    class Layout extends JFrame {       //添加窗口和时钟组件
        public Layout() {
            ClockPaint cp = new ClockPaint(20, 20, 170);
            add(cp);                    //添加组件到容器JFrame里
            setBounds(260, 120, 400, 400);  //设置组件的大小和位置
            setResizable(false);        //设置窗口不可调整大小
            this.setTitle("指针式时钟");  //设置窗口标题
            this.setVisible(true);      //设置窗口可见
        }
    }
    

    三、ClockPaint 定义时钟组件
    继承 JFrame 类,预定义 Runnable 接口。
    首先定义秒、分、时,以及弧度四个参数。

    class ClockPaint extends JPanel implements Runnable {   //定义时钟组件
        int x, y, r;                    //时钟的位置坐和半径
        int h, m, s;                    //小时,分钟,秒
        double rad = Math.PI / 180;     //定义弧度
    

    1、ClockPaint 构造函数
    构造 Clockpaint 对象,对参数 s、m、h 赋值。然后创建并启动线程。

        public ClockPaint(int x, int y, int r) {    //构造函数
            this.x = x;
            this.y = y;
            this.r = r;
            Calendar now = Calendar.getInstance();  //初始化H历对象
            s = now.get(Calendar.SECOND) * 6;       //获得初始秒转换成度数
            m = now.get(Calendar.MINUTE) * 6;       //获得初始分转换成度数
            h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30
                    + now.get(Calendar.MINUTE) * 6 / 12;    //获得初始小时转换成度数加分钟实现连贯
            Thread t = new Thread(this);      //新建线程
            t.start();                              //启动线程
        }
    

    2、paint 画钟函数
    设置背景色,边框色,画边框,画一共12个刻度每个相隔30度。
    然后就是画三条针, 长度各不相同,对应代码如下:

    x1 = (int) ((0.8 * r) * Math.sin(rad * s));
    y1 = (int) ((0.8 * r) * Math.cos(rad * s));
    x2 = (int) ((0.6 * r) * Math.sin(rad * s));
    y2 = (int) ((0.6 * r) * Math.cos(rad * s));
    x3 = (int) ((0.4 * r) * Math.sin(rad * s));
    y3 = (int) ((0.4 * r) * Math.cos(rad * s));
    

    最后在窗口数字显示时间,用 drawString()函数。

        public void paint(Graphics g) {             //画钟
            g.setColor(Color.BLACK);                //背景色
            g.fillRect(0, 0, r * 3, r * 3);
            g.setColor(Color.WHITE);                //边框色
            g.drawOval(x, y, r * 2, r * 2);
            int d = 0;
            int x1, y1, x2, y2;
            for (int i = 0; i < 60; i++) {          //每6度画一个点
                x1 = (int) ((r - 2) * Math.sin(rad * d));
                y1 = (int) ((r - 2) * Math.cos(rad * d));
                g.drawString("", x + r + x1 - 1, x + r - y1 + 1);
                d += 6;
            }
            d = 30;         //从30度开始每30度画一个数字和一条线
            for (int i = 1; i <= 12; i++) {
                x1 = (int) ((r - 14) * Math.sin(rad * d));
                y1 = (int) ((r - 14) * Math.cos(rad * d));
                g.drawString(i + "", x + r + x1 - 4, x + r - y1 + 5);
                x1 = (int) ((r - 6) * Math.sin(rad * d));
                y1 = (int) ((r - 6) * Math.cos(rad * d));
                x2 = (int) ((r - 2) * Math.sin(rad * d));
                y2 = (int) ((r - 2) * Math.cos(rad * d));
                g.drawLine(x + r + x2, y+r - y2, x + r + x1, y + r - y1);
                d += 30;
            }
            g.setColor(Color.RED);                  //按时间画秒针
            x1 = (int) ((0.8 * r) * Math.sin(rad * s));
            y1 = (int) ((0.8 * r) * Math.cos(rad * s));
            g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
    
            g.setColor(Color.BLUE);                 //按时间画分针
            x1 = (int) ((0.6 * r) * Math.sin(rad * m));
            y1 = (int) ((0.6 * r) * Math.cos(rad * m));
            g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
    
            g.setColor(Color.YELLOW);               //按时间画时针
            x1 = (int) ((0.4 * r) * Math.sin(rad * h));
            y1 = (int) ((0.4 * r) * Math.cos(rad * h));
            g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
    
            g.setColor(Color.GREEN);                //显示时间
            Calendar nowl = Calendar.getInstance();
            g.drawString(nowl.get(Calendar.HOUR_OF_DAY) + ":" +
                    nowl.get(Calendar.MINUTE) + ":" +
                    nowl.get(Calendar.SECOND), 0, 20);
        }
    

    3、run 运行函数
    启动时钟,线程设置每 1000 毫秒刷新一次。
    秒针每次走6度,秒针、分针、时针每走完 360 度重置一次。

        public void run() {         //运行时钟
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (Exception ex) {
                    System.out.println(ex);
                }
                s += 6;             //秒针走6度
                if (s >= 360) {     //秒针走完1分钟后重置
                    s = 0;
                    m += 6;
                    if (m >= 360) { //分针走完1小时后重置
                        m = 0;
                        h += 6;
                    }
                    if (h >= 360) { //时针走完12小时后重置
                        h = 0;
                    }
                }
                this.repaint();     //重新绘制时钟
            }
        }
    

    JFrame是官方提供的一个类,该类可以快速的开发出Java界面应用程序(c/s架构),属于java.swing知识体系;它是屏幕上window的对象,能够最大化、最小化、关闭。
    1.JFrame()创建一个无标题的窗口
    2.JFrame(String s)创建标题为s的窗口
    3.public void setSize(int width,int height)设置窗口大小4.public void setLocation(int x,int y)设置窗口位置,默认位置为(0,0)
    5.public void setBounds(int a,int b,int width,int height)设置窗口的初始位置是(a,b),即距离屏幕左边a个像素,巨离屏幕上方b个像素,窗口的宽是width,高是height
    6.public void setBackgorund(color.red)设置窗体背景颜色
    7.public void setVisible(boolean b)设置窗口是否可见,默认窗口是不可见的
    8.public void setResizable(boolean b)设置窗口是否可调整大小,默认窗口可调整大小
    9.public void dispose()撤销当前窗口并释放所有使用的资源
    10.add(Component comp)向容器中增加组件
    11.getContentpane()返回此窗口的容器对象
    12.public void setExtendedState(int state)设置窗口的扩展状态,其中参数state取JFrame类中的下面类常量:MAXIMIZED_HORIZ(水平方向最大化)
    MAXIMIZED_VERI(垂直方向最大化)
    MAXIMIZED_BOYH(水平、垂直方向最大化)
    13.public void setDefaultClose(int operation)设置单击窗体右上角的关闭图标后,程序会做出怎样的处理参数operation取JFrame类中的下列int型static常量,程序根据参数operation取值做出不同的处理:DO_NOTHING_ON_CLOSE(什么也不做)
    HIDE_ON_CLOSE(隐藏当前窗口)
    DISPOSE_ON_CLOSE(隐藏当前窗口并释放窗体占有的其他资源)
    实验三:流处理程序设计
    • 实验目的和要求

    要求学生能在学习和理解课堂学习内容中 JAVA 流编程理论的基础上,学习并逐步掌握 JAVA 流程序的编写和调试,学习根据处理需求对不同流的正确选择使用和组合使用方法。

    • 实验环境

    PC 微型计算机系统,Microsoft Windows 操作系统,SUN Java Development Kit开发工具包,NetBeans 开发工具。

    • 实验原理及内容

    设计和编写一个程序从键盘读入一行字符串,将其写入一个文本文件中,再编写另一个程序从文本文件中读入字符串并在命令行窗口显示出来。


    import java.io.*;
    import java.util.Calendar;
    
    public class CalendarWR {
        public static void main(String args[]){
            // 将Calendar对象写入文件cal.ser
            Calendar cal = Calendar.getInstance();
            int h = cal.get(Calendar.HOUR_OF_DAY);
            int m = cal.get(Calendar.MINUTE);
            int s = cal.get(Calendar.SECOND);
            String timeStr = h + ":" + m + ":" + s;
            System.out.println("Time stored -> " + timeStr);
            try{
                FileOutputStream f=new FileOutputStream("cal.txt");
                ObjectOutputStream o=new ObjectOutputStream(f);
                o.writeObject(cal);
                o.close();
                f.close();
            }
            catch(IOException e){}
    
            //从cal.ser文件读出数据恢复Calendar对象
            Calendar cal2 = null;
            try{
                FileInputStream f=new FileInputStream("cal.txt");
                ObjectInputStream o=new ObjectInputStream(f);
                cal2 = (Calendar)o.readObject();
                o.close();
                f.close();
            }catch(IOException e){}
            catch(ClassNotFoundException e){}
            int hh = cal2.get(Calendar.HOUR_OF_DAY);
            int mm = cal2.get(Calendar.MINUTE);
            int ss = cal2.get(Calendar.SECOND);
            String timeStr2 = hh + ":" + mm + ":" + ss;
            System.out.println("Time recovered -> " + timeStr2);
        }
    }
    
    实验四: 小应用程序 Applet 设计
    • 实验目的和要求

    要求学生能在学习和理解课堂学习内容中 JAVA 小应用程序的基础上,通
    过实验,培养学生将 JAVA 小应用程序相关知识点(包括 JAVA Applet 和低级事件处理模型)有机结合,设计基于 WEB 浏览器的小应用程序的能力。

    • 实验环境

    PC 微型计算机系统,Microsoft Windows 操作系统,SUN Java Development Kit开发工具包,IDEA 开发工具。

    • 实验原理及内容

    Java Applets Support 插件:https://plugins.jetbrains.com/plugin/13148-java-applets-support/versions

    Edit Run Configuration
    1、调用 paint()方法显示小应用程序
    // 1、HelloWorld.java
    
    import java.awt.*;
    import java.applet.*;
    
    public class HelloWorld extends Applet {
        public void paint(Graphics g){
            g.drawString("Hello World!", 25, 25);
        }
    }
    

    2、简单的图像测试

    // 2、HwImage.java
    
    import java.awt.*;
    import java.applet.Applet;
    
    public class HwImage extends Applet {
            Image duke;
            public void init() {
                    duke = getImage(getDocumentBase(), "images/duke.jpg");
            }
            public void paint(Graphics g) {
                    g.drawImage(duke, 25, 25, this);
            }
    }
    
    <html>
    <applet code=HwImage.class width=400 height=300>
    <param name=duke value="images/duke.jpg">
    </applet>
    </html>
    

    3、简单的声音测试

    // 3、HwLoop.java
    
    import java.awt.Graphics;
    import java.applet.*;
    
    public class HwLoop extends Applet {
            AudioClip sound;
            public void init() {
                    sound = getAudioClip(getCodeBase(),"sounds/duke.mp3");
            }
    
            public void paint(Graphics g) {
                    g.drawString("Audio Test", 25, 25);
            }
            public void start() {
                    sound.loop();
            }
            public void stop() {
                    sound.stop();
            }
    }
    
    <html>
    <applet code=HwLoop.class width=400 height=300>
    <param name=sound value="sounds/duke.mp3">
    </applet>
    </html>
    

    4、设计和编写一个可以用鼠标操作的 Applet 小应用程序和相应的 HTML 页面,观察 Applet 的执行过程,测试程序鼠标用户交互操作的效果。


    // Mouseevent.java
    
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Mouseevent extends Applet implements ActionListener {
        private static final long serialVersionUID = 1L;
        Button b,c;
        public void init() {
        Label label=new Label("请按按钮");
        b=new Button("按钮1");//构造按钮
        b.addActionListener(this);//监视器
        c=new Button("按钮2");
        c.addActionListener(this);
        add(label);
        add(b);//将按钮加入网页
        add(c);
    }
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==b) {
                Frame frame=new Frame("提醒");
                frame.setSize(230,100);//设置大小
                frame.setLocation(100,100);//设置位置
                frame.add(new Label("你按了按钮1"));//添加标签
                frame.setVisible(true);//设置是否可见
                frame.validate();
            }
            else if(e.getSource()==c) {
                Frame frame=new Frame("提醒");
                frame.setSize(230,100);
                frame.setLocation(100,100);
                frame.add(new Label("你按了按钮2"));
                frame.setVisible(true);
                frame.validate();
            }
        }
    }
    
    <html>
    <applet code=HwLoop.class width=400 height=400></applet>
    </html>
    

    3/3 期末大作业

    • 大作业课题说明

    课题代号: 2
    课题名称:TI程序员计算器程序
    课题要求:① 基本要求:设计德州仪器程序员专用计算器(1977 年-1982 年发布)。按照TI程序员计算器(1982 年LCD版本)的原始面板键盘和显示布局设计出计算器的交互窗口,参照计算器的功能介绍,基本要求只需要实现 10 进制和 16 进制的基础算术运算。即不含括号的单步加减乘除运算。
    ② 提高要求:参照计算器的功能介绍,实现输入的 10 进制数和 16 进制数之间的相互转换;实现与、或、异或、反码、补码和移位运算;实现单一进制模式下的带括号的组合多步运算;实现混合进制模式下带括号的组合多步运算;设计运算溢出等出错提示。

    • 补充材料
    • 实验原理及内容
    //  Convert.java
    
    
    package calculator;
    
    public class Convert {
    
        static int toInt(char str) {
            switch (str) {
                case '0':
                    return 0;
                case '1':
                    return 1;
                case '2':
                    return 2;
                case '3':
                    return 3;
                case '4':
                    return 4;
                case '5':
                    return 5;
                case '6':
                    return 6;
                case '7':
                    return 7;
                case '8':
                    return 8;
                case '9':
                    return 9;
                case 'a':
                    return 10;
                case 'b':
                    return 11;
                case 'c':
                    return 12;
                case 'd':
                    return 13;
                case 'e':
                    return 14;
                case 'f':
                    return 15;
            }
            return 0;
        }
        static int hex_convert(String s) {
            int i;
            int temp = 0;
            for(i=s.length()-1;i>=0;i--)
                temp += toInt(s.charAt(i))*Math.pow(16,s.length()-i-1);
            return temp;
        }
        static int oct_convert(String s) {
            int i;
            int temp = 0;
            for(i=s.length()-1;i>=0;i--)
                temp += toInt(s.charAt(i))*Math.pow(8,s.length()-i-1);
            return temp;
        }
    
        public static void main(String[] args){
    //        System.out.println(hex_convert("a7b"));
    //        System.out.println(oct_convert("57222"));
            System.out.println(Integer.toHexString(0xff<<8));
        }
    }
    
    //  demo.java
    
    
    package calculator;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import calculator.Convert;
    
    class Calculator {
        private Frame f = new Frame("计算器");
        private Frame info = new Frame("对照表");
        private Button[] b = new Button[16];
        private Button[] cal = new Button[20];
        private MenuBar mb = new MenuBar();
        private Menu option = new Menu("Edit");
        private Menu other = new Menu("Other");
        private MenuItem about = new MenuItem("About");
        private MenuItem reset = new MenuItem("Reset");
        private MenuItem exit = new MenuItem("Exit");
        private JLabel information = new JLabel();
        private TextField text = new TextField(30);
        private int flag = 0;
        private int flag2 = -1;
        private int operate = 0;    //0:进制转换,1:加减乘除,2:逻辑运算,3:位运算
        private int base = 0;       //0:10进制,1:16进制,2:8进制
        private int click = 0;      //0:第一次设置进制数,1:第二次设置进制数
        private int Key = -1;
        String load1 = new String();
        String load2 = new String();
    
    
        public void init(){
            Panel p = new Panel();
            NumListener numListen = new NumListener();
            CalListener calListen = new CalListener();
            load1 = load2 = null;
            for(int i = 0; i <= 9; i++)
                b[i] = new Button(""+i);
            info.setSize(300,400);
    
            information.setText("<html><p>Time:2022 / 5 / 18,Author:Du1in9</p>"+
                    "<p>0x01:0001</p><p>0x02:0012</p><p>0x03:0011</p><p>0x04:0100</p>"+
                    "<p>0x05:0001</p><p>0x06:0012</p><p>0x07:0011</p><p>0x08:1000</p></p>"+
                    "<p>0x09:1001</p><p>0x0a:1010</p><p>0x0b:1011</p><p>0x0c:1100</p></p>"+
                    "<p>0x0d:1101</p><p>0x0e:1110</p><p>0x0f:1111</p><p>0x00:0000</p><p></html>");
    
            for(int i = 0; i <= 9; i++) {
                b[i].addActionListener(numListen);
                b[i].setBackground(Color.pink);
            }
            b[10] = new Button("a");
            b[11] = new Button("b");
            b[12] = new Button("c");
            b[13] = new Button("d");
            b[14] = new Button("e");
            b[15] = new Button("f");
            b[10].setBackground(Color.pink);
            b[11].setBackground(Color.pink);
            b[12].setBackground(Color.pink);
            b[13].setBackground(Color.pink);
            b[14].setBackground(Color.pink);
            b[15].setBackground(Color.pink);
    
            b[10].addActionListener(numListen);
            b[11].addActionListener(numListen);
            b[12].addActionListener(numListen);
            b[13].addActionListener(numListen);
            b[14].addActionListener(numListen);
            b[15].addActionListener(numListen);
            b[10].addActionListener(numListen);
            b[11].addActionListener(numListen);
            b[12].addActionListener(numListen);
            b[13].addActionListener(numListen);
            b[14].addActionListener(numListen);
            b[15].addActionListener(numListen);
    
            cal[0] = new Button("+");
            cal[1] = new Button("-");
            cal[2] = new Button("*");
            cal[3] = new Button("/");
            cal[4] = new Button("=");
            cal[5] = new Button("DE");
            cal[6] = new Button("SHF");
            cal[7] = new Button("AC");
    
            cal[14] = new Button("DEC");
            cal[15] = new Button("HEX");
            cal[16] = new Button("OCT");
            cal[17] = new Button("AND");
            cal[18] = new Button("OR");
            cal[19] = new Button("XOR");
    
            cal[0].addActionListener(calListen);    // +
            cal[1].addActionListener(calListen);    // -
            cal[2].addActionListener(calListen);    // *
            cal[3].addActionListener(calListen);    // /
            cal[4].addActionListener(calListen);    // =
            cal[5].addActionListener(e->{           // DE
                text.setText(text.getText().substring(0,text.getText().length()-1));
            });
            cal[6].addActionListener(calListen);    // SHF
            cal[7].addActionListener(e->{           // AC
                load1 = load2 = null;
                Key = -1;
                flag = 0;
                flag2 = -1;
                operate = 0;
                base = 0;
                text.setText("");
            });
            cal[14].addActionListener(calListen);   // DEC
            cal[15].addActionListener(calListen);   // HEX
            cal[16].addActionListener(calListen);   // OCT
            cal[17].addActionListener(calListen);   // AND
            cal[18].addActionListener(calListen);   // OR
            cal[19].addActionListener(calListen);   // XOR
    
            p.add(b[1]);p.add(b[2]);p.add(b[3]);p.add(b[4]);p.add(cal[0]);p.add(cal[1]);
            p.add(b[5]);p.add(b[6]);p.add(b[7]);p.add(b[8]);p.add(cal[2]);p.add(cal[3]);
            p.add(b[9]);p.add(b[10]);p.add(b[11]);p.add(b[12]);p.add(cal[4]);p.add(cal[5]);
            p.add(b[13]);p.add(b[14]);p.add(b[15]);p.add(b[0]);p.add(cal[6]);p.add(cal[7]);
            p.add(cal[14]);p.add(cal[15]);p.add(cal[16]);p.add(cal[17]);p.add(cal[18]);p.add(cal[19]);
    
    
            text.setEditable(false);
            exit.addActionListener(e->System.exit(0));
            about.addActionListener(e->{
                info.setVisible(true);
            });
            option.add(reset);
            option.add(exit);
            other.add(about);
            mb.add(option);
            mb.add(other);
            f.setMenuBar(mb);
            f.add(text,BorderLayout.NORTH);
            p.setLayout(new GridLayout(5,4,4,4));
            info.add(information);
            info.addWindowListener(new WindowListener());
            f.addWindowListener(new WindowListener());
            f.add(p);
            f.setSize(560,500);
            f.setVisible(true);
        }
    
        class WindowListener extends WindowAdapter {
            public void windowClosing(WindowEvent e){
                if(e.getSource() == f)
                    System.exit(0);
                else if(e.getSource() == info)
                    info.setVisible(false);
            }
        }
    
        class NumListener implements ActionListener {       //一、输入监听
            public void actionPerformed(ActionEvent e){
                if(e.getSource() == b[0]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"0");
                    else{
                        text.setText("0");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[1]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"1");
                    else{
                        text.setText("1");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[2]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"2");
                    else{
                        text.setText("2");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[3]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"3");
                    else{
                        text.setText("3");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[4]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"4");
                    else{
                        text.setText("4");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[5]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"5");
                    else{
                        text.setText("5");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[6]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"6");
                    else{
                        text.setText("6");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[7]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"7");
                    else{
                        text.setText("7");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[8]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"8");
                    else{
                        text.setText("8");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[9]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"9");
                    else{
                        text.setText("9");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[10]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"a");
                    else{
                        text.setText("a");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[11]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"b");
                    else{
                        text.setText("b");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[12]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"c");
                    else{
                        text.setText("c");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[13]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"d");
                    else{
                        text.setText("d");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[14]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"e");
                    else{
                        text.setText("e");
                        flag2 = -1;
                    }
                }
                else if(e.getSource() == b[15]){
                    if(flag2 == -1)
                        text.setText(text.getText()+"f");
                    else{
                        text.setText("f");
                        flag2 = -1;
                    }
                }
            }
        }
    
    
        class CalListener implements ActionListener {           //二、操作监听
            public void actionPerformed(ActionEvent e){
                if(e.getSource() == cal[0]){        //加法运算
                    if(flag == 0){
                        load1 = text.getText();
                        flag = 1;
                        flag2 = Key = 0;
                    }
                    else if(flag == 1){
                        load2 = text.getText();
                        if(base == 0)
                            text.setText(String.valueOf(Integer.parseInt(load1) + Integer.parseInt(load2)));
                        else if(base == 1)
                            text.setText(Integer.toHexString(Convert.hex_convert(load1) + Convert.hex_convert(load2)));
                        else
                            text.setText(Integer.toOctalString(Convert.oct_convert(load1) + Convert.oct_convert(load2)));
                        load1 = text.getText();
                        load2 = null;
                        operate = 1;
                    }
                }
                else if(e.getSource() == cal[1]){       //减法运算
                    if(flag == 0){
                        load1 = text.getText();
                        flag = 1;
                        flag2 = Key = 1;
                    }
                    else if(flag == 1){
                        load2 = text.getText();
                        if(base == 0)
                            text.setText(String.valueOf(Integer.parseInt(load1) - Integer.parseInt(load2)));
                        else if(base == 1)
                            text.setText(Integer.toHexString(Convert.hex_convert(load1) - Convert.hex_convert(load2)));
                        else
                            text.setText(Integer.toOctalString(Convert.oct_convert(load1) - Convert.oct_convert(load2)));
                        load1 = text.getText();
                        load2 = null;
                        operate = 1;
                    }
                }
                else if(e.getSource() == cal[2]){       //乘法运算
                    if(flag == 0){
                        load1 = text.getText();
                        flag = 1;
                        flag2 = Key = 2;
                    }
                    else if(flag == 1){
                        load2 = text.getText();
                        if(base == 0)
                            text.setText(String.valueOf(Integer.parseInt(load1) * Integer.parseInt(load2)));
                        else if(base == 1)
                            text.setText(Integer.toHexString(Convert.hex_convert(load1) * Convert.hex_convert(load2)));
                        else
                            text.setText(Integer.toOctalString(Convert.oct_convert(load1) * Convert.oct_convert(load2)));
                        load1 = text.getText();
                        load2 = null;
                        operate = 1;
                    }
                }
                else if(e.getSource() == cal[3]){          //除法运算
                    if(flag == 0){
                        load1 = text.getText();
                        flag = 1;
                        flag2 = Key = 3;
                    }
                    else if(flag == 1){
                        load2 = text.getText();
                        if(base == 0)
                            text.setText(String.valueOf(Integer.parseInt(load1) / Integer.parseInt(load2)));
                        else if(base == 1)
                            text.setText(Integer.toHexString(Convert.hex_convert(load1) / Convert.hex_convert(load2)));
                        else
                            text.setText(Integer.toOctalString(Convert.oct_convert(load1) / Convert.oct_convert(load2)));
                        load1 = text.getText();
                        load2 = null;
                        operate = 1;
                    }
                }
    
                else if(e.getSource() == cal[4]){           //等号
                    if(load1 != null && load2 == null){
                        load2 = text.getText();
                        if(Key == 0){           //加
                            if(base == 0)
                                text.setText(String.valueOf(Integer.parseInt(load1) + Integer.parseInt(load2)));
                            else if(base == 1)
                                text.setText(Integer.toHexString(Convert.hex_convert(load1) + Convert.hex_convert(load2)));
                            else
                                text.setText(Integer.toOctalString(Convert.oct_convert(load1) + Convert.oct_convert(load2)));
                        }
                        else if(Key == 1){      //减
                            if(base == 0)
                                text.setText(String.valueOf(Integer.parseInt(load1) - Integer.parseInt(load2)));
                            else if(base == 1)
                                text.setText(Integer.toHexString(Convert.hex_convert(load1) - Convert.hex_convert(load2)));
                            else
                                text.setText(Integer.toOctalString(Convert.oct_convert(load1) - Convert.oct_convert(load2)));
                        }
                        else if(Key == 2) {     //乘
                            if(base == 0)
                                text.setText(String.valueOf(Integer.parseInt(load1) * Integer.parseInt(load2)));
                            else if(base == 1)
                                text.setText(Integer.toHexString(Convert.hex_convert(load1) * Convert.hex_convert(load2)));
                            else
                                text.setText(Integer.toOctalString(Convert.oct_convert(load1) * Convert.oct_convert(load2)));
                        }
                        else if(Key == 3) {     //除
                            if(base == 0)
                                text.setText(String.valueOf(Integer.parseInt(load1) / Integer.parseInt(load2)));
                            else if(base == 1)
                                text.setText(Integer.toHexString(Convert.hex_convert(load1) / Convert.hex_convert(load2)));
                            else
                                text.setText(Integer.toOctalString(Convert.oct_convert(load1) / Convert.oct_convert(load2)));
                        }
                        else if(Key == 4) {      //与
                            if(base == 0)
                                text.setText(String.valueOf(Integer.parseInt(load1) & Integer.parseInt(load2)));
                            else if(base == 1)
                                text.setText(Integer.toHexString(Convert.hex_convert(load1) & Convert.hex_convert(load2)));
                            else
                                text.setText(Integer.toOctalString(Convert.oct_convert(load1) & Convert.oct_convert(load2)));
                        }
                        else if(Key == 5){      //或
                            if(base == 0)
                                text.setText(String.valueOf(Integer.parseInt(load1) | Integer.parseInt(load2)));
                            else if(base == 1)
                                text.setText(Integer.toHexString(Convert.hex_convert(load1) | Convert.hex_convert(load2)));
                            else
                                text.setText(Integer.toOctalString(Convert.oct_convert(load1) | Convert.oct_convert(load2)));
                        }
                        else if(Key == 6){      //异或
                            if(base == 0)
                                text.setText(String.valueOf(Integer.parseInt(load1) ^ Integer.parseInt(load2)));
                            else if(base == 1)
                                text.setText(Integer.toHexString(Convert.hex_convert(load1) ^ Convert.hex_convert(load2)));
                            else
                                text.setText(Integer.toOctalString(Convert.oct_convert(load1) ^ Convert.oct_convert(load2)));
                        }
                        else if(Key == 7){      //左移
                            if(base == 0)
                                text.setText(Integer.toHexString(Integer.parseInt(load1)<<Integer.parseInt(load2)));
                            else if (base == 1)
                                text.setText(Integer.toHexString(Integer.parseInt(load1,16)<<Integer.parseInt(load2)));
                            else
                                text.setText(Integer.toHexString(Integer.parseInt(load1,8)<<Integer.parseInt(load2)));
                        }
                        load1 = text.getText();
                        load2 = null;
                        Key = -1;
                        flag = 0;
                        flag2 = -1;
                        base = 0;
                        operate = 0;
                        click = 0;
                    }
                }
                else if(e.getSource() == cal[6]){           //位运算
                    if(flag == 0){
                        load1 = text.getText();
                        flag = 1;
                        flag2 = Key = 7;
                    }
                    else if(flag == 1){
                        load2 = text.getText();
                        if(base == 0)
                            text.setText(Integer.toHexString(Integer.parseInt(load1)<<Integer.parseInt(load2)));
                        else if (base == 1)
                            text.setText(Integer.toHexString(Integer.parseInt(load1,16)<<Integer.parseInt(load2)));
                        else
                            text.setText(Integer.toHexString(Integer.parseInt(load1,8)<<Integer.parseInt(load2)));
                        load1 = text.getText();
                        load2 = null;
                        operate = 2;
                    }
                }
                else if(e.getSource() == cal[14]) {         //10进制
                    load2 = text.getText();
                    if(click == 1 && operate ==0){
                        if(base == 0)
                            text.setText(load2);
                        else if (base == 1)
                            text.setText(String.valueOf(Integer.parseInt(load2,16)));
                        else
                            text.setText(String.valueOf(Integer.parseInt(load2,8)));
                        click--;
                    }
                    else
                        click++;
                    load1 = text.getText();
                    load2 = null;
                    base = 0;
                }
                else if(e.getSource() == cal[15]) {     //16进制
                    load2 = text.getText();
                    if(click == 1 && operate ==0){
                        if(base == 0)
                            text.setText(Integer.toHexString(Integer.parseInt(load2)));
                        else if (base == 1)
                            text.setText(load2);
                        else
                            text.setText(Integer.toHexString(Integer.valueOf(load2, 8)));
                        click--;
                    }
                    else
                        click++;
                    load1 = text.getText();
                    load2 = null;
                    base = 1;
                }
                else if(e.getSource() == cal[16]) {     //8进制
                    load2 = text.getText();
                    if(click == 1 && operate ==0){
                        if(base == 0)
                            text.setText(Integer.toOctalString(Integer.parseInt(load2)));
                        else if (base == 1)
                            text.setText(Integer.toOctalString(Integer.valueOf(load2, 16)));
                        else
                            text.setText(load2);
                        click--;
                    }
                    else
                        click++;
                    load1 = text.getText();
                    load2 = null;
                    base = 2;
                }
                else if(e.getSource() == cal[17]) {         //与运算
                    if(flag == 0){
                        load1 = text.getText();
                        flag = 1;
                        flag2 = Key = 4;
                    }
                    else if(flag == 1){
                        load2 = text.getText();
                        if(base == 0)
                            text.setText(String.valueOf(Integer.parseInt(load1) & Integer.parseInt(load2)));
                        else if(base == 1)
                            text.setText(Integer.toHexString(Convert.hex_convert(load1) & Convert.hex_convert(load2)));
                        else
                            text.setText(Integer.toOctalString(Convert.oct_convert(load1) & Convert.oct_convert(load2)));
                        load1 = text.getText();
                        load2 = null;
                        operate = 2;
                    }
                }
                else if(e.getSource() == cal[18]) {             //或运算
                    if(flag == 0){
                        load1 = text.getText();
                        flag = 1;
                        flag2 = Key = 5;
                    }
                    else if(flag == 1){
                        load2 = text.getText();
                        if(base == 0)
                            text.setText(String.valueOf(Integer.parseInt(load1) | Integer.parseInt(load2)));
                        else if(base == 1)
                            text.setText(Integer.toHexString(Convert.hex_convert(load1) | Convert.hex_convert(load2)));
                        else
                            text.setText(Integer.toOctalString(Convert.oct_convert(load1) | Convert.oct_convert(load2)));
                        load1 = text.getText();
                        load2 = null;
                        operate = 2;
                    }
                }
                else if(e.getSource() == cal[19]) {             //异或运算
                    if(flag == 0){
                        load1 = text.getText();
                        flag = 1;
                        flag2 = Key = 6;
                    }
                    else if(flag == 1){
                        load2 = text.getText();
                        if(base == 0)
                            text.setText(String.valueOf(Integer.parseInt(load1) ^ Integer.parseInt(load2)));
                        else if(base == 1)
                            text.setText(Integer.toHexString(Convert.hex_convert(load1) ^ Convert.hex_convert(load2)));
                        else
                            text.setText(Integer.toOctalString(Convert.oct_convert(load1) ^ Convert.oct_convert(load2)));
                        load1 = text.getText();
                        load2 = null;
                        operate = 2;
                    }
                }
            }
        }
        public static void main(String[] args){
            new Calculator().init();
        }
    }
    

    相关文章

      网友评论

        本文标题:NJUPT《JAVA程序设计》

        本文链接:https://www.haomeiwen.com/subject/zhklprtx.html