Java/JavaSwing/课程设计及答辩

是不是要课程设计了?是不是还没做完,还处在不会答辩和不懂原理的时期?救星来了!

注意,JavaSwing已经是过时的东西,无需过多研究,仅用于课程设计,希望大家都能顺利通过!


一、总体逻辑:

       1.创建窗口对象

        2.设置窗口样式

        3.创建GUI组件对象

        4.设置GUI组件样式

        5.绑定监听

        6.添加到界面中

二、解释:

        1.首先,我就猜到了某些人不懂什么叫GUI,这里解释一下,GUI,Graphical User Interface,图形用户界面,就是不单单是命令行,而是实实在在的图形界面。

        2.创建窗口对象:有两种方式,可以看看你的课程设计代码究竟是哪一种:

                第一种就是继承了JFrame。因为JFrame是JavaSwing的顶层容器,用于创建和管理容器,继承它就相当于继承了它的所有属性和方法,即成为其子类,所以会展示出来一个窗口

                第二种就是直接new JFrame,就是直接创建了一个JFrame的对象,展示出一个JFrame窗口

        3.设置窗口样式,这里就涉及很多方法,比如:

this.setTitle("这是窗口的标题");//设置标题
this.setSize(800, 600);//设置窗体大小
this.setAlwaysOnTop(true);//设置界面置顶
this.setLocationRelativeTo(null);//设置窗体居中
this.setLayout(null);//取消默认的居中放置
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置关闭窗体的时候,退出程序this.setVisible(true);//设置窗体可见

见名知意,没什么好说的,都是驼峰命名,哪个方法不懂直接查单词意思就行

        4.创建组件对象:

                比如可能用到的:

                                按钮JButton

                                输入框JTextField

                                选择框JRadioButton

                                下拉框JComboBox

                                文本框JTextArea

                要用什么就new什么即可

        5.补充一下如何使用选择框来设置是多选还是只能单选:创建出选择框JRadioButton默认就是能多选,想变成单选只需创建一个按钮组ButtonGroup,并把单选中的选项使用add方法添加进去即可

        6.设置组件样式:这个就比较多了,说几个常用的对应组件的对象的成员方法:

                        setBounds()设置组件位置,注意是以左上角为原点

                        setText()设置显示的文本

                        setFont()设置字体

                        setBackground()设置背景

        7.绑定监听:这里简单的直接使用组件对象中的addActionListener()方法即可,方法中使用Lambda表达式(箭头)或者匿名内部类进行设置。说白了就是在addActionListener()方法中创建了一个监听器对象罢了,即:

xxx.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("执行xxx操作");
            }
        });

 xxx.addActionListener(e -> {
    System.out.println("执行xxx操作");
});

三、代码附上:注意:本代码中还含有向数据库发送POST请求的扩展,所以不要照抄!仅供参考

package ui;

import util.RequestTool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

public class MainJFrame extends JFrame {
    //输入框
    JTextField textField_name = new JTextField();
    //单选框
    JRadioButton CAT = new JRadioButton();
    JRadioButton CAT_enterprise = new JRadioButton();
    //下拉框
    JComboBox<String> grade = new JComboBox<>();
    //单选框
    JRadioButton hobby_zh = new JRadioButton();
    JRadioButton hobby_en = new JRadioButton();
    //文本显示框
    JTextArea textArea_introduction = new JTextArea();
    //按钮
    JButton inZH = new JButton("中文");
    JButton inEN = new JButton("英文");

    public MainJFrame() {
        //初始化界面
        initJFrame();

        //初始化界面元素
        initView();

        //添加事件监听
        addListener();
    }

    private void addListener() {
        //事件监听
        //若姓名未填写,或专业无选择,或爱好无选择,则在文本显示框中显示提示信息
        //爱好可以多选,但专业只能选一个
        inZH.addActionListener(e -> {
            System.out.println("按下按钮“中文”");

            String nameStr = textField_name.getText();
            String majorStr = "";
            if (CAT.isSelected()) {
                majorStr = CAT.getText();
            } else if (CAT_enterprise.isSelected()) {
                majorStr = CAT_enterprise.getText();
            }
            String gradeStr = (String) grade.getSelectedItem();
            String hobbyStr = "";
            if (hobby_zh.isSelected()) {
                hobbyStr += hobby_zh.getText();
            }
            if (hobby_en.isSelected()) {
                if (hobbyStr.isEmpty()) {
                    hobbyStr += hobby_en.getText();
                } else {
                    hobbyStr += "和" + hobby_en.getText();
                }
            }
            if (nameStr.isEmpty() || majorStr.isEmpty() || hobbyStr.isEmpty()) {
                textArea_introduction.setText("请填写完整信息");
            } else {
                textArea_introduction.setText("我叫" + nameStr + ",是" + gradeStr + majorStr + "专业的学生,爱好是" + hobbyStr + "。");
                //发送请求
                System.out.println("发送请求");
                sendRequest(nameStr, majorStr, gradeStr, hobbyStr);

            }
        });

        inEN.addActionListener(e -> {
            System.out.println("按下按钮“英文”");

            String nameStr = textField_name.getText();
            String majorStr = "";
            if (CAT.isSelected()) {
                majorStr = "Computer Application Technology";
            } else if (CAT_enterprise.isSelected()) {
                majorStr = "Computer Application Technology and Enterprise";
            }
            String gradeStr = (String) grade.getSelectedItem();
            String hobbyStr = "";
            if (hobby_zh.isSelected()) {
                hobbyStr += "Chinese";
            }
            if (hobby_en.isSelected()) {
                if (hobbyStr.isEmpty()) {
                    hobbyStr += "English";
                } else
                    hobbyStr += "and English";
            }
            if (nameStr.isEmpty() || majorStr.isEmpty() || hobbyStr.isEmpty()) {
                textArea_introduction.setText("Please fill in the complete information");
            } else {
                textArea_introduction.setText("My name is " + nameStr + ", a student majoring in " + majorStr + " of " + gradeStr + ", and my hobby is " + hobbyStr + ".");
                //发送请求
                sendRequest(nameStr, majorStr, gradeStr, hobbyStr);
            }
        });
        xxx.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("执行xxx操作");
            }
        });
    }

    private void sendRequest(String name, String major, String grade, String hobby) {
        //将表单数据封装到map集合中
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("major", major);
        map.put("grade", grade);
        map.put("hobby", hobby);
        RequestTool.sendRequest("http://localhost:8080/studentInfo", "POST", map);
    }

    private void initView() {

        //添加姓名输入框
        JLabel text_nameLabel = new JLabel("姓名:");

        //添加单选框
        JLabel text_major = new JLabel("专业:");
        CAT.setText("计应");
        CAT_enterprise.setText("计应企");
        ButtonGroup group_hobby = new ButtonGroup();
        group_hobby.add(CAT);
        group_hobby.add(CAT_enterprise);

        //添加下拉框
        JLabel text_grade = new JLabel("年级:");
        grade.addItem("2021");
        grade.addItem("2022");
        grade.addItem("2023");
        grade.addItem("2024");

        //添加多选框
        JLabel text_hobby = new JLabel("爱好:");
        hobby_zh.setText("中文");
        hobby_en.setText("英文");

        //添加文本显示框
        JLabel text_introduction = new JLabel("请选择中文或英文自我介绍:");
        textArea_introduction.setFont(new java.awt.Font("Dialog", Font.PLAIN, 16));
        textArea_introduction.setEditable(false);
        textArea_introduction.setLineWrap(true);
        textArea_introduction.setWrapStyleWord(true);

        //设置位置
        text_nameLabel.setBounds(100, 100, 50, 20);
        textField_name.setBounds(145, 94, 200, 30);
        text_major.setBounds(400, 100, 50, 20);
        CAT.setBounds(450, 100, 60, 20);
        CAT_enterprise.setBounds(510, 100, 100, 20);
        text_grade.setBounds(100, 150, 50, 20);
        grade.setBounds(155, 144, 100, 30);
        text_hobby.setBounds(400, 150, 50, 20);
        hobby_zh.setBounds(450, 150, 60, 20);
        hobby_en.setBounds(510, 150, 60, 20);
        text_introduction.setBounds(100, 250, 400, 20);
        textArea_introduction.setBounds(350, 200, 300, 200);
        inZH.setBounds(250, 450, 100, 30);
        inEN.setBounds(400, 450, 100, 30);

        //设置字体
        Font font = new Font("Dialog", Font.BOLD, 16);
        text_nameLabel.setFont(font);
        text_major.setFont(font);
        text_grade.setFont(font);
        text_hobby.setFont(font);
        text_introduction.setFont(font);
        textField_name.setFont(font);
        CAT.setFont(font);
        CAT_enterprise.setFont(font);
        grade.setFont(font);
        hobby_zh.setFont(font);
        hobby_en.setFont(font);

        //添加到界面中
        this.getContentPane().add(text_nameLabel);
        this.getContentPane().add(textField_name);
        this.getContentPane().add(text_major);
        this.getContentPane().add(CAT);
        this.getContentPane().add(CAT_enterprise);
        this.getContentPane().add(text_grade);
        this.getContentPane().add(grade);
        this.getContentPane().add(text_hobby);
        this.getContentPane().add(hobby_zh);
        this.getContentPane().add(hobby_en);
        this.getContentPane().add(text_introduction);
        this.getContentPane().add(textArea_introduction);
        this.getContentPane().add(textArea_introduction);
        this.getContentPane().add(inZH);
        this.getContentPane().add(inEN);
    }

    private void initJFrame() {
        this.setTitle("xxx");//设置标题
        this.setSize(800, 600);//设置窗体大小
        this.setAlwaysOnTop(true);//设置界面置顶
        this.setLocationRelativeTo(null);//设置窗体居中
        this.setLayout(null);//取消默认的居中放置
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置关闭窗体的时候,退出程序
        this.setVisible(true);//设置窗体可见
    }
}

  • 27
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值