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

Java Swing如何将JFormattedTextField.getText()返回值转换为自定义类型

问题解答

关于「转Object再向下转型」的可行性

该方案完全不可行。
Java的类型转换只支持存在继承/实现关系的类之间操作,String类和自定义Matiére类没有任何继承关系,即使将String对象先赋值给Object类型变量,其底层本质仍然是String实例,强转为Matiére类型只会直接抛出ClassCastException,无法实现需求。

正确实现方案

你需要根据业务场景二选一:

场景1:用户输入的是已存在的Matiére实例的标识(如名称、编号)

通常你的Centre de formation实例C中会维护所有可用的Matiére实例列表,你可以根据用户输入的标识匹配到对应的Matiére实例:

示例逻辑:

// 先拿到用户输入的Matiére名称
String matiereNom = testField4.getText();
Matiere targetMatiere = null;
// 从C中获取所有已有的Matiére实例遍历匹配
for (Matiere m : C.getListeMatieres()) {
    if (m.getNom().equals(matiereNom)) {
        targetMatiere = m;
        break;
    }
}
// 匹配成功再构造formation
if (targetMatiere != null) {
    formation R = new formation(nom,duree,prix,targetMatiere);
    C.ajouter_forma(R);
} else {
    // 可以弹提示告诉用户输入的Matiére不存在
    JOptionPane.showMessageDialog(f, "输入的学科不存在");
}

场景2:用户输入的是要新建的Matiére实例的参数

如果业务允许用户输入名称直接创建新的Matiére实例,直接调用Matiére类的构造方法即可:

示例逻辑:

String matiereNom = testField4.getText();
// 直接构造新的Matiére实例,根据你的Matiére构造参数调整即可
Matiere newMatiere = new Matiere(matiereNom);
formation R = new formation(nom,duree,prix,newMatiere);
C.ajouter_forma(R);

你当前代码的其他明显问题

  • 取值逻辑错误:
    • 价格prix取了testField2(时长输入框)的内容,应该取testField3.getText()
    • 学科字符串取了testField1(课程名称输入框)的内容,应该取testField4.getText()
  • 取值时机错误:你现在创建文本框后立刻调用getText(),拿到的是你设置的默认提示文本,用户还没有输入内容。正确逻辑应该是给弹窗加一个「确认提交」按钮,用户点击按钮后再获取各个输入框的内容构造对象。
  • 输入合法性校验缺失:用户输入的时长、价格如果不是数字,调用Float.parseFloat会直接抛出异常,建议加try-catch捕获异常,给用户友好提示。
  • 命名规范问题:Java规范要求类名首字母大写,建议将formation、Matiére调整为Formation、Matiere,便于代码维护。

调整后的参考代码

bouton_f0.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (!bouton_f0.getModel().isPressed()) {
            /*ajouter formation*/
            JFrame f = new JFrame("Ajout d'une formation");
            f.setSize(500, 200);
            JPanel pannel = new JPanel();
            JTextField testField1 = new JTextField("nom formation", 15);
            JTextField testField2 = new JTextField("duree formation", 15);
            JTextField testField3 = new JTextField("prix formation", 15);
            JTextField testField4 = new JTextField("Matiére initiale", 15);
            JButton confirmBtn = new JButton("确认添加");
            
            // 确认按钮点击后再取值
            confirmBtn.addActionListener(confirmEvent -> {
                try {
                    String nom = testField1.getText();
                    float duree = Float.parseFloat(testField2.getText());
                    float prix = Float.parseFloat(testField3.getText());
                    String matiereNom = testField4.getText();
                    
                    // 这里根据你的业务选上面两种场景之一构造Matiere实例,下面以新建为例
                    Matiere matiere = new Matiere(matiereNom);
                    formation R = new formation(nom, duree, prix, matiere);
                    C.ajouter_forma(R);
                    
                    // 关闭弹窗
                    f.dispose();
                    
                    // 刷新原有面板
                    panneau.removeAll();
                    panneau.revalidate();
                    panneau.repaint();
                    setContentPane(p_forma);
                    setSize(600,150);
                    setVisible(true); 
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(f, "时长和价格请输入合法数字");
                }
            });
            
            pannel.add(testField1);
            pannel.add(testField2);
            pannel.add(testField3);
            pannel.add(testField4);
            pannel.add(confirmBtn);
            f.getContentPane().add(pannel);
            f.setVisible(true);
        }
    }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 00:27:02