如何创建一个arraylist并使用按钮添加和查看?

时间:2014-04-12 01:59:09

标签: user-interface arraylist jframe jbutton actionlistener

我试图制作一个GUI程序,用于从arraylist中输入和移除汽车,并使用JButton显示汽车。我无法通过点击其中一个按钮来打印arraylist。我也不确定我的arraylist是否正确。任何帮助将不胜感激。

import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class Window extends JFrame {

    public Window() {
    super ("Rent-a-Car");

    setSize(400, 100);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

    JPanel p = new JPanel();
    JButton b1 = new JButton("Add Car");
    JButton b2 = new JButton("Rent Car");
    JButton b3 = new JButton("Library");

    p.add(b1);
    p.add(b2);
    p.add(b3);

    add(p);

    final ArrayList<String> cars = new ArrayList<String>();
    cars.add("Audi");
    cars.add("VolksWagon");
    cars.add("Mercedes");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Subaru");
    cars.add("Lexus");
    cars.add("Acura");
    cars.add("Nissan");

    b1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            Scanner sc = new Scanner(System.in);
            JOptionPane.showInputDialog("Enter car model");
            String model = sc.next();
            JOptionPane.showMessageDialog(null, "Car added");
            cars.add(model);

        }

    });


    /*b3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for(int i=0 < cars.size(); i++) {
                System.out.println();
            }
        }
    });*/


    }
}

1 个答案:

答案 0 :(得分:0)

ActionListener b1 System.in中,您试图从错误的地方扫描它。如果你运行它,你可能已经注意到当你使用添加按钮提交汽车时,JOptionPane不会做任何事情。但是,如果您在终端中输入一个字符串,则会弹出一个对话框,说“已添加汽车”。这是因为Scanner sc正在扫描JOptionPane!这不适用于GUI。

幸运的是,从showInputDialog获取输入非常简单。

编辑:在JScrollPane中添加了一些像JTextArea这样的东西,很容易追加并将其更改为简单的GridLayout。

我还将ActionListeners移动到内部类中,这样就不会在任何地方使用匿名函数来混淆代码。使用(e.getSource())条件也添加内容也更容易。

将模型设置为等于import java.util.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.GridLayout; import javax.swing.*; public class Window extends JFrame { private JPanel p; private JButton b1, b2, b3; private JTextArea textArea; private JScrollPane scrollPane; private ArrayList<String> cars; public Window() { super ("Rent-a-Car"); setLayout(new GridLayout(2, 1)); setSize(400, 100); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); p = new JPanel(); b1 = new JButton("Add Car"); b2 = new JButton("Rent Car"); b3 = new JButton("Library"); b1.addActionListener(new ButtonListener()); b2.addActionListener(new ButtonListener()); b3.addActionListener(new ButtonListener()); textArea = new JTextArea(""); textArea.setEditable(false); scrollPane = new JScrollPane(textArea); p.add(b1); p.add(b2); p.add(b3); add(p); //add(textArea); add(scrollPane); cars = new ArrayList<String>(); cars.add("Audi"); cars.add("VolksWagon"); cars.add("Mercedes"); cars.add("BMW"); cars.add("Ford"); cars.add("Subaru"); cars.add("Lexus"); cars.add("Acura"); cars.add("Nissan"); } public void displayText(String s) { textArea.append(s + "\n"); textArea.setCaretPosition(textArea.getDocument().getLength()); } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == b1) { String model = JOptionPane.showInputDialog("Enter car model"); cars.add(model); displayText("Added car: " + model); } else if (e.getSource() == b2) { //add something later } else if (e.getSource() == b3) { for (String car : cars) { displayText(car); } } } } } 的结果:

{{1}}