JOptionPane下拉菜单Java

时间:2014-02-16 04:44:50

标签: java arrays arraylist joptionpane

我正在尝试使用JOptionPane创建一个下拉菜单。我希望它拥有的选项是数组列表的元素。所以我所做的是创建一个包含数组列表元素的数组。我希望用户能够看到元素作为选项。到目前为止我提供的代码。

我提供了人员,地址,学生和司机的课程。任何帮助将不胜感激。

package lab;

import java.util.Scanner;

public class Person {
Scanner in = new Scanner(System.in);
public String name;
public String phone;
public String email;
public Address address;

public Person(String name, String phone, String email, Address address){
    this.name = name;
    this.phone = phone;
    this.email = email;
    this.address = address;
}
public Person(){}

public String getName(){
    return name;
}
public String getPhone(){
    return phone;
}
public String getEmail(){
    return email;
}
public Address getAddress(){
    return address;
}
public String toString(){
    return name + "\n" + address + "\n" + phone + "\n" + email;
}
}

这是学生班

package lab;

import java.util.ArrayList;

public class Student extends Person{
private String studentID;
private String status;
private ArrayList<Course> courses = new ArrayList<Course>();
private Person person;
private Student student;

public Student(String id, String status, ArrayList<Course> courses, String name, String phone, String email, Address address){
    this.studentID = id;
    this.status = status;
    this.courses = courses;
    this.name = name;
    this.phone = phone;
    this.email = email;
    this.address = address;
}
public String getStudentID(){
    return studentID;
}
public String getStatus(){
    return status;
}
public ArrayList getCourses(){
    return courses;
}
public String toString(){
    return "Current Student: " + name + "\n" + studentID + "\n" + status + "\n" + address + "\n" + phone + "\n" + email;
}
}

这是Address类:

package lab;

public class Address {
private String number;
private String street;
private String city;
private String state;
private String zip;
public Address(String number, String street, String city, String state, String zip){
    this.number = number;
    this.street = street;
    this.city = city;
    this.state = state;
    this.zip = zip;
}
public String toString(){
    return number + " " + street + "\n" + city + "," + " " + state + "\n" + zip;
}
}

这是Driver类:

package lab;

import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Driver {
public static ArrayList<Person> students;
public static ArrayList<Course> courses;
public static ArrayList<FacultyMember> faculty;
static Scanner in;
public static int x;


public Driver()
{
    students = new ArrayList<Person>();
    courses = new ArrayList<Course>();
    faculty = new ArrayList<FacultyMember>();
}
public static void input(){
    String response = JOptionPane.showInputDialog(null, "Please enter your name: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    String response2 = JOptionPane.showInputDialog(null, "Please enter your phone number: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    String response3 = JOptionPane.showInputDialog(null, "Please enter your email: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    String response4 = JOptionPane.showInputDialog(null, "Please enter your street number: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    String response5 = JOptionPane.showInputDialog(null, "Please enter your street name: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    String response6 = JOptionPane.showInputDialog(null, "Please enter your city: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    String response7 = JOptionPane.showInputDialog(null, "Please enter your state: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    String response8 = JOptionPane.showInputDialog(null, "Please enter your postal code: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    String response9 = JOptionPane.showInputDialog(null, "Please enter your student ID: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    String response10 = JOptionPane.showInputDialog(null, "Please enter your student status: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    Address anAddress = new Address(response4, response5, response6, response7, response8);
    Student aStudent = new Student(response9, response10, courses, response, response2, response3, anAddress);
    students.add(aStudent);
    JOptionPane.showMessageDialog(null, students.get(students.size()-1).toString());
}
public static void courseInput(){
    FacultyMember instructor = null;
    in = new Scanner(System.in);
    String response = JOptionPane.showInputDialog(null, "Please enter the course identifier: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    String response2 = JOptionPane.showInputDialog(null, "Please enter course title: ", "A Request", JOptionPane.PLAIN_MESSAGE);
    String response3 = JOptionPane.showInputDialog(null, "Please enter course term: ", "A Request", JOptionPane.PLAIN_MESSAGE);
}
public static void main(String[] args) {
    students = new ArrayList<Person>();
    courses = new ArrayList<Course>();
    faculty = new ArrayList<FacultyMember>();
    Person[] choices = new Person[students.size()];//declare an array to use in drop down menu
    Object[] options1 = {"Quit", "Students", "Faculty", "Courses"};
    Object[] options2 = {"Go Back", "Add a Student", "Expel a Student", "Show Students"};
    Object[] options3 = {"Go Back", "Add an Instructor", "Delete an Instructor", "Show Instructors"};
    Object[] options4 = {"Go Back", "Create a Course", "Remove a Course", "View Courses"};
    int n = 0;
    do{//outer loop
        n = JOptionPane.showOptionDialog(null, "Welcome to CSULA, please select an option:", "Main Menu", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options1, options1[0]);
        if(n != 0){
            if(n == 1){//student loop
                int o = JOptionPane.showOptionDialog(null, "What would you like to do?", "Students", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options2, options2[0]);
                    if(n != 0){
                        if(o == 1){//add a student
                            input();
                        }
                        else if(o == 2){//expel a student
                            students.toArray(choices);
                            JOptionPane.showMessageDialog(null, choices[0]);
                            String input = (String) JOptionPane.showInputDialog(null, "Choose a student: ", "Expel", JOptionPane.QUESTION_MESSAGE,null, choices,choices[0]);



                            }
                      }
                        else if(o == 3){//Show students - go to drop down menu
                            for(int i = 0; i<students.size(); i++){
                                JOptionPane.showMessageDialog(null, students.get(i).toString());
                            }
                        }
                    }   
            }
            if(n == 2){//faculty loop
                int o = JOptionPane.showOptionDialog(null, "What would you like to do?", "Instructors", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options3, options3[0]);
                if(n != 0){
                    if(o == 1){//add instructor
                        input();
                    }
                    else if(o == 2){//fire instructor
                    }
                    else if(o == 3){//show instructor (via drop down menu)
                    }
                }
            }
            if(n == 3){//course loop
                int o = JOptionPane.showOptionDialog(null, "What would you like to do?", "Courses", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options4, options4[0]);
                if(n != 0){
                    if(o == 1){//add a course
                    }
                    else if(o == 2){//remove a course
                    }
                    else if(o == 3){//Show courses
                    }
                }
            }
            else if(n == 0){//quit program
            }

    }while(n != 0);
}
}

1 个答案:

答案 0 :(得分:0)

             public JButton createComponentDialogButton() { 
      Action a = new AbstractAction(getString("OptionPaneDemo.componentbutton")) { 
      public void actionPerformed(ActionEvent e) { 
    // In a ComponentDialog, you can show as many message components and 
    // as many options as you want: 

    // Messages 
             Object[]      message = new Object[4]; 
             message[0] = getString("OptionPaneDemo.componentmessage"); 
             message[1] = new JTextField(getString("OptionPaneDemo.componenttextfield")); 

             JComboBox cb = new JComboBox(); 
             cb.addItem(getString("OptionPaneDemo.component_cb1")); 
             cb.addItem(getString("OptionPaneDemo.component_cb2")); 
             cb.addItem(getString("OptionPaneDemo.component_cb3")); 
             message[2] = cb; 

             message[3] = getString("OptionPaneDemo.componentmessage2"); 

    // Options 
             String[] options = { 
        getString("OptionPaneDemo.component_op1"), 
        getString("OptionPaneDemo.component_op2"), 
        getString("OptionPaneDemo.component_op3"), 
        getString("OptionPaneDemo.component_op4"), 
        getString("OptionPaneDemo.component_op5") 
    }; 
             int result = JOptionPane.showOptionDialog( 
        getDemoPanel(),                             // the parent that the dialog blocks 
        message,                                    // the dialog message array 
        getString("OptionPaneDemo.componenttitle"), // the title of the dialog window 
        JOptionPane.DEFAULT_OPTION,                 // option type 
        JOptionPane.INFORMATION_MESSAGE,            
             // message type  null,                                      
            // optional icon, use null to use the default icon options,                                   
            // options string array, will be made into buttons  options[3]                                 


            // option that should be made into a default button 
    ); 
    switch(result) { 
       case 0: // yes 
         JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r1")); 
         break; 
       case 1: // no 
         JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r2")); 
         break; 
       case 2: // maybe 
         JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r3")); 
         break; 
       case 3: // probably 
         JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r4")); 
         break; 
       default: 
         break; 
    } 

    } 
}; 
return createButton(a); 
 }