Java图形化界面&&swing

Java图形化界面&&swing,第1张

Java图形化界面&&swing

Java程序设计
大作业课程报告
实现加减乘除功能:

功能模块及详细说明

程序模块流程


import javax.swing.*;
        import javax.swing.Jframe;
        import java.awt.*;
        import java.awt.event.*;
public class Main {

    Jframe step3 = new Jframe("图形化计算器");     //显示界面标题
    JButton button1 = new JButton("+");       //创建一个按钮
    JButton button2 = new JButton("-");        //创建一个按钮
    JButton button3 = new JButton("全部清零");    //创建一个按钮
    JButton button4 = new JButton("*");        //创建一个按钮
    JButton button5 = new JButton("/");        //创建一个按钮
    JLabel text1 = new JLabel("运算数一");          //创建一个文本
    JLabel text2 = new JLabel("运算数二");          //创建一个文本
    JLabel text3 = new JLabel("运算结果");          //创建一个文本
    JLabel text = new JLabel("图形化计算器");          //创建一个文本
    JTextField jet1 = new JTextField(30);       //创建一个输入框
    JTextField jet2 = new JTextField(30);       //创建一个输入框
    JTextField jet3 = new JTextField(30);       //创建一个输入框
    public Main () {

        step3.setLayout(null);                          //清空布局管理器
        step3.setSize(300,350);            //设置系统框的大小
        button1.setSize(60,30);            //设置按钮的大小
        button2.setSize(60,30);            //设置按钮的大小
        button4.setSize(60,30);            //设置按钮的大小
        button5.setSize(60,30);            //设置按钮的大小
        button3.setSize(100,30);           //设置按钮的大小
        
        
        button1.setLocation(70,200);              //设置按钮的位置(相对与系统框的左上角)
        button2.setLocation(150,200);             //设置按钮的位置(相对与系统框的左上角)
        button4.setLocation(70,250);             //设置按钮的位置(相对与系统框的左上角)
        button5.setLocation(150,250);             //设置按钮的位置(相对与系统框的左上角)
        button3.setLocation(85,280);              //设置按钮的位置(相对与系统框的左上角)
        
        
        button1.addActionListener(new event1(jet1,jet2,jet3));       //添加事件监听器(为后面创建响应做准备)
        button2.addActionListener(new event2(jet1,jet2,jet3));       //添加事件监听器(为后面创建响应做准备)
        button4.addActionListener(new event4(jet1,jet2,jet3));       //添加事件监听器(为后面创建响应做准备)
        button5.addActionListener(new event5(jet1,jet2,jet3));       //添加事件监听器(为后面创建响应做准备)
        button3.addActionListener(new event3(jet1,jet2,jet3));       //添加事件监听器(为后面创建响应做准备)
        
        
        text1.setSize(70,30);
        text2.setSize(70,30);
        text3.setSize(70,30);
        text.setSize(160,30);
        text.setFont(new Font("宋体",Font.PLAIN,20));     //设置字体格式
        text1.setLocation(30,50);
        text2.setLocation(30,100);
        text3.setLocation(30,150);
        text.setLocation(85,10);
        jet1.setSize(90,30);
        jet2.setSize(90,30);
        jet3.setSize(90,30);
        jet1.setLocation(150,50);
        jet2.setLocation(150,100);
        jet3.setLocation(150,150);
        jet3.setEditable(false);
        step3.add(jet1);step3.add(jet2);step3.add(jet3);                        //将以上内容添加到step3中
        step3.add(text1);step3.add(text2);step3.add(text3);step3.add(text);     //将以上内容添加到step3中
        step3.add(button1);step3.add(button2);step3.add(button3); step3.add(button4);step3.add(button5);              //将以上内容添加到step3中
        step3.setResizable(false);                  //表示不可以由用户自定义窗口大小
//        step3.setLocation(600,100);
        step3.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);       //设置默认的关闭 *** 作,”在关闭动作时退出“
        step3.setVisible(true);                     //将其改为可见
    }

    

    public  boolean num(String n)
    {

        int count=0;
        char ch =n.charAt(0);
        if (ch == '-' || ch >= '0' && ch <= '9')
        {

            for (int i = 1; i < n.length(); i++)
            {

                ch = n.charAt(i);
                if (Character.isDigit(ch) && ch != '.')
                    return false;
                if (ch == '.')
                    count++;
            }
            return count < 2 && ch != '.';
        }
        return false;
    }

    

    class event1 implements ActionListener{

        JTextField num1=null;
        JTextField num2=null;
        JTextField num3=null;
        public event1(JTextField jet1,JTextField jet2,JTextField jet3)
        {

            num1=jet1;
            num2=jet2;
            num3=jet3;
        }
        public void actionPerformed(ActionEvent actionEvent) {

            String st1=num1.getText();
            String st2=num2.getText();
            if (st1.equals(" ") || st2.equals(" "))
                JOptionPane.showMessageDialog(null,"请输入数字");
            else if(!num(st1) || !num(st2))
                JOptionPane.showMessageDialog(null,"请输入数字");
            else
            {

                double a = Double.parseDouble(st1);
                double b = Double.parseDouble(st2);
                num3.setText(Double.toString(a+b));
            }
        }

    }

    

    class event2 implements ActionListener{

        JTextField num1=null;
        JTextField num2=null;
        JTextField num3=null;
        public event2(JTextField jet1,JTextField jet2,JTextField jet3)
        {

            num1=jet1;
            num2=jet2;
            num3=jet3;
        }
        public void actionPerformed(ActionEvent actionEvent) {

            String st1=num1.getText();
            String st2=num2.getText();
            if (st1.equals(" ") || st2.equals(" "))
                JOptionPane.showMessageDialog(null,"请输入数字");
            else if(!num(st1) || !num(st2))
                JOptionPane.showMessageDialog(null,"请输入数字");
            else
            {

                double a = Double.parseDouble(st1);
                double b = Double.parseDouble(st2);
                jet3.setText(Double.toString(a-b));
            }
        }


    }
    

    class event4 implements ActionListener{

        JTextField num1=null;
        JTextField num2=null;
        JTextField num3=null;
        public event4(JTextField jet1,JTextField jet2,JTextField jet3)
        {

            num1=jet1;
            num2=jet2;
            num3=jet3;
        }
        public void actionPerformed(ActionEvent actionEvent) {

            String st1=num1.getText();
            String st2=num2.getText();
            if (st1.equals(" ") || st2.equals(" "))
                JOptionPane.showMessageDialog(null,"请输入数字");
            else if(!num(st1) || !num(st2))
                JOptionPane.showMessageDialog(null,"请输入数字");
            else
            {

                double a = Double.parseDouble(st1);
                double b = Double.parseDouble(st2);
                jet3.setText(Double.toString(a*b));
            }
        }


    }

    
    

    class event5 implements ActionListener{

        JTextField num1=null;
        JTextField num2=null;
        JTextField num3=null;
        public event5(JTextField jet1,JTextField jet2,JTextField jet3)
        {

            num1=jet1;
            num2=jet2;
            num3=jet3;
        }
        public void actionPerformed(ActionEvent actionEvent) {

            String st1=num1.getText();
            String st2=num2.getText();
            if (st1.equals(" ") || st2.equals(" "))
                JOptionPane.showMessageDialog(null,"请输入数字");
            else if(!num(st1) || !num(st2))
                JOptionPane.showMessageDialog(null,"请输入数字");
            else if(st2.equals("0"))
                JOptionPane.showMessageDialog(null,"除数不能为0!");
            else
            {

                double a = Double.parseDouble(st1);
                double b = Double.parseDouble(st2);
                jet3.setText(Double.toString(a/b));
            }
        }


    }

    

    class event3 implements ActionListener {

        JTextField num1 = null;
        JTextField num2 = null;
        JTextField num3 = null;

        public event3(JTextField jet1, JTextField jet2, JTextField jet3) {

            num1 = jet1;
            num2 = jet2;
            num3 = jet3;
        }

        public void actionPerformed(ActionEvent actionEvent) {

            num1.setText("");
            num2.setText("");
            num3.setText("");
        }

    }

    public static void main(String[] args) {

        new Main();//创建对象
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5685192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存