JComboBox ActionEvent返回Java中的值
我正在尝试将JComboBox添加到我的学生计划中,选项包括:大一、大二、大三或大四。当选择高级选项时,它应该输出大致如下的内容:“学生在校四年”或“学生在校三年”。等等…我在论坛上搜索了一下,在谷歌上搜索了一下,但是我找不到一个类似的例子。我将在下面发布代码。任何帮助都将不胜感激。谢谢 学生班级:JComboBox ActionEvent返回Java中的值,java,swing,user-interface,jcombobox,actionevent,Java,Swing,User Interface,Jcombobox,Actionevent,我正在尝试将JComboBox添加到我的学生计划中,选项包括:大一、大二、大三或大四。当选择高级选项时,它应该输出大致如下的内容:“学生在校四年”或“学生在校三年”。等等…我在论坛上搜索了一下,在谷歌上搜索了一下,但是我找不到一个类似的例子。我将在下面发布代码。任何帮助都将不胜感激。谢谢 学生班级: public class Student { protected String name; protected String address; protected doubl
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;
}
}
公共类管理器{
私有静态管理器;
公立学生[]学生=新生[50];
公共int studentCounter=0;
公立研究生[]研究生=新研究生[50];
公共计数器=0;
//私有构造函数(单实例方法)
公共经理(){
}
//实例方法(单例)
公共静态管理器实例(){
if(thisManager==null){
返回thisManager=newmanager();
}否则{
返回此经理;
}
}
公立学生创建学生(字符串名称、字符串地址、字符串专业、,
双倍金额){
学生c1=新学生(姓名、地址、专业、金额);
学生(c1);
返回c1;
}
私人学生(学生c1){
学生[studentCounter++]=c1;
}
public GradStudent createGradStudent(字符串名称、字符串地址、,
字符串主调,双倍金额){
研究生e1=新研究生(姓名、地址、专业、金额);
研究生(e1);
返回e1;
}
私人研究生(研究生g1){
研究生[gradCounter++]=g1;
}
公共字符串listStudents(){
字符串temp=“\n\n学生列表\n”;
for(int i=0;i
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();
}
}
import javax.swing.*;
导入java.awt.event.*;
导入java.awt.*;
导入java.util.Enumeration;
公共类GUI扩展JFrame实现ActionListener{
//单选按钮和按钮组
私人JRadioButton jrbStudent=新JRadioButton(“学生”);
私人JRadioButton jrbGraduate=新JRadioButton(“研究生”);
private ButtongGroup=新建ButtongGroup();
//JTextFields
私有JTextField name=新JTextField(20);
私有JTextField地址=新JTextField(20);
私人JTextField余额=新JTextField(20);
专用JTextField major=新JTextField(20);
//提交按钮
私有JButton jbtSubmit=新JButton(“提交”);
//学生输出区
私人JTextArea echoStudent=新JTextArea(5,20);
//特定按钮
私有JButton printStudentNames=新JButton(“打印学生姓名”);
私有JButton printGradStudentNames=新JButton(
“打印研究生姓名”);
私有JButton calcBalance=新JButton(
“计算所有学生的平均余额”);
私有JButton compSciMajor=新JButton(
“显示计算机科学专业学生”);
//滚动窗格
私有JScrollPane scrollPane=新JScrollPane(echoStudent);
//填写并创建JComboBox
私人字符串[]学生年={“大一”、“大二”、“大三”、“大四”};
私人JCOMBOX jcbo=新JCOMBOX(学生年);
公共图形用户界面(){
超级(“大学生信息界面”);
//创建面板
JPanel bottomPanel=新的JPanel();
JPanel topLeftPanel=新的JPanel();
JPanel topRightPanel=新的JPanel();
//将标签、字段和按钮添加到topRightPanel
setLayout(新的GridLayout(7,2,5,5));
添加(新的JLabel(“名称”);
topRightPanel.add(名称);
添加(新的JLabel(“地址:”);
topRightPanel.add(地址);
添加(新的JLabel(“Major:”);
右上面板。添加(主);
添加(新JLabel(“余额:”);
右上面板。添加(平衡);
添加(新JLabel(“提交:”);
添加(jbtSubmit);
topRightPanel.add(printStudentNames);
topRightPanel.add(printGradStudentNames);
topRightPanel.add(calcBalance);
topRightPanel.add(compSciMajor);
//处理radioButton组并添加ActionListeners
jrbStudent.setSelected(true);
添加组(jrbStudent);
添加组(JRBGRADUDE);
jrbStudent.addActionListener(这个);
jrbGraduate.addActionListener(此);
//topLeftPanel包括带学生/研究生收音机的学生类型
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");
}
});