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 09:46:14 浏览: 61
Sure, I can help you with that. Here's an example code for the ProductSystem class:
```
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class ProductSystem extends JFrame {
private JTextField nameField;
private JTextField priceField;
private JTextField numberField;
private JTextArea displayArea;
private ArrayList<Product> productList;
public ProductSystem() {
super("Product System");
// Create GUI components
JLabel nameLabel = new JLabel("Name:");
JLabel priceLabel = new JLabel("Price:");
JLabel numberLabel = new JLabel("Number:");
nameField = new JTextField(10);
priceField = new JTextField(10);
numberField = new JTextField(10);
JButton addButton = new JButton("Add");
addButton.addActionListener(new AddButtonListener());
JButton displayButton = new JButton("Display");
displayButton.addActionListener(new DisplayButtonListener());
JButton saveButton = new JButton("Save to File");
saveButton.addActionListener(new SaveButtonListener());
displayArea = new JTextArea(10, 30);
displayArea.setEditable(false);
// Add components to content pane
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(priceLabel);
inputPanel.add(priceField);
inputPanel.add(numberLabel);
inputPanel.add(numberField);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(displayButton);
buttonPanel.add(saveButton);
JScrollPane scrollPane = new JScrollPane(displayArea);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
mainPanel.add(scrollPane, BorderLayout.SOUTH);
getContentPane().add(mainPanel);
// Initialize product list
productList = new ArrayList<Product>();
// Set window properties
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setVisible(true);
}
private class AddButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
// Get input values
String name = nameField.getText();
double price = Double.parseDouble(priceField.getText());
int number = Integer.parseInt(numberField.getText());
// Create product object
Product product = new Product(name, price, number);
// Add product to list
productList.add(product);
// Update display area
displayArea.append("Add Product " + product.getName() + " success!\n");
// Clear input fields
nameField.setText("");
priceField.setText("");
numberField.setText("");
}
}
private class DisplayButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
// Clear display area
displayArea.setText("");
// Display product list
for (Product product : productList) {
displayArea.append(product.toString() + "\n");
}
}
}
private class SaveButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
try {
// Open file for writing
FileOutputStream fileStream = new FileOutputStream("product.txt");
PrintWriter writer = new PrintWriter(fileStream);
// Write product list to file
for (Product product : productList) {
writer.println(product.toString());
}
// Close file
writer.close();
fileStream.close();
// Display success message
displayArea.append("File write success!\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ProductSystem();
}
}
```
Note that the Product class is assumed to be already defined elsewhere. You may need to modify the code to match your specific requirements.
阅读全文