我正在尝试将 JComboBox 添加到我的学生计划中,并提供以下选项:新生、大二、大三或大四。选择高级选项时,它应该输出类似“学生已在校学习四年”或初级选项“学生已在校学习三年”的内容。等等...我搜索了论坛并做了一些谷歌搜索,但我无法提供与我在这里尝试做的类似的例子。我将在下面发布代码。任何帮助将不胜感激。谢谢!
学生类(class):
public class Student {
protected String name;
protected String address;
protected double balance;
protected String major;
// Constructs fields
public Student(String name, String address, String major, double balance) {
this.name = name;
this.address = address;
this.major = major;
this.balance = balance;
}
public String setName() {
return name;
}
public String setAddress() {
return address;
}
public double setBalance() {
return balance;
}
public String setMajor() {
return major;
}
public String setStudentInformation() {
return "\n\tName: " + name + "\n\tAddress: " + address + "\n\tMajor: " + major + "\n\tBalance: " + balance;
}
public String toString() {
return setStudentInformation();
}
}
经理类:
public class Manager {
private static Manager thisManager;
public Student[] students = new Student[50];
public int studentCounter = 0;
public GradStudent[] gradStudents = new GradStudent[50];
public int gradCounter = 0;
// Private Constructor (Singleton instance method)
public Manager() {
}
// Instance method (Singleton)
public static Manager instance() {
if (thisManager == null) {
return thisManager = new Manager();
} else {
return thisManager;
}
}
public Student createStudent(String name, String address, String major,
double amount) {
Student c1 = new Student(name, address, major, amount);
storeStudent(c1);
return c1;
}
private void storeStudent(Student c1) {
students[studentCounter++] = c1;
}
public GradStudent createGradStudent(String name, String address,
String major, double amount) {
GradStudent e1 = new GradStudent(name, address, major, amount);
storeGradStudent(e1);
return e1;
}
private void storeGradStudent(GradStudent g1) {
gradStudents[gradCounter++] = g1;
}
public String listStudents() {
String temp = "\n\nStudent List\n";
for (int i = 0; i < studentCounter; i++) {
temp += "Student: " + students[i].setName() + "\n";
}
return temp;
}
public String listGrads() {
String temp = "\n\nGraduate Student List\n";
for (int i = 0; i < gradCounter; i++) {
temp += "Graduate Student: " + gradStudents[i].setName() + "\n";
}
return temp;
}
public String aveBalance() {
String temp = "\n\nStudent Average Balance: ";
double sum = 0;
for (int i = 0; i < studentCounter; i++) {
sum += students[i].setBalance();
}
for (int i = 0; i < gradCounter; i++) {
sum += gradStudents[i].setBalance();
}
return temp + sum / (studentCounter + gradCounter) + "\n";
}
public String listCsci() {
String temp = "\n\nComputer Science Student List\n";
for (int i = 0; i < studentCounter; i++) {
if (students[i].setMajor().equals("Computer Science")) {
temp += "Student: " + students[i].setName() + "\n";
}
}
for (int i = 0; i < gradCounter; i++) {
if (gradStudents[i].setMajor().equals("Computer Science")) {
temp += "Graduate Student: " + gradStudents[i].setName() + "\n";
}
}
return temp;
}
}
GUI 类:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Enumeration;
public class GUI extends JFrame implements ActionListener {
// RadioButtons & ButtonGroup
private JRadioButton jrbStudent = new JRadioButton("Student");
private JRadioButton jrbGraduate = new JRadioButton("Graduate Student");
private ButtonGroup group = new ButtonGroup();
// JTextFields
private JTextField name = new JTextField(20);
private JTextField address = new JTextField(20);
private JTextField balance = new JTextField(20);
private JTextField major = new JTextField(20);
// Submit Button
private JButton jbtSubmit = new JButton("Submit");
// echoStudent output area
private JTextArea echoStudent = new JTextArea(5, 20);
// Specific Buttons
private JButton printStudentNames = new JButton("Print Student's Names");
private JButton printGradStudentNames = new JButton(
"Print Graduate Student's Names");
private JButton calcBalance = new JButton(
"Calculate Average Balance of All Students");
private JButton compSciMajor = new JButton(
"Displays Computer Science Major Students");
// ScrollPane
private JScrollPane scrollPane = new JScrollPane(echoStudent);
// Fill and create JComboBox
private String[] studentYear = { "Freshman", "Sophomore", "Junior", "Senior" };
private JComboBox<String> jcbo = new JComboBox<String>(studentYear);
public GUI() {
super("College Student Information Interface");
// Creates Panels
JPanel bottomPanel = new JPanel();
JPanel topLeftPanel = new JPanel();
JPanel topRightPanel = new JPanel();
// Adds Labels, Fields, and Buttons to the topRightPanel
topRightPanel.setLayout(new GridLayout(7, 2, 5, 5));
topRightPanel.add(new JLabel("Name:"));
topRightPanel.add(name);
topRightPanel.add(new JLabel("Address:"));
topRightPanel.add(address);
topRightPanel.add(new JLabel("Major:"));
topRightPanel.add(major);
topRightPanel.add(new JLabel("Balance:"));
topRightPanel.add(balance);
topRightPanel.add(new JLabel("Submit:"));
topRightPanel.add(jbtSubmit);
topRightPanel.add(printStudentNames);
topRightPanel.add(printGradStudentNames);
topRightPanel.add(calcBalance);
topRightPanel.add(compSciMajor);
// Handles the radioButton group and adds ActionListeners
jrbStudent.setSelected(true);
group.add(jrbStudent);
group.add(jrbGraduate);
jrbStudent.addActionListener(this);
jrbGraduate.addActionListener(this);
// topLeftPanel includes student type with student/gradStudent radio
// buttons & freshman/sophomore/junior/senior in a combo box
topLeftPanel.setBorder(BorderFactory
.createTitledBorder("Which Type of Student Are You?"));
topLeftPanel.add(jrbStudent);
topLeftPanel.add(jrbGraduate);
topLeftPanel.add(jcbo);
// Adds topLeftPanel and topRightPanel to bottomPanel
bottomPanel.add(topLeftPanel);
bottomPanel.add(topRightPanel);
// Places bottomPanel and scrollPane
getContentPane().add(BorderLayout.NORTH, bottomPanel);
getContentPane().add(BorderLayout.CENTER, scrollPane);
// Adds ActionListeners to the buttons
jbtSubmit.addActionListener(this);
printStudentNames.addActionListener(this);
printGradStudentNames.addActionListener(this);
calcBalance.addActionListener(this);
compSciMajor.addActionListener(this);
echoStudent.setEditable(false);
// Interface Settings
setSize(1200, 700);
setVisible(true);
}
// ActionEvent methods to handle interface events
public void actionPerformed(ActionEvent event) {
if (event.getSource() == jbtSubmit) {
if (jrbStudent.isSelected()) {
Student s1 = Manager.instance().createStudent(name.getText(),
address.getText(), major.getText(),
Double.parseDouble(balance.getText()));
echoStudent.append("\n\nCreated Student: " + s1.toString());
}
else if (jrbGraduate.isSelected()) {
GradStudent g1 = Manager.instance().createGradStudent(
name.getText(), address.getText(), major.getText(),
Double.parseDouble(balance.getText()));
echoStudent.append("\n\nCreated Graduate Student: "
+ g1.toString());
}
} else if (event.getSource() == printStudentNames) {
echoStudent.append(Manager.instance().listStudents());
} else if (event.getSource() == printGradStudentNames) {
echoStudent.append(Manager.instance().listGrads());
} else if (event.getSource() == calcBalance) {
echoStudent.append(Manager.instance().aveBalance());
} else if (event.getSource() == compSciMajor) {
echoStudent.append(Manager.instance().listCsci());
} else if (event.getSource() == jcbo) {
}
}
public static void main(String[] args) {
new GUI();
}
}
最佳答案
你可以这样实现。 首先,在 GUI() 构造函数中将 actionListener 添加到 JComboBox jcbo。
jcbo.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
String selected=""+jcbo.getSelectedItem();
if(selected.equals("Senior"))
System.out.println("Student has been in school for four years")
else if(selected.equals("Junior"))
System.out.println("Student has been in school for three years");
}
});
像这样,您可以对所有选项执行此操作。代码本身是不言自明的。 即使是这样。我给你一个简短的解释。 当选择 JComboBox 项目时,将执行上述代码。如果选择的是
- Senior :- Prints Student has been in school for four years.
- Junior :- Prints Student has been in school for three years.
我想这会对你有帮助。
关于java - JComboBox ActionEvent 在 Java 中返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23092661/