java小程序 if(buttonG.getSelection() == BM) 输出问题

来源:百度知道 编辑:UC知道 时间:2024/06/22 23:45:46
在做一个简单的 个人简历 程序 在输出的时候 性别显示null 不知道怎么修改 求解 程序太长 截取了一段 就是中间 sex 那里 输出的时候 不显示男女 显示null

//OK按钮事件
ok.addActionListener(new Yes());
cancel.addActionListener(new Cancel());
buttonG = new ButtonGroup();
buttonG.add(BM);
buttonG.add(BF);
setSize(280, 200);
setLocation(300,200);
setVisible(true);
}

public static void main(String args[])
{
Person per = new Person();
//per.addWindowListener(new Handler2());
}

private class Yes implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//按下OK后
String personName = name.getText().trim();//从

不要那样用比较好。
最好是下边判断
BM.isSelected() -> sex = "男"
BF.isSelected() -> sex = "女"

说明buttonG.getSelection()既不等于BM也不等于BF,你再好好检查一下

if(buttonG.getSelection() == BM)
{
sex = "男";
}
else if(buttonG.getSelection() == BF)
{
sex = "女";

}

不能直接用“==”判断,判断方式错误

import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class CTextArea implements ActionListener
{

JRadioButton bm;
JRadioButton bf;
JButton bt;
JFrame f;
ButtonGroup buttonG;
public CTextArea()
{
f= new JFrame("新建 文本文档.txt -记事本");
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
Container c=f.getContentPane();
c.setLayout(new FlowLayout());
bm=new JRadioButton("男生",true);
bf=new JRadioButton("女生");
bt=new JBut