使用JButton,JTextArea和showInputDialog从ArrayList显示GUI

时间:2014-01-02 10:55:47

标签: java swing arraylist

有没有人知道如何使用actionPerformed方法迭代ArrayList并检索项目?

基本上我试图让用户点击一个按钮。单击该按钮后会出现showInputDialog,然后用户输入学生的ID号。所有学生的详细信息都将显示在jtextArea或showMessageDialog框中。

有人能告诉我吗? 这是我刚刚以学生为例的实际代码。

import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*; 

public class DepotAdmin 
{
    private ArrayList<Vehicle> DepoVehicle = new ArrayList<Vehicle>(); 
    public DepotAdmin(String hd,String c, String m, String d,Fuel p,int EngSize)
    {
          DepoVehicle = new ArrayList<Vehicle>();

    }


     private void setupDepot(){
        //DepotArraylist vehicles = new DepotArraylist("1111","1111","1111","1111",new Fuel(""),0);

        DepoVehicle.add(new Car("BobMart","567890","Volvo","04/06/2014", new Fuel("Diesel"),6000));
        DepoVehicle.add(new Car("BobMart","465783","BMW","12/052014", new Fuel ("Petrol"), 8000));
        DepoVehicle.add(new Car("BobMart","348709","Ford","13/08/2014",new Fuel ("Petrol"), 3000));
        DepoVehicle.add(new Car("BobMart","145367","Lexus","18/08/2015",new Fuel ("Diesel"), 2000));
        DepoVehicle.add(new RentableCar("BobMart","847469","Citreon","08/07/2015",new             Fuel("Diesel"),1000));
        Car car1 = new Car("BobMart","567890","Volvo","04/06/2014", new Fuel("Diesel"),6000);



    public Vehicle byMake(String make)
    {
            String st[]=DepoVehicle.toArray(new String[DepoVehicle.size()]); 
              for ( Vehicle v : DepoVehicle)
         if (v.getMake().equals(make)){
            return v;
            }
           return null;

      }




    private JFrame Depot ;
    private JMenu Vehicles;
    private JMenu searchVehicle;    
    private JMenu rentVehicle;    
    private JMenu returnVehicle;
    private JMenuItem vehicles1;
    private JMenuItem searchVehicle1;
    private JMenuItem rentVehicle1;
    private JMenuItem returnVehicle1; 
    private JTextArea textArea;
    private JMenuBar menubar;
    private JList vList;
    private ResDepotAdmin listener;
    private ResDepotAdmin listener1;
    private ResDepotAdmin listener2;
    private ResDepotAdmin listener3;
    private JLabel repeatLabel;
    private JTextField inputText;
    private JPanel textPanel;
    public DepotAdmin(){
    //creating the frame to place the menu bar
    Depot = new JFrame ("Depot");
    //creating the menubar to place the menu items
    menubar = new JMenuBar();
    //set the menu bar in the frame
    Depot.setJMenuBar(menubar);
    //set the menus on the menu bar
    Vehicles = new JMenu ("vehicles");
    menubar.add(Vehicles);
    searchVehicle = new JMenu ("search");
    menubar.add(searchVehicle);
    rentVehicle = new JMenu ("rent");
    menubar.add(rentVehicle);
    returnVehicle = new JMenu ("return");
    menubar.add(returnVehicle);
    //set the menu items on the menues
    vehicles1 = new JMenuItem ("Vehicle List");
    Vehicles.add(vehicles1);
    searchVehicle1 = new JMenuItem ("Search Vehicle");
    searchVehicle.add(searchVehicle1);
    rentVehicle1 = new JMenuItem ("Rent Vehicle");
    rentVehicle.add(rentVehicle1);
    returnVehicle1 = new JMenuItem ("Return Vehicle");
    returnVehicle.add(returnVehicle1);
    //data display section
    textArea = new JTextArea(70,70);
    textPanel = new JPanel();
    //add textarea to panel
    textPanel.add(textArea);
    //add panel to frame
    Depot.add((textPanel),BorderLayout.CENTER);
    listeners to respond to click by uaer
    listener = new ResDepotAdmin();
    listener1 = new ResDepotAdmin();
    listener2 = new ResDepotAdmin();
    listener3 = new ResDepotAdmin();
    vehicles1.addActionListener(listener);
    searchVehicle1.addActionListener(listener1);
    rentVehicle1.addActionListener(listener2);
    returnVehicle.addActionListener(listener3);


    String vehicleMake = "make";
    //JOptionPane.showInputDialog(searchVehicle1,"Enter vehicle make");
    //set the size of the frame
    Depot.setSize(600,600);
    //aet the visibility of the frame
    Depot.setVisible(true);
    //prevent the running the the program on exit
    Depot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



         }
         public class ResDepotAdmin implements ActionListener
    {


       private String make; 
     private void setupDepot(){
        DepotArraylist vehicles = new DepotArraylist("1111","1111","1111","1111",new Fuel(""),0);
        vehicles.addVehicle(new Car("BobMart","567890","Volvo","04/06/2014", new Fuel("Diesel"),6000));
        vehicles.addVehicle(new Car("BobMart","465783","BMW","12/052014", new Fuel ("Petrol"), 8000));
        vehicles.addVehicle(new Car("BobMart","348709","Ford","13/08/2014",new Fuel ("Petrol"), 3000));
        vehicles.addVehicle(new Car("BobMart","145367","Lexus","18/08/2015",new Fuel ("Diesel"), 2000));
        vehicles.addVehicle(new RentableCar("BobMart","847469","Citreon","08/07/2015",new Fuel("Diesel"),1000));
        Car car1 = new Car("BobMart","567890","Volvo","04/06/2014", new Fuel("Diesel"),6000);



}


         public Vehicle findVehicle(String make)
          {
           for (Vehicle dv : DepoVehicle)
             if (dv.getMake().equals(make))
            return dv;
              return null;
            }


             public void actionPerformed(ActionEvent e)
             {
                       //DepotAdmin DepoVehicle = new DepotAdmin("null","null","null","null",new Fuel(""),0);

                       ArrayList<Vehicle> DepoVehicle = new ArrayList<Vehicle>();
                               DepoVehicle.add(new Car("BobMart","567890","Volvo","04/06/2014",    new Fuel("Diesel"),6000));
                               DepoVehicle.add(new Car("BobMart","348709","Ford","13/08/2014",new Fuel ("Petrol"), 3000));


                     if ( e.getSource() == vehicles1){
                     textArea.append(DepoVehicle.toString()+"\n"+"\n");
                     }


                     String make = JOptionPane.showInputDialog(null,"Enter vehicle make");

                      if (e.getSource()== searchVehicle1){

                           for ( Vehicle v : DepoVehicle){ if (v.getMake().equals(make)){
                                                        textArea.append(make);

                                                                  }
                                                     }

                     }
            }
      }
}

2 个答案:

答案 0 :(得分:1)

我真的不确定如何解释你有关于汽车的信息,但你希望学生作为输入。所以我改变了一下。相应地进行更改。

我只是循环播放,如果学生ID与输入匹配,我将学生添加到文本区域

public void actionPerformed{

   String stuId = JOptionPane.showInputDialog(null,"Enter Student ID");

   for (Student student : pupils){
       if (student.getId().equals(stuId)){
           textArea.append(student);
           break;
       }
   }
}

  • 另外,我注意到你在main方法中有一个actionPerformed。您是否正在尝试创建GUI程序?因为在这一点上,你的代码远未完成它。

  • 你似乎也有一个main内的方法,这是一个不可以。你的代码绝对不会编译。请发布一个可编译的程序,以便我们可以准确地看到您要完成的任务。因为帖子中的代码是垃圾。


我注意到的另一个巨大错误就在这里

    String name;
    String id;
    public Student (String n, String i) {
        n = name;
        i = id;
    }

你的构造函数是错误的。 n = name什么也没做。相反,你想要

    name = n;
    id = i;

同样Student接受字符串输入,但您尝试添加int。也许这仅仅是为了SSCCE尝试,但是它从文本字段中获取这些输入,它可以工作,但如果你正在编写它,它们需要是String。

    pupils.add(new Student("james", "2345"));
    pupils.add(new Student ("taylor", "5768"));
    pupils.add(new Student("andy", "9874"));

对于你的Student类,如果你想要Student对象的String表示,你应该@Override toString()方法,类似这样

    public String toString() {
       //am i wrong here?  
        return "Student Name: " + name + ", ID: " + id;
    }

这是一个简单的完整可运行程序,因此您可以看到所有内容如何组合在一起。检查它,看看你可能出错的地方。随意提问。

import java.awt.BorderLayout;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

public class StudentGUI extends JFrame {

    private JTextArea jtaStudent = new JTextArea(10, 30);
    private JButton showStudents = new JButton("Show Students");
    ArrayList<Student> pupils = new ArrayList<>();

    public StudentGUI() {
        pupils.add(new Student("james", "2345"));
        pupils.add(new Student("taylor", "5768"));
        pupils.add(new Student("andy", "9874"));

        add(showStudents, BorderLayout.SOUTH);
        add(jtaStudent, BorderLayout.CENTER);

        showStudents.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                for (Student student : pupils) {
                    jtaStudent.append(student + "\n");
                }
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new StudentGUI();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }
}

class Student {
    String name;
    String id;

    public Student(String n, String i) {
        name = n;
        id = i;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public String toString() {
        // am i wrong here?
        return "Student Name: " + name + ", ID: " + id;
    }
}

答案 1 :(得分:-1)

您为:

创建了一个构造函数
public Students(parameter, parameter){}

,但您没有为:

创建构造函数
public Student(){}

通常,如果您自己不创建任何构造函数,编译器将为您创建一个no-arg cosntructor,但是当您自己创建任何构造函数时,您将需要自己创建一个无参数构造函数。希望这会有所帮助。