[英]JComboBox ActionEvent to return a value in Java
我试图将JComboBox添加到我的学生程序中,并具有以下选项:新生,大二,大三或大四。 选择“高级”选项时,它应该输出类似“学生已经在学校学习四年”或“初级”,“学生已经在学校学习三年”的内容。 等等...我搜索了论坛并做了一些谷歌搜索,但是我无法在此尝试与之相似的示例。 我将在下面发布代码。 任何帮助将不胜感激。 谢谢!
学生班:
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()构造函数中向JComboBox jcbo添加actionListener。
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项目时,将执行上述代码。
- 高级:-版画学生已经在学校学习了四年。
- 初中:-版画学生已经在学校学习了三年。
我认为它将为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.