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

Adding object instance to the array in the next available location

我正在做我的实验室任务。

创建 class 图书(字符串 isbn、字符串标题、价格) 提供参数化构造函数 提供toString方法 提供 getter 方法 设计左侧的GUI 按下添加按钮时,将使用指定的数据创建一个新的书籍实例,并将其添加到下一个可用位置的数组中。 为下一个和上一个按钮提供循环访问数组并在字段中显示相应记录的功能

我在使用数组列表和列表迭代器实现添加和下一个按钮时遇到问题。我已经为此编写了代码,但它没有相应地工作。

下面是我的代码 它包含两个 classes

**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()+" ");
        }

            }

        }


    }

}

图书Class

/*
 * 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 + '}';
    }



}

您正在 actionPerformed(...) 中创建 book 对象,这意味着每次按下按钮时,您都会从一个新的空 ArrayList<Book>.

开始

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

例如:

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