用Java编写程序,实现收集学生喜欢的食物功能:输入姓名,性别在男和女中单选一个,以下拉选择方式在计算机系,电气系和外国语系中选择一个院系,再在主食,菜肴和汤羹中勾选一个或多个喜欢的食物(各生成5个不同的)。点击“确定”,在控制台显示上述信息;点击“取消”,清除选项。
时间: 2023-06-20 14:03:26 浏览: 227
这是一个比较完整的用户交互程序,需要使用Java图形用户界面(GUI)库来实现。下面我将提供一个基本的实现思路和代码示例,你可以根据需要进行修改和扩展。
首先,我们需要创建一个窗口和一些控件,如标签、文本框、单选框、下拉列表和复选框。可以使用Swing库来实现。以下是一个基本的窗口和控件示例:
```
import javax.swing.*;
import java.awt.*;
public class FoodPreferenceGUI extends JFrame {
private JLabel nameLabel, genderLabel, deptLabel, foodLabel;
private JTextField nameField;
private JRadioButton maleButton, femaleButton;
private JComboBox<String> deptBox;
private JCheckBox riceBox, noodleBox, dumplingBox, beefBox, porkBox, vegetableBox, soupBox, porridgeBox, hotpotBox, tofuBox;
private JButton okButton, cancelButton;
public FoodPreferenceGUI() {
// 设置窗口标题和大小
super("学生食物喜好收集");
setSize(400, 400);
// 创建标签和文本框
nameLabel = new JLabel("姓名:");
genderLabel = new JLabel("性别:");
deptLabel = new JLabel("院系:");
foodLabel = new JLabel("喜欢的食物:");
nameField = new JTextField(20);
// 创建单选框
maleButton = new JRadioButton("男");
femaleButton = new JRadioButton("女");
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(maleButton);
genderGroup.add(femaleButton);
// 创建下拉列表
String[] deptList = {"计算机系", "电气系", "外国语系"};
deptBox = new JComboBox<>(deptList);
// 创建复选框
riceBox = new JCheckBox("米饭");
noodleBox = new JCheckBox("面条");
dumplingBox = new JCheckBox("饺子");
beefBox = new JCheckBox("牛肉");
porkBox = new JCheckBox("猪肉");
vegetableBox = new JCheckBox("蔬菜");
soupBox = new JCheckBox("汤羹");
porridgeBox = new JCheckBox("粥");
hotpotBox = new JCheckBox("火锅");
tofuBox = new JCheckBox("豆腐");
// 创建按钮
okButton = new JButton("确定");
cancelButton = new JButton("取消");
// 添加控件到窗口中
JPanel panel = new JPanel(new GridLayout(6, 2));
panel.add(nameLabel);
panel.add(nameField);
panel.add(genderLabel);
panel.add(maleButton);
panel.add(new JLabel());
panel.add(femaleButton);
panel.add(deptLabel);
panel.add(deptBox);
panel.add(foodLabel);
panel.add(riceBox);
panel.add(noodleBox);
panel.add(dumplingBox);
panel.add(beefBox);
panel.add(porkBox);
panel.add(vegetableBox);
panel.add(soupBox);
panel.add(porridgeBox);
panel.add(hotpotBox);
panel.add(tofuBox);
panel.add(okButton);
panel.add(cancelButton);
add(panel);
// 添加事件处理器
okButton.addActionListener(e -> {
String name = nameField.getText();
String gender = maleButton.isSelected() ? "男" : "女";
String dept = (String) deptBox.getSelectedItem();
String food = "";
if (riceBox.isSelected()) food += "米饭 ";
if (noodleBox.isSelected()) food += "面条 ";
if (dumplingBox.isSelected()) food += "饺子 ";
if (beefBox.isSelected()) food += "牛肉 ";
if (porkBox.isSelected()) food += "猪肉 ";
if (vegetableBox.isSelected()) food += "蔬菜 ";
if (soupBox.isSelected()) food += "汤羹 ";
if (porridgeBox.isSelected()) food += "粥 ";
if (hotpotBox.isSelected()) food += "火锅 ";
if (tofuBox.isSelected()) food += "豆腐 ";
JOptionPane.showMessageDialog(this, name + " " + gender + " " + dept + " " + food);
});
cancelButton.addActionListener(e -> {
nameField.setText("");
maleButton.setSelected(true);
deptBox.setSelectedIndex(0);
riceBox.setSelected(false);
noodleBox.setSelected(false);
dumplingBox.setSelected(false);
beefBox.setSelected(false);
porkBox.setSelected(false);
vegetableBox.setSelected(false);
soupBox.setSelected(false);
porridgeBox.setSelected(false);
hotpotBox.setSelected(false);
tofuBox.setSelected(false);
});
// 设置窗口可见性和关闭方式
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new FoodPreferenceGUI();
}
}
```
上面的代码创建了一个窗口,包括姓名、性别、院系和食物四个部分。姓名和性别使用文本框和单选框,院系使用下拉列表,食物使用复选框。确定按钮将收集所有用户输入的信息,并在对话框中显示,取消按钮将清除所有选项。
你可以根据需要修改控件的属性、布局和事件处理器来实现自己的需求。例如,可以添加输入验证、数据存储和网络传输等功能。
阅读全文