将对象实例添加到下一个可用位置的数组中

时间:2015-04-04 16:45:58

标签: java user-interface arraylist event-handling listiterator

我正在完成我的实验室任务。

创建一个类Book(String isbn,String title,intprice) 提供参数化构造函数 提供toString方法 提供getter方法 在左边设计GUI 按下添加按钮后,将使用指定的数据创建新的书籍实例,并将其添加到下一个可用位置的阵列中。 为下一个和上一个按钮提供功能,以迭代数组并在字段中显示相应的记录

我在使用arraylist和list iterator实现add和next按钮时遇到了问题。我已经为此编写了代码,但它没有相应的工作。

以下是我的代码 它包含两个类

**BookGUI.java**
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ahmed.classtask.one;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.ListIterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Ahmed Adnan
 */
public class BookGUI extends JFrame {

    public static void main(String args[]) {
        BookGUI b = new BookGUI();
        b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        b.setVisible(true);
        b.setSize(300, 200);
        b.setTitle("Book Frame");

    }

    private JFrame f;
    //private JPanel p;

    private JLabel l1;
    private JLabel l2;
    private JLabel l3;

    private JTextField t1;
    private JTextField t2;
    private JTextField t3;

    private JButton addButton;
    private JButton nextButton;
    private JButton previousButton;

    public BookGUI() {
        frame();

    }

    public final void frame() {

        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        l1 = new JLabel("ISBN");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        add(l1, c);

        t1 = new JTextField(10);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 3;
        add(t1, c);

        l2 = new JLabel("Title");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        add(l2, c);

        t2 = new JTextField(10);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 3;
        add(t2, c);

        l3 = new JLabel("Price");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 2;
        add(l3, c);

        t3 = new JTextField(10);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 3;
        add(t3, c);

        addButton = new JButton("Add");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        add(addButton, c);

        nextButton = new JButton("Next");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        add(nextButton, c);

        previousButton = new JButton("Previous");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 2;
        c.gridy = 3;
        add(previousButton, c);

        MyListener listener = new MyListener();

        addButton.addActionListener(listener);
        nextButton.addActionListener(listener);
        previousButton.addActionListener(listener);

    }

    private class MyListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String isbnTF, titleTF;
            int priceTF = 0;

            ArrayList<Book> book = new ArrayList<>();
            ListIterator itr = book.listIterator();

            Book b = null;

            Object op = e.getSource();

            if (op.equals(addButton)) {
                isbnTF = t1.getText();
                titleTF = t2.getText();

                try {
                    priceTF = Integer.parseInt(t3.getText());

                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "Invalid input! ");

                }

                b = new Book(isbnTF, titleTF, priceTF);

                book.add(b);
                t1.setText("");
                t2.setText("");
                t3.setText("");

                System.out.println(b);
            } else if (op.equals(nextButton)) {

        if (itr == null) {
            itr = book.listIterator();
        }
        System.out.print("reached here");
        if (itr.hasNext()) {

            b = (Book) itr.next();
            t1.setText(b.getIsbn());
            t2.setText(b.getTitle());
            t3.setText(b.getPrice()+" ");
        }

            }

        }


    }

}

图书类

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ahmed.classtask.one;

/**
 *
 * @author Ahmed Adnan
 */
public class Book {

    private String isbn;
    private String title;
    private int price;

    public Book(String isbn, String title, int price) {
        this.isbn = isbn;
        this.title = title;
        this.price = price;
    }

    /**
     * @return the isbn
     */
    public String getIsbn() {
        return isbn;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @return the price
     */
    public int getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Book{" + "isbn=" + isbn + ", title=" + title + ", price=" +       price + '}';
    }



}

1 个答案:

答案 0 :(得分:0)

您正在book内创建actionPerformed(...)对象,这意味着每次按下按钮时,您都会以新的空ArrayList<Book>开始。

您应该将book对象设为类变量。

例如:

 private class MyListener implements ActionListener {
    ArrayList<Book> book = new ArrayList<>();
    int displayedBook = 0;
    @Override
    public void actionPerformed(ActionEvent e) {
        String isbnTF, titleTF;
        int priceTF = 0;
        Book b = null;
        Object op = e.getSource();
        if (op.equals(addButton)) {
            isbnTF = t1.getText();
            titleTF = t2.getText();
            try {
                priceTF = Integer.parseInt(t3.getText());
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Invalid input! ");
            }
            b = new Book(isbnTF, titleTF, priceTF);
            book.add(b);
            t1.setText("");
            t2.setText("");
            t3.setText("");
            System.out.println(b);
        } else if (op.equals(nextButton)) {
           if(book.size()>0){
               if(displayedBook >= book.size()){
                  displayedBook = 0;
               }
               b = book.get(displayedBook);
               t1.setText(b.getIsbn());
               t2.setText(b.getTitle());
               t3.setText(b.getPrice()+" ");
               displayedBook++;
           }
        }
    }
}
}