Domanda

I'm trying to make a drop down menu using JOptionPane. The options I want it to have are the elements of an array list. So what i have done is create an array with elements of the array list. I want the user to be able to see the elements as options.The code I have so far is provided.

I have provided my classes for Person, Address, Student and Driver. Any help would be greatly appreciated.

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

Here is the Student class

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

Here is the Address class:

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

Here is the Driver class:

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);
}
}
È stato utile?

Soluzione

             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); 
 } 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top