GUI帮助 - HashMapping

时间:2017-07-15 19:20:18

标签: java

我正在尝试创建一个确定学生GPA的GUI。我在comboBox的“查找”选项中特别需要帮助。我正在尝试检索与学生ID相关联的信息。当选择“查找”选项并且用户按下“处理”按钮时,我希望根据ID字段填写“名称”和“主要”字段。任何帮助将不胜感激。

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 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");

    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);
}

}

0 个答案:

没有答案