一次测试
This commit is contained in:
@@ -1,104 +1,127 @@
|
|||||||
package com.example;
|
package com.example;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import javax.swing.table.DefaultTableModel;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class SalesProductManagement extends JFrame implements ActionListener {
|
public class SalesProductManagement extends JFrame implements ActionListener {
|
||||||
|
private JTable table;
|
||||||
|
private DefaultTableModel tableModel;
|
||||||
|
private JTextField idField, nameField, phoneField, addressField;
|
||||||
|
private JButton addBtn;
|
||||||
|
|
||||||
JButton btn1;
|
public SalesProductManagement() {
|
||||||
JLabel lab1,lab2,lab3,lab4,lab5,lab6,lab7;
|
|
||||||
JTextField text2,text3,text5,text6,text8,text9,text10;
|
|
||||||
// JPasswordField passwordfile;
|
|
||||||
JPanel panel1,panel2,panel3;
|
|
||||||
|
|
||||||
|
setTitle("商品信息管理");
|
||||||
|
setSize(800, 600);
|
||||||
SalesProductManagement(){
|
|
||||||
super("销售信息管理");
|
|
||||||
panel1 = new JPanel();
|
|
||||||
panel2 = new JPanel();
|
|
||||||
panel3 = new JPanel();
|
|
||||||
lab1=new JLabel("名称",JLabel.CENTER);
|
|
||||||
lab2=new JLabel("单价",JLabel.CENTER);
|
|
||||||
lab3=new JLabel("数量",JLabel.CENTER);
|
|
||||||
lab4=new JLabel("总计:",JLabel.CENTER);
|
|
||||||
lab5=new JLabel("苹果",JLabel.CENTER);
|
|
||||||
lab6=new JLabel("香蕉",JLabel.CENTER);
|
|
||||||
lab7=new JLabel("橘子",JLabel.CENTER);
|
|
||||||
|
|
||||||
text2=new JTextField("2"+"元",JTextField.CENTER);
|
|
||||||
text3=new JTextField(3);
|
|
||||||
text5=new JTextField("3"+"元",JTextField.CENTER);
|
|
||||||
text6=new JTextField(3);
|
|
||||||
text8=new JTextField("5"+"元",JTextField.CENTER);
|
|
||||||
text9=new JTextField(3);
|
|
||||||
text10=new JTextField(5);
|
|
||||||
|
|
||||||
btn1=new JButton("结算");
|
|
||||||
btn1.addActionListener(this);
|
|
||||||
|
|
||||||
|
|
||||||
add(panel1);
|
|
||||||
panel1.setLayout(new GridLayout(1,3,5,5));
|
|
||||||
panel1.add(lab1);
|
|
||||||
panel1.add(lab2);
|
|
||||||
panel1.add(lab3);
|
|
||||||
panel1.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));
|
|
||||||
add(panel2);
|
|
||||||
panel2.setLayout(new GridLayout(3,3,5,5));
|
|
||||||
panel2.add(lab5);
|
|
||||||
panel2.add(text2);
|
|
||||||
panel2.add(text3);
|
|
||||||
panel2.add(lab6);
|
|
||||||
panel2.add(text5);
|
|
||||||
panel2.add(text6);
|
|
||||||
panel2.add(lab7);
|
|
||||||
panel2.add(text8);
|
|
||||||
panel2.add(text9);
|
|
||||||
panel2.setMaximumSize(new Dimension(Integer.MAX_VALUE, 120));
|
|
||||||
add(panel3);
|
|
||||||
panel3.setLayout(new GridLayout(1,3,5,5));
|
|
||||||
panel3.add(btn1);
|
|
||||||
panel3.add(lab4);
|
|
||||||
panel3.add(text10);
|
|
||||||
panel3.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
setLayout(new GridLayout(3,1));
|
|
||||||
setSize(500,500);
|
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
|
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||||
|
|
||||||
|
// 创建主面板
|
||||||
|
JPanel mainPanel = new JPanel(new BorderLayout());
|
||||||
|
|
||||||
|
// 创建表格面板
|
||||||
|
JPanel tablePanel = new JPanel(new BorderLayout());
|
||||||
|
tablePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
||||||
|
|
||||||
|
// 创建表格模型
|
||||||
|
String[] columnNames = {"名称", "单价", "购买数量"};
|
||||||
|
tableModel = new DefaultTableModel(columnNames, 0);
|
||||||
|
table = new JTable(tableModel);
|
||||||
|
table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
||||||
|
table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
|
||||||
|
table.setRowHeight(25);
|
||||||
|
|
||||||
|
// 添加滚动面板
|
||||||
|
JScrollPane scrollPane = new JScrollPane(table);
|
||||||
|
tablePanel.add(scrollPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
// 创建输入面板
|
||||||
|
JPanel inputPanel = new JPanel(new GridLayout(1, 2, 10, 10));
|
||||||
|
inputPanel.setBorder(BorderFactory.createTitledBorder("结算信息"));
|
||||||
|
inputPanel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
||||||
|
|
||||||
|
addBtn = new JButton("结算");
|
||||||
|
addBtn.addActionListener(this);
|
||||||
|
|
||||||
|
|
||||||
|
inputPanel.add(addBtn);
|
||||||
|
|
||||||
|
inputPanel.add(new JLabel("共计:"));
|
||||||
|
addressField = new JTextField();
|
||||||
|
inputPanel.add(addressField);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 组装面板
|
||||||
|
mainPanel.add(tablePanel, BorderLayout.CENTER);
|
||||||
|
mainPanel.add(inputPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
add(mainPanel);
|
||||||
|
|
||||||
|
loadData();
|
||||||
|
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
|
|
||||||
}
|
}
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
// 判断事件源是否为结算按钮
|
|
||||||
if (e.getSource() == btn1) {
|
|
||||||
try {
|
|
||||||
// 获取单价(从文本框提取数字部分)
|
|
||||||
double applePrice = Double.parseDouble(text2.getText().replace("元", ""));
|
|
||||||
double bananaPrice = Double.parseDouble(text5.getText().replace("元", ""));
|
|
||||||
double orangePrice = Double.parseDouble(text8.getText().replace("元", ""));
|
|
||||||
|
|
||||||
// 获取数量(处理空输入情况)
|
//主类,调试
|
||||||
int appleCount = text3.getText().trim().isEmpty() ? 0 : Integer.parseInt(text3.getText().trim());
|
|
||||||
int bananaCount = text6.getText().trim().isEmpty() ? 0 : Integer.parseInt(text6.getText().trim());
|
|
||||||
int orangeCount = text9.getText().trim().isEmpty() ? 0 : Integer.parseInt(text9.getText().trim());
|
|
||||||
|
|
||||||
// 计算总金额
|
|
||||||
double total = (applePrice * appleCount) + (bananaPrice * bananaCount) + (orangePrice * orangeCount);
|
|
||||||
|
|
||||||
// 显示结果,保留两位小数
|
|
||||||
text10.setText(String.format("%.2f元", total));
|
|
||||||
} catch (NumberFormatException ex) {
|
|
||||||
// 处理输入非数字的情况
|
|
||||||
JOptionPane.showMessageDialog(this, "请输入有效的数字", "输入错误", JOptionPane.ERROR_MESSAGE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new SalesProductManagement();
|
new SalesProductManagement();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
private void loadData() {
|
||||||
|
Jdbc jdbc = new Jdbc();
|
||||||
|
try {
|
||||||
|
ResultSet rs = jdbc.query("SELECT name,selling_price,quantity_sold FROM commodity");
|
||||||
|
// 清空旧数据
|
||||||
|
tableModel.setRowCount(0);
|
||||||
|
// 填充新数据
|
||||||
|
while (rs.next()) {
|
||||||
|
Object[] row = {
|
||||||
|
rs.getString("name"),
|
||||||
|
rs.getDouble("selling_price"),
|
||||||
|
rs.getInt("quantity_sold"),
|
||||||
|
};
|
||||||
|
tableModel.addRow(row);
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
jdbc.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
JOptionPane.showMessageDialog(this, "加载数据失败: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if(e.getSource() == addBtn) {
|
||||||
|
double total = 0.0; // 总金额
|
||||||
|
|
||||||
|
// 获取表格的行数
|
||||||
|
int rowCount = tableModel.getRowCount();
|
||||||
|
|
||||||
|
// 遍历每一行数据
|
||||||
|
for(int i = 0; i < rowCount; i++) {
|
||||||
|
// 获取单价(第1列,索引从0开始)
|
||||||
|
double price = (double) tableModel.getValueAt(i, 1);
|
||||||
|
// 获取数量(第2列)
|
||||||
|
int quantity = (int) tableModel.getValueAt(i, 2);
|
||||||
|
|
||||||
|
// 计算该行的金额并累加到总金额
|
||||||
|
total += price * quantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 在文本框中显示总金额
|
||||||
|
addressField.setText(String.format("%.2f", total) + " 元");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user