Java 试图在GUI中显示总库存方法
我正在尝试使用calculateInventoryTotal方法在GUI中显示总库存字符串之后的总库存值。我无法让它正确显示,所以我请求帮助。我搜索了几个小时,但找不到这个问题的答案。提前感谢您帮助我完成这个项目 使用此语句可以获得inventoryTotal值Java 试图在GUI中显示总库存方法,java,swing,awt,Java,Swing,Awt,我正在尝试使用calculateInventoryTotal方法在GUI中显示总库存字符串之后的总库存值。我无法让它正确显示,所以我请求帮助。我搜索了几个小时,但找不到这个问题的答案。提前感谢您帮助我完成这个项目 使用此语句可以获得inventoryTotal值 public class BookStore { //total price of inventory method public static double calculateInventoryTotal(Book[
public class BookStore {
//total price of inventory method
public static double calculateInventoryTotal(Book[] books) {
double total = 0;
for (int i = 0; i < books.length; i++) {
total += books[i].getPrice();
}
return total;
}
static int bookIndex = 0;
public static JTextArea prepareDisplay(Book myBook, JTextArea myTextArea) {
DecimalFormat money = new DecimalFormat("$0.00");
myTextArea.setText("");
myTextArea.append("Book Store Inventory: \n\n");
myTextArea.append(myBook.toString());
myTextArea.append("Total Inventory Value: ");
return myTextArea;
}
//main method
public static void main(String[] args) {
// book array
final Book[] inventory = new Book[5];
Book a = new Book(7423540089L, "No David!", "David Shannon", 2009, "Shannon Rock", 12.99);
EBook b = new EBook(75260012L, "David goes to School", "David Shannon", 2010, "Shannon Rock", 11.98, "http://www.tinyurl.qqwert67o9");
Book c = new Book(743200616L, "Simple Abundance", "Sarah Breathnach", 2009, "Scribner", 14.99);
EBook d = new EBook(78137521819L, "The very hungry caterpillar", "Eric Carle", 2005, "Philomel Books", 13.99, "http://www.tinyurl.fguopt8u90");
Book e = new Book(9781416987116L, "We're going on a bear hunt", "Michael Rosen", 2009, "McElderry", 15.99);
inventory[0] = a;
inventory[1] = b;
inventory[2] = c;
inventory[3] = d;
inventory[4] = e;
Arrays.sort(inventory);
//parameters of the JTextArea
final JTextArea textArea = new JTextArea(15, 15);
textArea.setText("");
textArea.setEditable(false);
DecimalFormat money = new DecimalFormat("$0.00");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 3));
JButton firstButton = new JButton("First");
buttonPanel.add(firstButton);
firstButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookIndex = 0;
prepareDisplay(inventory[bookIndex], textArea);
}
});
JButton nextButton = new JButton("Next");
buttonPanel.add(nextButton);
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookIndex++;
if (bookIndex >= inventory.length) {
bookIndex = 0;
}
prepareDisplay(inventory[bookIndex], textArea);
}
});
JButton previousButton = new JButton("Previous");
buttonPanel.add(previousButton);
previousButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookIndex--;
if (bookIndex < 0) {
bookIndex = inventory.length - 1;
}
prepareDisplay(inventory[bookIndex], textArea);
}
});
JButton lastButton = new JButton("Last");
buttonPanel.add(lastButton);
lastButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookIndex = (inventory.length - 1);
prepareDisplay(inventory[bookIndex], textArea);
}
});
JLabel logoLabel = new JLabel(new ImageIcon("bluehills.jpg"));
JPanel logoPanel = new JPanel();
logoPanel.add(logoLabel);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
centerPanel.add(prepareDisplay(inventory[bookIndex], textArea)); //show the first employee
//calculate inventory total
double inventoryTotal = calculateInventoryTotal(inventory);
//myTextArea.append("Total Inventory Value: " + money.format(inventoryTotal) + "\n\n");
//set the GUI window with the parameters
JFrame frame = new JFrame("Book Store Program");
frame.setLayout(new BorderLayout()); // set layout
frame.add(logoPanel, BorderLayout.NORTH); //add logo
frame.add(buttonPanel, BorderLayout.SOUTH); // add previous and next button to frame
frame.add(centerPanel, BorderLayout.CENTER); // add center panel to frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}// end class
但是你从来没有将该值关联到gui元素来显示你说我无法让它正确显示。。。但是没有告诉我们您当前的显示器有什么问题,或者您试图如何显示它。请提供更突出的细节。此外,如果您试图同时显示多个Book对象,一种非常常见的方法是使用JTable。你在尝试这个吗?为了更快地获得更好的帮助,请发布一个MCTRE。你能解释一下应该出现什么以及什么时候出现吗?在代码中的那个点上,范围内没有名为myTextArea的变量。但是,有一个名为textArea的,但这可能无法解决OP的问题。正在尝试在代码块prepareDisplay中的字符串之后显示总库存量。我能够修复它,很抱歉造成混淆。我需要货币格式的所有书籍的总库存价值显示在总库存价值之后:
class EBook extends Book implements Comparable<Book> {
private String website;
//constructor and super
public EBook(long isbn, String title, String autName, int year,
String pubName, double price, String website) {
super(isbn, title, autName, year, pubName, price);
setWebsite(website);
}
// get and set
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public double discount() {
return price * 0.10;
}
DecimalFormat money = new DecimalFormat("$0.00");
//toString for the Book array
public String toString() {
return "ISBM: " + isbn + "\n"
+ "Title: " + title + "\n"
+ "Author's Name: " + autName + "\n"
+ "Year Published: " + year + "\n"
+ "Publisher's Name: " + pubName + "\n"
+ "Price: " + money.format(price) + "\n"
+ "Website: " + website + "\n"
+ "Discount: " + money.format(discount()) + "\n\n";
}
//comparable for array
public int compareTo(Book otherBook) {
return title.compareTo(otherBook.getTitle());
}
}//end class EBook
public class BookStore {
//total price of inventory method
public static double calculateInventoryTotal(Book[] books) {
double total = 0;
for (int i = 0; i < books.length; i++) {
total += books[i].getPrice();
}
return total;
}
static int bookIndex = 0;
public static JTextArea prepareDisplay(Book myBook, JTextArea myTextArea) {
DecimalFormat money = new DecimalFormat("$0.00");
myTextArea.setText("");
myTextArea.append("Book Store Inventory: \n\n");
myTextArea.append(myBook.toString());
myTextArea.append("Total Inventory Value: ");
return myTextArea;
}
//main method
public static void main(String[] args) {
// book array
final Book[] inventory = new Book[5];
Book a = new Book(7423540089L, "No David!", "David Shannon", 2009, "Shannon Rock", 12.99);
EBook b = new EBook(75260012L, "David goes to School", "David Shannon", 2010, "Shannon Rock", 11.98, "http://www.tinyurl.qqwert67o9");
Book c = new Book(743200616L, "Simple Abundance", "Sarah Breathnach", 2009, "Scribner", 14.99);
EBook d = new EBook(78137521819L, "The very hungry caterpillar", "Eric Carle", 2005, "Philomel Books", 13.99, "http://www.tinyurl.fguopt8u90");
Book e = new Book(9781416987116L, "We're going on a bear hunt", "Michael Rosen", 2009, "McElderry", 15.99);
inventory[0] = a;
inventory[1] = b;
inventory[2] = c;
inventory[3] = d;
inventory[4] = e;
Arrays.sort(inventory);
//parameters of the JTextArea
final JTextArea textArea = new JTextArea(15, 15);
textArea.setText("");
textArea.setEditable(false);
DecimalFormat money = new DecimalFormat("$0.00");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 3));
JButton firstButton = new JButton("First");
buttonPanel.add(firstButton);
firstButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookIndex = 0;
prepareDisplay(inventory[bookIndex], textArea);
}
});
JButton nextButton = new JButton("Next");
buttonPanel.add(nextButton);
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookIndex++;
if (bookIndex >= inventory.length) {
bookIndex = 0;
}
prepareDisplay(inventory[bookIndex], textArea);
}
});
JButton previousButton = new JButton("Previous");
buttonPanel.add(previousButton);
previousButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookIndex--;
if (bookIndex < 0) {
bookIndex = inventory.length - 1;
}
prepareDisplay(inventory[bookIndex], textArea);
}
});
JButton lastButton = new JButton("Last");
buttonPanel.add(lastButton);
lastButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookIndex = (inventory.length - 1);
prepareDisplay(inventory[bookIndex], textArea);
}
});
JLabel logoLabel = new JLabel(new ImageIcon("bluehills.jpg"));
JPanel logoPanel = new JPanel();
logoPanel.add(logoLabel);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
centerPanel.add(prepareDisplay(inventory[bookIndex], textArea)); //show the first employee
//calculate inventory total
double inventoryTotal = calculateInventoryTotal(inventory);
//myTextArea.append("Total Inventory Value: " + money.format(inventoryTotal) + "\n\n");
//set the GUI window with the parameters
JFrame frame = new JFrame("Book Store Program");
frame.setLayout(new BorderLayout()); // set layout
frame.add(logoPanel, BorderLayout.NORTH); //add logo
frame.add(buttonPanel, BorderLayout.SOUTH); // add previous and next button to frame
frame.add(centerPanel, BorderLayout.CENTER); // add center panel to frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}// end class
double inventoryTotal = calculateInventoryTotal(inventory);