Button sendButton = new JButton("发出"); sendButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String customer = customerTextField.getText(); String paymentStr = paymentTextField.getText(); String getperson = receiverTextField.getText(); String sendperson = senderTextField.getText(); String trackingNumber = trackingNumberTextField.getText(); // get tracking number from new text field if (customer.isEmpty() || paymentStr.isEmpty()) { return; } double payment = Double.parseDouble(paymentStr); totalIncome += payment; tableModel.addRow(new Object[]{trackingNumber, customer,payment,sendperson,getperson}); // add tracking number to table customerTextField.setText(""); paymentTextField.setText(""); trackingNumberTextField.setText(""); receiverTextField.setText(""); senderTextField.setText("");; trackingNumberTextField.setText(""); // clear tracking number text field try { BufferedWriter writer = new BufferedWriter(new FileWriter("sendexpress.txt", true)); writer.write(trackingNumber + ", "+ customer + ", " + payment + ", " + sendperson + ", " + getperson); writer.newLine(); writer.close(); } catch (IOException ex) { ex.printStackTrace(); } } });和xpressTable = new JTable(); expressTable.setModel(new DefaultTableModel( new Object[][] {}, new String[] {"快递单号", "类别/物品", "金额", "寄件人", "收件人"} // added columns for tracking number, sender, and receiver )); tableModel = (DefaultTableModel) expressTable.getModel(); scrollPane.setViewportView(expressTable);单独封装成两个类
时间: 2024-02-14 18:29:01 浏览: 82
您的代码可以封装成两个类,一个是用于创建界面和处理按钮点击事件的类,另一个是用于管理数据和文件操作的类。
第一个类可以命名为`ExpressForm`,其中包含以下内容:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class ExpressForm {
private JTextField customerTextField;
private JTextField paymentTextField;
private JTextField senderTextField;
private JTextField receiverTextField;
private JTextField trackingNumberTextField;
private JButton sendButton;
private JTable expressTable;
private JScrollPane scrollPane;
private DefaultTableModel tableModel;
private double totalIncome;
public ExpressForm() {
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String customer = customerTextField.getText();
String paymentStr = paymentTextField.getText();
String getperson = receiverTextField.getText();
String sendperson = senderTextField.getText();
String trackingNumber = trackingNumberTextField.getText();
if (customer.isEmpty() || paymentStr.isEmpty()) {
return;
}
double payment = Double.parseDouble(paymentStr);
totalIncome += payment;
tableModel.addRow(new Object[]{trackingNumber, customer, payment, sendperson, getperson});
customerTextField.setText("");
paymentTextField.setText("");
trackingNumberTextField.setText("");
receiverTextField.setText("");
senderTextField.setText("");
trackingNumberTextField.setText("");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("sendexpress.txt", true));
writer.write(trackingNumber + ", " + customer + ", " + payment + ", " + sendperson + ", " + getperson);
writer.newLine();
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("Express Form");
frame.setContentPane(new ExpressForm().scrollPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
```
第二个类可以命名为`ExpressManager`,用于管理数据和文件操作:
```java
import javax.swing.table.DefaultTableModel;
public class ExpressManager {
private DefaultTableModel tableModel;
private double totalIncome;
public ExpressManager(DefaultTableModel tableModel) {
this.tableModel = tableModel;
this.totalIncome = 0.0;
}
// 添加数据到表格和文件
public void addData(String trackingNumber, String customer, double payment, String sendperson, String getperson) {
totalIncome += payment;
tableModel.addRow(new Object[]{trackingNumber, customer, payment, sendperson, getperson});
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("sendexpress.txt", true));
writer.write(trackingNumber + ", " + customer + ", " + payment + ", " + sendperson + ", " + getperson);
writer.newLine();
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
// 获取总收入
public double getTotalIncome() {
return totalIncome;
}
}
```
您可以将第一个类的`main`方法作为入口点运行,这样就可以创建一个包含界面和按钮点击事件处理的窗口。第二个类用于数据管理和文件操作,可以在第一个类的按钮点击事件中调用相应的方法来添加数据到表格和文件。
请注意,这只是一个简单的示例,您可能需要根据实际需求进行适当的修改和调整。
阅读全文