Java JOptionPane和组合框
我正试图添加一个组合框作为上一个组合框选择的结果。一旦用户选择“更新”,它应该打开另一个组合框的JopOptions窗格。我该怎么做呢?到目前为止,我有一个JOptionPane打开并要求用户输入他们的选择,但是,我尝试使用一个组合框,而不是像下图所示Java JOptionPane和组合框,java,combobox,joptionpane,Java,Combobox,Joptionpane,我正试图添加一个组合框作为上一个组合框选择的结果。一旦用户选择“更新”,它应该打开另一个组合框的JopOptions窗格。我该怎么做呢?到目前为止,我有一个JOptionPane打开并要求用户输入他们的选择,但是,我尝试使用一个组合框,而不是像下图所示 import java.awt.*; 导入java.awt.event.*; 导入java.util.HashMap; 导入javax.swing.*; 公共类GUI扩展JFrame实现ActionListener{ private HashM
import java.awt.*;
导入java.awt.event.*;
导入java.util.HashMap;
导入javax.swing.*;
公共类GUI扩展JFrame实现ActionListener{
private HashMap map=new HashMap();
私有JButton processRequest=新JButton(“流程请求”);
私有JComboBox组合框=新JComboBox();
私有JComboxCombox2=新的JCombox2();
私有JLabel=newjlabel(“选择请求:”);
专用JLabel label1=新JLabel(“Id:”);
专用JLabel label2=新JLabel(“名称:”);
专用JLabel标签3=新JLabel(“主要:”);
私有JTextField text1=新的JTextField(“”);
私有JTextField text2=新的JTextField(“”);
私有JTextField text3=新JTextField(“”);
公共图形用户界面(){
setLayout(新的GridLayout(0,2));
comboBox.addItem(“添加”);
comboBox.addItem(“查找”);
comboBox.addItem(“更新”);
组合框。添加项(“删除”);
组合框2.附加项(“A”);
组合框2.附加项(“B”);
组合框2.附加项(“C”);
组合框2.添加项(“D”);
组合框2.附加项(“F”);
添加(标签1);
添加(文本1);
添加(标签2);
添加(文本2);
添加(标签3);
添加(文本3);
添加(标签);
添加(组合框);
添加(处理请求);
processRequest.addActionListener(此);
}
已执行的公共无效操作(操作事件e){
if(comboBox.getSelectedItem().equals(“Add”)){
String str=text1.getText();
int-sid=Integer.parseInt(str);
字符串sname=text2.getText();
字符串smajor=text3.getText();
text1.setText(“”);
text2.setText(“”);
text3.setText(“”);
学生=新学生(sname、smajor);
if(地图容器(sid))
showMessageDialog(null,sid+“已插入数据库。”);
其他的
地图。放置(sid,学生);
}
else if(comboBox.getSelectedItem().equals(“Find”)){
String str=text1.getText();
int-sid=Integer.parseInt(str);
if(地图容器(sid)){
String result=“ID:”+sid+”,“+map.get(sid);
showMessageDialog(空,结果);
}
其他的
showMessageDialog(在数据库中找不到null,sid+);
}
else if(comboBox.getSelectedItem().equals(“更新”)){
String str=JOptionPane.showInputDialog(“输入学生ID:”);
int-sid=Integer.parseInt(str);
str=JOptionPane.showInputDialog(“输入课程成绩:”);
char grade=Character.toUpperCase(str.charAt(0));
str=JOptionPane.showInputDialog(“输入学分:”);
双倍小时=double.parseDouble(str);
if(地图容器(sid)){
地图。获取(sid)。课程完成(年级,学时);
}
其他的
showMessageDialog(在数据库中找不到null,sid+);
}
else if(comboBox.getSelectedItem().equals(“删除”)){
String str=JOptionPane.showInputDialog(“输入学生ID:”);
int-sid=Integer.parseInt(str);
if(地图容器(sid))
地图删除(sid);
其他的
showMessageDialog(在数据库中找不到null,sid+);
}
否则{
System.out.println();
}
}
公共静态void main(字符串[]args){
GUI框架=新GUI();
框架。设置标题(“项目4”);
框架。设置尺寸(400300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import javax.swing.*;
public class GUI extends JFrame implements ActionListener{
private HashMap<Integer, Student> map = new HashMap<Integer,Student>();
private JButton processRequest = new JButton("Process Request");
private JComboBox<String> comboBox = new JComboBox<String>();
private JComboBox<String> comboBox2 = new JComboBox<String>();
private JLabel label = new JLabel("Choose a request:");
private JLabel label1 = new JLabel("Id:");
private JLabel label2 = new JLabel("Name:");
private JLabel label3 = new JLabel("Major:");
private JTextField text1 = new JTextField("");
private JTextField text2 = new JTextField("");
private JTextField text3 = new JTextField("");
public GUI(){
setLayout(new GridLayout(0,2));
comboBox.addItem("Add");
comboBox.addItem("Find");
comboBox.addItem("Update");
comboBox.addItem("Delete");
comboBox2.addItem("A");
comboBox2.addItem("B");
comboBox2.addItem("C");
comboBox2.addItem("D");
comboBox2.addItem("F");
add(label1);
add(text1);
add(label2);
add(text2);
add(label3);
add(text3);
add(label);
add(comboBox);
add(processRequest);
processRequest.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(comboBox.getSelectedItem().equals("Add")){
String str = text1.getText();
int sid = Integer.parseInt(str);
String sname = text2.getText();
String smajor = text3.getText();
text1.setText("");
text2.setText("");
text3.setText("");
Student student = new Student(sname, smajor);
if(map.containsKey(sid))
JOptionPane.showMessageDialog(null, sid + " is already inserted in the database.");
else
map.put(sid, student);
}
else if(comboBox.getSelectedItem().equals("Find")){
String str = text1.getText();
int sid = Integer.parseInt(str);
if(map.containsKey(sid)){
String result = "ID: " + sid + ", " + map.get(sid);
JOptionPane.showMessageDialog(null, result);
}
else
JOptionPane.showMessageDialog(null, sid + " is not found in the database.");
}
else if(comboBox.getSelectedItem().equals("Update")){
String str = JOptionPane.showInputDialog("Enter student ID:");
int sid = Integer.parseInt(str);
str = JOptionPane.showInputDialog("Enter the course grade:");
char grade = Character.toUpperCase(str.charAt(0));
str = JOptionPane.showInputDialog("Enter credit hours:");
double hours = Double.parseDouble(str);
if(map.containsKey(sid)){
map.get(sid).courseCompleted(grade, hours);
}
else
JOptionPane.showMessageDialog(null, sid + " is not found in the database.");
}
else if(comboBox.getSelectedItem().equals("Delete")){
String str = JOptionPane.showInputDialog("Enter student ID:");
int sid = Integer.parseInt(str);
if(map.containsKey(sid))
map.remove(sid);
else
JOptionPane.showMessageDialog(null, sid + "is not found in the database.");
}
else{
System.out.println();
}
}
public static void main(String [] args){
GUI frame = new GUI();
frame.setTitle("Project 4");
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}