我正在为大学做一个书店 GUI,
我有 4 个类:Book
、bookfile
、BookGUI
、TestClass
。
我在测试中创建了一个 bookFile()
对象 (bkf
),并向其中添加了一个 Book
(b
),然后创建我的 BookGUI
(bkf
) 对象 (g
),并将 bkf
传递给它.
我只能编辑 BookGUI
类!
我不知道如何从 actionPerformed()
中访问创建 BookGUI
对象时传入的 bkf
对象
当按下添加按钮时,我需要更新 bookFile()
中的 Arraylist
,
如何从执行的操作中调用 bkf
对象上的 .addBook()
?
请只回答我提出的问题,因为我会自己解决剩下的问题,除非这是良好的编码道德。
感谢您的帮助。
测试类
public class TestClass {
public static void main(String[] args)
{
bookFile bkf = new bookFile();
Book b = new Book("Dan Brown","The daVinci Code",3,10.99);
bkf.addBook(b);
BookGUI g = new BookGUI(bkf);
g.setVisible(true);
}
}
BookGUI
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
class BookGUI extends JFrame implements ActionListener{
private final int width = 290;
private final int height = 260;
private JLabel title, author, quantity, price;
private JTextField titleTBox, authorTBox, quantityTBox, priceTBox;
private JTextArea outputBox;
private JButton addButton, totalQ, totalV, exit;
private TitledBorder border;
private Container c;
private JPanel input = new JPanel(new GridLayout(0,2));
private JPanel buttons = new JPanel(new GridLayout(2,2));
private JPanel output = new JPanel(new FlowLayout());
public BookGUI(bookFile bkf)
{
c = getContentPane();
this.setLayout(new BorderLayout(2,2));
this.setSize(width, height);
this.add(input,BorderLayout.NORTH);
input.setBackground(Color.LIGHT_GRAY);
title = new JLabel("Title");
input.add(title);
titleTBox = new JTextField();
input.add(titleTBox);
author = new JLabel("Author");
input.add(author);
authorTBox = new JTextField();
input.add(authorTBox);
quantity = new JLabel("Quantity");
input.add(quantity);
quantityTBox = new JTextField();
input.add(quantityTBox);
price = new JLabel("Price");
input.add(price);
priceTBox = new JTextField();
input.add(priceTBox);
this.add(buttons,BorderLayout.CENTER);
addButton = new JButton("Add");
buttons.add(addButton);
addButton.addActionListener(this);
totalQ = new JButton("Total Quantity");
buttons.add(totalQ);
totalQ.addActionListener(this);
totalV = new JButton("Total Value");
buttons.add(totalV);
totalV.addActionListener(this);
exit = new JButton("Exit");
buttons.add(exit);
exit.addActionListener(this);
this.add(output,BorderLayout.SOUTH);
output.setBorder(new TitledBorder("Output"));
outputBox = new JTextArea(3, 20);
output.add(outputBox);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addButton)
{
int q = Integer.parseInt(quantityTBox.getText());
double p = Double.parseDouble(priceTBox.getText());
Book nb = new Book(titleTBox.getText(), authorTBox.getText(), q, p);
.addBook(nb);
outputBox.setText(bkf.getDetails());
}
}
}
图书文件
import java.util.ArrayList;
public class bookFile {
private ArrayList <Book> blist = new ArrayList<Book>();
public void addBook(Book b)
{
blist.add(b);
}
public Book getBook(int i) {
return blist.get(i);
}
// returns a string containing all the book details in the array list
public String getDetails()
{
String output="";
for (int i=0;i<blist.size();i++)
{
output += (i+1)+"."+blist.get(i).getTitle() +","+ blist.get(i).getAuthor()+","
+ blist.get(i).getQuantity()+","+ blist.get(i).getPrice() +"\n";
}
return output;
}
// returns the total quantity of books in the array list
public int gettotalQuantity()
{
int total=0;
for (int i=0;i<blist.size();i++)
{
total += blist.get(i).getQuantity();
}
return total;
}
// returns the total value of books in the array list
public double gettotalValue()
{
double total=0;
for (int i=0;i<blist.size();i++)
{
total += (blist.get(i).getQuantity()*blist.get(i).getPrice());
}
return total;
}
}
预订
public class Book {
//Member Variables:
private String author;
private String title;
private int quantity;
private double price;
public Book(String a, String t, int q, double p)
{
author=a;
title=t;
quantity=q;
price=p;
}
//Getters & Setters:
public String getAuthor()
{
return author;
}
public void setAuthor(String a)
{
author=a;
}
public String getTitle()
{
return title;
}
public void setTitle(String t)
{
title=t;
}
public double getPrice()
{
return price;
}
public void setPrice(double p)
{
price = p;
}
public int getQuantity()
{
return quantity;
}
public void setQuantity(int q)
{
quantity = q;
}
}
最佳答案
在 BookGUI
的构造函数中保存对 BookFile
实例的引用。然后,您可以通过 BookGUI
中的任何方法访问它。另外,您应该避免像 bkg
这样的短变量名称。使您的代码更难理解。
BookGUI.java
// ...
private final BookFile bookFile;
public BookGUI(BookFile bookFile)
{
this.bookFile = bookFile;
// ...
编辑
正如 @Majid L 所说,类型名称实际上应该以大写字母开头,因此 bookFile
应该是 BookFile
关于java - 从 actionPerformed 中访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22022656/