【课后练习分享】Java用户注册界面设计和求三角形面积的图形界面程序

目录

java编程题(每日一练):

问题一的答案代码如下:

问题一的运行截图如下:

问题二的答案代码如下:

问题二的运行截图如下:


java编程题(每日一练):

1.设计一个用户注册的界面,包括用户名、密码输入框,性别单选按钮,表示爱好的 篮球、足球、乒乓球复选框,表示学历的下拉列表框,"注册"按钮和"取消"按钮。

2.设计一个求三角形面积的图形界面程序,要求通过3个输入框输入3个边长,当单 击"计算"按钮时,能够计算以输入的3个边长所形成的三角形的面积。要求能够判断输 入的3个边长是否是数字,三个边长是否能够构成合法的三角形

问题一的答案代码如下:

Java 用户注册界面设计

java 复制代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("User Registration");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setLayout(new GridLayout(6, 2));

        JLabel nameLabel = new JLabel("Username:");
        JTextField nameField = new JTextField();
        JLabel passwordLabel = new JLabel("Password:");
        JPasswordField passwordField = new JPasswordField();

        JLabel genderLabel = new JLabel("Gender:");
        JRadioButton maleButton = new JRadioButton("Male");
        JRadioButton femaleButton = new JRadioButton("Female");
        ButtonGroup genderGroup = new ButtonGroup();
        genderGroup.add(maleButton);
        genderGroup.add(femaleButton);
        JPanel genderPanel = new JPanel();
        genderPanel.add(maleButton);
        genderPanel.add(femaleButton);

        JLabel hobbyLabel = new JLabel("Hobbies:");
        JCheckBox basketballBox = new JCheckBox("Basketball");
        JCheckBox footballBox = new JCheckBox("Football");
        JCheckBox pingpongBox = new JCheckBox("Ping Pong");
        JPanel hobbyPanel = new JPanel();
        hobbyPanel.add(basketballBox);
        hobbyPanel.add(footballBox);
        hobbyPanel.add(pingpongBox);

        JLabel educationLabel = new JLabel("Education:");
        String[] educationLevels = {"High School", "Associate", "Bachelor", "Master", "Doctor"};
        JComboBox<String> educationComboBox = new JComboBox<>(educationLevels);

        JButton registerButton = new JButton("Register");
        JButton cancelButton = new JButton("Cancel");

        frame.add(nameLabel);
        frame.add(nameField);
        frame.add(passwordLabel);
        frame.add(passwordField);
        frame.add(genderLabel);
        frame.add(genderPanel);
        frame.add(hobbyLabel);
        frame.add(hobbyPanel);
        frame.add(educationLabel);
        frame.add(educationComboBox);
        frame.add(registerButton);
        frame.add(cancelButton);

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = nameField.getText();
                String password = new String(passwordField.getPassword());
                String gender = maleButton.isSelected() ? "Male" : "Female";
                String hobbies = "";
                if (basketballBox.isSelected()) hobbies += "Basketball ";
                if (footballBox.isSelected()) hobbies += "Football ";
                if (pingpongBox.isSelected()) hobbies += "Ping Pong ";
                String education = (String) educationComboBox.getSelectedItem();
                JOptionPane.showMessageDialog(frame, "Username: " + username + "\nPassword: " + password + "\nGender: " + gender + "\nHobbies: " + hobbies + "\nEducation: " + education);
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                nameField.setText("");
                passwordField.setText("");
                genderGroup.clearSelection();
                basketballBox.setSelected(false);
                footballBox.setSelected(false);
                pingpongBox.setSelected(false);
                educationComboBox.setSelectedIndex(0);
            }
        });

        frame.setVisible(true);
    }
}

问题一的运行截图如下:

问题二的答案代码如下:

求三角形面积的图形界面程序

java 复制代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TriangleAreaCalculator {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Triangle Area Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 200);
        frame.setLayout(new GridLayout(5, 2));

        JLabel sideALabel = new JLabel("Side A:");
        JTextField sideAField = new JTextField();
        JLabel sideBLabel = new JLabel("Side B:");
        JTextField sideBField = new JTextField();
        JLabel sideCLabel = new JLabel("Side C:");
        JTextField sideCField = new JTextField();

        JButton calculateButton = new JButton("Calculate");
        JLabel resultLabel = new JLabel("Area:");

        frame.add(sideALabel);
        frame.add(sideAField);
        frame.add(sideBLabel);
        frame.add(sideBField);
        frame.add(sideCLabel);
        frame.add(sideCField);
        frame.add(calculateButton);
        frame.add(resultLabel);

        calculateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    double a = Double.parseDouble(sideAField.getText());
                    double b = Double.parseDouble(sideBField.getText());
                    double c = Double.parseDouble(sideCField.getText());

                    if (isValidTriangle(a, b, c)) {
                        double s = (a + b + c) / 2;
                        double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
                        resultLabel.setText("Area: " + area);
                    } else {
                        resultLabel.setText("Invalid Triangle");
                    }
                } catch (NumberFormatException ex) {
                    resultLabel.setText("Please enter valid numbers");
                }
            }
        });

        frame.setVisible(true);
    }

    public static boolean isValidTriangle(double a, double b, double c) {
        return a + b > c && a + c > b && b + c > a;
    }
}

问题二的运行截图如下:

这两个程序分别实现了用户注册和三角形面积计算的图形界面。第一个程序包含用户名、密码输入框,性别单选按钮,爱好复选框,学历下拉列表框,以及注册和取消按钮。第二个程序通过三个输入框输入三角形的三条边长,并在点击"计算"按钮时计算三角形的面积,并判断输入的边长是否合法。

相关推荐
海鸥-w2 小时前
什么是AOP
java
Cc.Y2 小时前
Java零基础入门:可变字符串与包装类:StringBuilder、StringBuffer 与 通讯录管理系统实战
java·开发语言·python
paopaokaka_luck3 小时前
基于springboot+vue的智慧家政管理系统(AI问答、 WebSocket实时沟通、ECharts运营分析、订单规则与服务追踪)
java·javascript·spring boot·数据分析·echarts·mybatis
sm_926787053 小时前
RFID 标签打印的技术实现要点与二次开发实践
java·大数据·前端·c++·编辑器
阿森_dev3 小时前
2027 届毕设选题怎么选:30 个 Java 方向选题的实现难度排序
java·开发语言·spring boot·毕业设计·课程设计
by209993 小时前
C++ 多态:同一个基类接口,调用不同的函数
开发语言·c++·经验分享·多态
笔墨登场说说3 小时前
flink bin/start-cluster.sh 帮我做成开机启动
开发语言·python
Sarvartha3 小时前
Lambda 表达式和函数式接口
java·开发语言
计算机毕业设计杰瑞3 小时前
【计算机毕设精品】基于SpringBoot的一体化教务与学业质量管理系统的设计与实现,附源码_增删改查_java项目_可完美运行部署_网站项目
java·spring boot·课程设计
小白学大数据3 小时前
拼多多反爬对抗实战:Scrapy 中间件化采集架构解析
开发语言·scrapy·中间件·架构