一次测试
This commit is contained in:
@@ -37,6 +37,7 @@ public class SalesProductManagement extends JFrame implements ActionListener {
|
||||
text10=new JTextField(5);
|
||||
|
||||
btn1=new JButton("结算");
|
||||
btn1.addActionListener(this);
|
||||
|
||||
|
||||
add(panel1);
|
||||
@@ -70,9 +71,35 @@ public class SalesProductManagement extends JFrame implements ActionListener {
|
||||
setSize(500,500);
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 添加窗口关闭功能
|
||||
}
|
||||
@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) {
|
||||
new SalesProductManagement();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user