**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 + '}';
}
}
您正在ActionPerformed(...)
中创建Book
对象,这意味着每次按下按钮时,都以一个新的、空的ArrayList
开始。
您应该使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++;
}
}
}
}
}
很抱歉这太模糊了,但代码太多了。 这是一个五卡梭哈游戏。基本上,我试图将对象添加到另一个数组列表中的数组列表中。 这是我创建玩家数组并将手类中的数组添加到其中的地方,至少我认为这就是正在发生的事情: 稍后,我尝试在循环交易中将牌添加到玩家阵列中的手牌阵列中。由于有五张牌,第一个循环进行了五次。第二个是玩家数组的长度(玩家总数),每次应该添加一张卡。 Deck类中有一个deal方法,由以下方法初始化
问题内容: 如何将对象添加到数组(使用javascript或jquery)?例如,此代码有什么问题? 我想使用此代码在function1数组中保存许多对象,并调用function2在数组中使用该对象。 如何将对象保存在数组中? 如何将对象放入数组并将其保存到变量? 问题答案: 使用Array.push()将任何东西放入数组。 有关数组的更多信息 一次添加多个项目 将项目添加到数组的开头 将一个数组
问题内容: 我很难弄清楚如何移动数组元素。例如,给出以下内容: 我为什么能写入移动功能之前? 还是之后? 移动后,应更新其余元素的索引。这意味着在第一个示例中,移动后arr [0] =’a’,arr [1] =’d’arr [2] =’b’,arr [3] =’c’,arr [4] = ‘e’ 这似乎应该很简单,但是我无法将其包裹住。 问题答案: 如果您想在npm上使用一个版本,则array-mo
问题内容: 我是Java的初学者,正在尝试创建自定义类的数组。假设我有一个名为car的类,并且我想创建一组称为Garage的汽车。如何将每辆车添加到车库?这就是我得到的: 问题答案: 如果要使用数组,则必须保留一个包含车库中汽车数量的计数器。最好使用而不是数组:
而不是更改InventoryItem中的参数 我想弄清楚如何传递新产品,如上面Store类的代码块所示。
Im正在尝试返回目录中的文件列表。有谁能帮忙吗?