weixin_43939819 2019-12-20 00:41 采纳率: 100%
浏览 340
已采纳

java 为什么我写的代码中内部类不能够改变外部类成员的值?

1.在做用Swing组件往数据库插入数据的实验时,我为JRadioButon添加了一个用内部类写的点击事件,用外部类的Radiotext成员获取JRadioButton的text内容并传入数据库,然而在反复调试的过程中Radiotext的值始终为初始值null,有朋友知道怎么解决吗?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;

public class JudgeEdit extends BaseFrameEdit {

    private JTextArea txtQuestion;
    private JRadioButton rbtnT;
    private JRadioButton rbtnF;
    private String radioText = null;
    private String txtQuestionText;
    private radioAction RadioAction = new radioAction();
    private PreparedStatement sql;


    public JudgeEdit(){
        super.setTitle("添加判断题");
    }


    protected void addControls() {
        //题目
        JLabel lblName = new JLabel("题目:");
        lblName.setBounds(50, 10, 50, 25);
        super.add(lblName);

        txtQuestion = new JTextArea();
        txtQuestion.setBounds(100, 10, 200, 270);
        super.add(txtQuestion);

        //答案
        JLabel lblAnswer = new JLabel("答案:");
        lblAnswer.setBounds(50, 285, 50, 25);
        super.add(lblAnswer);

        rbtnT = new JRadioButton("正确");
        rbtnT.setBounds(100, 285, 100, 25);
        rbtnT.addActionListener(RadioAction);
        super.add(rbtnT);

        rbtnF = new JRadioButton("错误");
        rbtnF.setBounds(200, 285, 100, 25);
        rbtnF.addActionListener(RadioAction);
        super.add(rbtnF);

        ButtonGroup btnGroup = new ButtonGroup();
        btnGroup.add(rbtnT);
        btnGroup.add(rbtnF);



        //保存
        JButton btnSave = new JButton("保存");
        btnSave.setBounds(170, 320, 60, 25);
        btnSave.addActionListener(new btnSaveAction());
        super.add(btnSave);
    }




    class radioAction implements ActionListener{
        public void actionPerformed(ActionEvent arg0){


            if(rbtnT.isSelected()){
                 radioText = rbtnT.getText();
                 System.out.println(radioText);
            }
            if(rbtnF.isSelected()){
                radioText = rbtnF.getText();
                System.out.println(radioText);
            }
        }
    }

    class btnSaveAction implements ActionListener{
        public void actionPerformed(ActionEvent e){
            txtQuestionText = txtQuestion.getText();
            Conn c = new Conn();
            Connection con = c.getConnection();
            try{
                sql = con.prepareStatement("insert into JudgeList(JText,JAnswer)" + "values(?,?)");
                sql.setString(1,txtQuestionText);
                sql.setString(2,radioText);
                sql.executeUpdate();
                System.out.println("添加判断题成功!");
            }
            catch(Exception e2){
                e2.printStackTrace();
            }
        }
    }
}

  • 写回答

1条回答

  • 王能 2019-12-23 11:16
    关注

    可以肯定的是“System.out.println(radioText);没”有执行或,者打印在了“System.out.println("添加判断题成功!");”这句话的后面。

    问答版主就是牛,从现在起只回论坛,不在踏入问答半步

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器