You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java自定义计算器故障排查:Swing计算按钮点击后价格不显示问题

Java Swing 成本计算器问题修复

核心错误点

  • 显示估算价格的estimate JLabel组件没有添加到面板中,界面上不存在该控件,自然看不到价格输出
  • 监听器逻辑缺失计算按钮的触发分支,最后一个判断分支重复判断输入框linear,点击计算按钮没有对应执行逻辑
  • 下拉框取值错误:调用getToolTipText()获取选中项是错误用法,应该调用getSelectedItem()强转为字符串
  • 插座选项取值后直接尝试转整数,会直接抛出NumberFormatException异常,逻辑中断
  • 成本计算完成后,没有手动更新estimate组件的显示文本,Swing不会自动同步变量值到界面
  • 布局空边设置过大(400px上下左边距),导致控件显示异常
  • 字符串比较混用==和equals,字符串内容判断必须使用equals方法
  • 重复给calculate按钮添加监听器,属于冗余代码

修正后的完整代码

package packagepackage;

import packagepackage.HintTextFieldUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CP_GUI implements ActionListener {
    String[] sumpOptions = {"New sump pump", "Existing sump pump"};
    String[] elecOptions = {"There is an outlet within 6 feet of sump pump", "There is no outlet nearby, or I do not need a new one"};
    private JComboBox<String> sump = new JComboBox<>(sumpOptions);
    private JComboBox<String> elec = new JComboBox<>(elecOptions);
    private JTextField linear = new JTextField();
    private JTextField prep = new JTextField();
    private JLabel title = new JLabel("Drain Tile Calculator");
    private JButton calculate = new JButton("Calculate!");
    public JLabel estimate = new JLabel("Estimated Cost: $0");

    private JFrame frame = new JFrame();

    public CP_GUI() {
        // 面板设置
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
        panel.setLayout(new GridLayout(0, 1, 10, 10));
        panel.add(title);
        panel.add(linear);
        panel.add(sump);
        panel.add(elec);
        panel.add(prep);
        panel.add(calculate);
        panel.add(estimate);

        // 监听器绑定
        calculate.addActionListener(this);

        // 窗口设置
        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Drain Tile Calculator");
        frame.pack();
        frame.setVisible(true);

        // 输入框提示文字
        linear.setUI(new HintTextFieldUI("Perimeter length", true));
        prep.setUI(new HintTextFieldUI("Hours of preptime", true));
    }

    public static void main(String[] args) {
        new CP_GUI();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == calculate) {
            int totalCost = 0;
            try {
                // 计算周长成本
                int perimeter = Integer.parseInt(linear.getText().trim());
                totalCost += perimeter * 13;

                // 计算水泵成本
                String selectedSump = (String) sump.getSelectedItem();
                if ("New sump pump".equals(selectedSump)) {
                    totalCost += 260;
                }

                // 计算插座成本
                String selectedElec = (String) elec.getSelectedItem();
                if ("There is an outlet within 6 feet of sump pump".equals(selectedElec)) {
                    totalCost += 280;
                }

                // 计算准备时长成本
                int prepHours = Integer.parseInt(prep.getText().trim());
                totalCost += (prepHours + 1) * 110;

                // 更新显示
                estimate.setText(String.format("Estimated Cost: $%d", totalCost));
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(frame, "请输入合法的数字参数!");
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Johnxkeefe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 03:36:03