feat(experiment_5): 面向对象课程,实现了一个体育比赛报名系统的基本功能- 添加了 SportRegister 类,包含姓名、学号输入框,性别选择按钮,参赛项目选择框和操作按钮

- 实现了性别选择后更新参赛项目列表的功能- 添加了重置和提交按钮,但尚未实现具体逻辑
- 创建了 task 类作为程序入口,用于启动报名系统界面
This commit is contained in:
dev_xulongjin 2025-05-13 11:09:46 +08:00
parent 1a1d4b52f4
commit ab7ec43d97
3 changed files with 376 additions and 0 deletions

View File

@ -0,0 +1,240 @@
package cn.vscoder.experiment_5;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/*
* Created by JFormDesigner on Mon May 12 16:02:01 CST 2025
*/
/**
* @author xulongjin
*/
public class SportRegister extends JFrame {
public SportRegister() {
initComponents();
}
private void selectSexBoyItemStateChanged(ItemEvent e) {
// TODO add your code here
selectCompetitiveItem.removeAllItems();
selectCompetitiveItem.addItem("男子跳高");
selectCompetitiveItem.addItem("男子跳远");
selectCompetitiveItem.addItem("男子铅球");
selectCompetitiveItem.addItem("男子100米");
selectCompetitiveItem.addItem("男子400米");
selectCompetitiveItem.addItem("男子自由泳");
}
private void selectSexGirlItemStateChanged(ItemEvent e) {
// TODO add your code here
selectCompetitiveItem.removeAllItems();
selectCompetitiveItem.addItem("女子跳高");
selectCompetitiveItem.addItem("女子跳远");
selectCompetitiveItem.addItem("女子铅球");
selectCompetitiveItem.addItem("女子100米");
selectCompetitiveItem.addItem("女子400米");
selectCompetitiveItem.addItem("女子自由泳");
}
private void bottomReset(ActionEvent e) {
// TODO add your code here
}
private void bottomPush(ActionEvent e) {
// TODO add your code here
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
panel1 = new JPanel();
textName = new JLabel();
textSn = new JLabel();
inputName = new JTextField();
inputSn = new JTextField();
panel2 = new JPanel();
selectSexBoy = new JRadioButton();
selectSexGirl = new JRadioButton();
panel3 = new JPanel();
selectCompetitiveItem = new JComboBox<>();
panel4 = new JPanel();
bottomReset = new JButton();
bottomPush = new JButton();
scrollPane1 = new JScrollPane();
textArea1 = new JTextArea();
//======== this ========
var contentPane = getContentPane();
contentPane.setLayout(new GridLayout(5, 0));
//======== panel1 ========
{
panel1.setBorder(new TitledBorder("\u57fa\u7840\u4fe1\u606f"));
panel1.setLayout(null);
//---- textName ----
textName.setText("\u59d3\u540d\uff1a");
panel1.add(textName);
textName.setBounds(25, 32, 40, 34);
//---- textSn ----
textSn.setText("\u5b66\u53f7\uff1a");
panel1.add(textSn);
textSn.setBounds(205, 30, 40, 34);
panel1.add(inputName);
inputName.setBounds(65, 30, 125, 35);
panel1.add(inputSn);
inputSn.setBounds(250, 30, 125, 35);
{
// compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < panel1.getComponentCount(); i++) {
Rectangle bounds = panel1.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = panel1.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
panel1.setMinimumSize(preferredSize);
panel1.setPreferredSize(preferredSize);
}
}
contentPane.add(panel1);
//======== panel2 ========
{
panel2.setBorder(new TitledBorder("\u9009\u62e9\u6027\u522b"));
panel2.setLayout(null);
//---- selectSexBoy ----
selectSexBoy.setText("\u7537");
selectSexBoy.setSelected(true);
selectSexBoy.addItemListener(e -> selectSexBoyItemStateChanged(e));
panel2.add(selectSexBoy);
selectSexBoy.setBounds(115, 35, 55, 30);
//---- selectSexGirl ----
selectSexGirl.setText("\u5973");
selectSexGirl.addItemListener(e -> selectSexGirlItemStateChanged(e));
panel2.add(selectSexGirl);
selectSexGirl.setBounds(205, 35, 55, 30);
{
// compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < panel2.getComponentCount(); i++) {
Rectangle bounds = panel2.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = panel2.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
panel2.setMinimumSize(preferredSize);
panel2.setPreferredSize(preferredSize);
}
}
contentPane.add(panel2);
//======== panel3 ========
{
panel3.setBorder(new TitledBorder("\u9009\u62e9\u53c2\u8d5b\u9879\u76ee"));
panel3.setLayout(null);
//---- selectCompetitiveItem ----
selectCompetitiveItem.setModel(new DefaultComboBoxModel<>(new String[] {
"\u53c2\u8d5b\u9879",
"\u8df3\u9ad8",
"\u8df3\u8fdc",
"100\u7c73",
"400\u7c73",
"\u94c5\u7403"
}));
panel3.add(selectCompetitiveItem);
selectCompetitiveItem.setBounds(115, 30, 140, 40);
{
// compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < panel3.getComponentCount(); i++) {
Rectangle bounds = panel3.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = panel3.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
panel3.setMinimumSize(preferredSize);
panel3.setPreferredSize(preferredSize);
}
}
contentPane.add(panel3);
//======== panel4 ========
{
panel4.setBorder(new TitledBorder("\u64cd\u4f5c"));
panel4.setLayout(null);
//---- bottomReset ----
bottomReset.setText("\u91cd\u7f6e");
bottomReset.addActionListener(e -> bottomReset(e));
panel4.add(bottomReset);
bottomReset.setBounds(new Rectangle(new Point(95, 40), bottomReset.getPreferredSize()));
//---- bottomPush ----
bottomPush.setText("\u63d0\u4ea4");
bottomPush.addActionListener(e -> bottomPush(e));
panel4.add(bottomPush);
bottomPush.setBounds(210, 40, 78, 34);
{
// compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < panel4.getComponentCount(); i++) {
Rectangle bounds = panel4.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = panel4.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
panel4.setMinimumSize(preferredSize);
panel4.setPreferredSize(preferredSize);
}
}
contentPane.add(panel4);
//======== scrollPane1 ========
{
scrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane1.setBorder(new TitledBorder("\u62a5\u540d\u4fe1\u606f"));
scrollPane1.setViewportView(textArea1);
}
contentPane.add(scrollPane1);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off
private JPanel panel1;
private JLabel textName;
private JLabel textSn;
private JTextField inputName;
private JTextField inputSn;
private JPanel panel2;
private JRadioButton selectSexBoy;
private JRadioButton selectSexGirl;
private JPanel panel3;
private JComboBox<String> selectCompetitiveItem;
private JPanel panel4;
private JButton bottomReset;
private JButton bottomPush;
private JScrollPane scrollPane1;
private JTextArea textArea1;
// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
}

View File

@ -0,0 +1,129 @@
JFDML JFormDesigner: "8.2.4.0.393" Java: "21.0.6" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
root: new FormRoot {
add( new FormWindow( "javax.swing.JFrame", new FormLayoutManager( class java.awt.GridLayout ) {
"rows": 5
} ) {
name: "this"
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class com.jformdesigner.runtime.NullLayout ) ) {
name: "panel1"
"border": new javax.swing.border.TitledBorder( "基础信息" )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "textName"
"text": "姓名:"
}, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) {
"x": 25
"y": 32
"width": 40
"height": 34
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "textSn"
"text": "学号:"
}, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) {
"width": 40
"height": 34
"x": 205
"y": 30
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "inputName"
}, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) {
"x": 65
"y": 30
"width": 125
"height": 35
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "inputSn"
}, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) {
"width": 125
"x": 250
"y": 30
"height": 35
} )
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class com.jformdesigner.runtime.NullLayout ) ) {
name: "panel2"
"border": new javax.swing.border.TitledBorder( "选择性别" )
add( new FormComponent( "javax.swing.JRadioButton" ) {
name: "selectSexBoy"
"text": "男"
"selected": true
addEvent( new FormEvent( "java.awt.event.ItemListener", "itemStateChanged", "selectSexBoyItemStateChanged", true ) )
}, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) {
"x": 115
"y": 35
"width": 55
"height": 30
} )
add( new FormComponent( "javax.swing.JRadioButton" ) {
name: "selectSexGirl"
"text": "女"
addEvent( new FormEvent( "java.awt.event.ItemListener", "itemStateChanged", "selectSexGirlItemStateChanged", true ) )
}, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) {
"width": 55
"height": 30
"x": 205
"y": 35
} )
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class com.jformdesigner.runtime.NullLayout ) ) {
name: "panel3"
"border": new javax.swing.border.TitledBorder( "选择参赛项目" )
add( new FormComponent( "javax.swing.JComboBox" ) {
name: "selectCompetitiveItem"
"model": new javax.swing.DefaultComboBoxModel {
selectedItem: "参赛项"
addElement( "参赛项" )
addElement( "跳高" )
addElement( "跳远" )
addElement( "100米" )
addElement( "400米" )
addElement( "铅球" )
}
}, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) {
"x": 115
"y": 30
"width": 140
"height": 40
} )
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class com.jformdesigner.runtime.NullLayout ) ) {
name: "panel4"
"border": new javax.swing.border.TitledBorder( "操作" )
add( new FormComponent( "javax.swing.JButton" ) {
name: "bottomReset"
"text": "重置"
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "bottomReset", true ) )
}, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) {
"x": 95
"y": 40
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "bottomPush"
"text": "提交"
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "bottomPush", true ) )
}, new FormLayoutConstraints( class com.jformdesigner.runtime.NullConstraints ) {
"width": 78
"height": 34
"x": 210
"y": 40
} )
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane1"
"horizontalScrollBarPolicy": 32
"border": new javax.swing.border.TitledBorder( "报名信息" )
add( new FormComponent( "javax.swing.JTextArea" ) {
name: "textArea1"
} )
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 425, 565 )
} )
}
}

View File

@ -0,0 +1,7 @@
package cn.vscoder.experiment_5;
public class task {
public static void main(String[] args) {
new SportRegister().setVisible(true);
}
}