Write a class named ProductSystem, which extends JFrame class. The appearance of the application looks like the following (as shown in Figure 1). 2. At the beginning, input name,price and number at three JTextField fields, then click “Add”button (event source), which binds to a method (event listener) that create a Product object (using name,price and number) and add it into ArrayList<Product>, at the same time display “Add Product xxx sucess!” in JTextArea on the GUI (as shown in Figure 2) 3. When click “Display” button, then read the records from ArrayList , and display the records in JTextArea on the GUI(as shown in Figure 3). 4. When click “Save to File” button, then write the records from ArrayList to file product.txt, at the same time display "file write success" in JTextArea on the GUI (as shown in Figure 4)
时间: 2024-03-08 20:46:32 浏览: 132
Sure, I understand your question. You want me to write a class named ProductSystem, which extends JFrame class and performs the following tasks:
1. The appearance of the application should look like the following (as shown in Figure 1).
2. At the beginning, input name, price and number at three JTextField fields, then click “Add” button (event source), which binds to a method (event listener) that creates a Product object (using name, price, and number) and adds it to ArrayList<Product>, at the same time display “Add Product xxx success!” in JTextArea on the GUI (as shown in Figure 2).
3. When click “Display” button, then read the records from ArrayList, and display the records in JTextArea on the GUI (as shown in Figure 3).
4. When click “Save to File” button, then write the records from ArrayList to file product.txt, at the same time display "file write success" in JTextArea on the GUI (as shown in Figure 4).
Here is the code for the ProductSystem class:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
class Product {
private String name;
private double price;
private int number;
public Product(String name, double price, int number) {
this.name = name;
this.price = price;
this.number = number;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getNumber() {
return number;
}
public String toString() {
return String.format("%-20s $%.2f %d", name, price, number);
}
}
public class ProductSystem extends JFrame implements ActionListener {
private ArrayList<Product> productList = new ArrayList<>();
private JLabel nameLabel = new JLabel("Name:");
private JTextField nameField = new JTextField(20);
private JLabel priceLabel = new JLabel("Price:");
private JTextField priceField = new JTextField(20);
private JLabel numberLabel = new JLabel("Number:");
private JTextField numberField = new JTextField(20);
private JButton addButton = new JButton("Add");
private JButton displayButton = new JButton("Display");
private JButton saveButton = new JButton("Save to File");
private JTextArea textArea = new JTextArea(10, 30);
public ProductSystem() {
setTitle("Product System");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(10, 10, 10, 10);
constraints.gridx = 0;
constraints.gridy = 0;
panel.add(nameLabel, constraints);
constraints.gridx = 1;
panel.add(nameField, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
panel.add(priceLabel, constraints);
constraints.gridx = 1;
panel.add(priceField, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
panel.add(numberLabel, constraints);
constraints.gridx = 1;
panel.add(numberField, constraints);
constraints.gridx = 0;
constraints.gridy = 3;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.CENTER;
panel.add(addButton, constraints);
constraints.gridx = 0;
constraints.gridy = 4;
constraints.gridwidth = 2;
panel.add(displayButton, constraints);
constraints.gridx = 0;
constraints.gridy = 5;
constraints.gridwidth = 2;
panel.add(saveButton, constraints);
add(panel, BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
addButton.addActionListener(this);
displayButton.addActionListener(this);
saveButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == addButton) {
String name = nameField.getText();
double price = Double.parseDouble(priceField.getText());
int number = Integer.parseInt(numberField.getText());
Product product = new Product(name, price, number);
productList.add(product);
textArea.append(String.format("Add Product %s success!\n", product.getName()));
} else if (event.getSource() == displayButton) {
textArea.setText("");
for (Product product : productList) {
textArea.append(product.toString() + "\n");
}
} else if (event.getSource() == saveButton) {
try (PrintWriter out = new PrintWriter(new FileWriter("product.txt"))) {
for (Product product : productList) {
out.println(product.getName() + "," + product.getPrice() + "," + product.getNumber());
}
textArea.append("File write success!\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ProductSystem system = new ProductSystem();
system.setVisible(true);
}
}
```
In the above code, the Product class represents a product with its name, price, and number. The ProductSystem class extends the JFrame class and implements the ActionListener interface to handle events from buttons.
The GUI of the application is created using GridBagLayout and contains three text fields for name, price, and number, and three buttons for adding, displaying, and saving products. The JTextArea is used to display the results of adding, displaying, and saving products.
When the user clicks the "Add" button, the actionPerformed() method reads the values from the text fields, creates a new Product object, adds it to the productList ArrayList, and displays the message "Add Product xxx success!" in the JTextArea.
When the user clicks the "Display" button, the actionPerformed() method reads the records from the productList ArrayList and displays them in the JTextArea.
When the user clicks the "Save to File" button, the actionPerformed() method writes the records from the productList ArrayList to a file named product.txt and displays the message "File write success!" in the JTextArea.
I hope this code helps you out! Let me know if you have any questions.
阅读全文