我正在为大学做书店GUI,
我有4个课程:Book
,bookfile
,BookGUI
,TestClass
。
我在测试中创建了一个bookFile()
对象(bkf
),并向其添加了Book
(b
),然后创建了我的BookGUI
(bkf
)对象(g
)将bkf
传递给它。
我只能编辑BookGUI
类!
在bkf
BookGUI
对象时,我无法弄清楚如何访问我传入的actionPerformed()
对象
我需要在按下添加按钮时更新Arraylist
中的bookFile()
,
如何在预先执行的操作中调用.addBook()
对象上的bkf
?
请仅回答我提出的问题,因为除非是良好的编码道德规范,否则我将自己解决问题。
感谢您的帮助。
识别TestClass
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());
}
}
}
bookFile
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;
}
}
答案 0 :(得分:1)
private final bookFile bkf;
public BookGUI(bookFile bkf)
{
this.bkf = bkf;
//The rest of your constructor..
}
在旁注中,bookFile
应为BookFile
,因为它是一个类。
答案 1 :(得分:1)
在BookGUI
的构造函数中保存对BookFile
实例的引用。然后,您可以从BookGUI
中的任何方法访问它。此外,您应该避免使用bkg
之类的短变量名称。使您的代码更难理解。
<强> BookGUI.java 强>
// ...
private final BookFile bookFile;
public BookGUI(BookFile bookFile)
{
this.bookFile = bookFile;
// ...
修改强>
正如@Majid L所说,类型名称应该以大写字母开头,因此bookFile
应为BookFile