各位大神求帮助,尽快,谢谢

liandan1114 2011-12-17 11:27:22
为什么第九行,按键设置背景色会错误,正确应该怎么样,谢谢各位大神
import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;

public class IdealWeight extends JFrame implements ActionListener
{
JRadioButton genderM, genderF;
genderM.setBackground(Color.white);
ButtonGroup genderGroup;
JPanel genderPanel;

JRadioButton heightA, heightB, heightC, heightD, heightE;
ButtonGroup heightGroup;
JPanel heightPanel;

JTextField resultText;
JLabel resultLabl;
JPanel resultPanel;

int sex=28,h=62;
double out;
String str;

public IdealWeight()
{

setTitle( "Your Ideal Weight" );
setDefaultCloseOperation( EXIT_ON_CLOSE );

// gender group
genderM = new JRadioButton("Male", true );
genderF = new JRadioButton("Female", false );
genderGroup = new ButtonGroup();
genderGroup.add( genderM ); genderGroup.add( genderF );
genderPanel = new JPanel();
genderPanel.setLayout(
new BoxLayout( genderPanel, BoxLayout.Y_AXIS ) );
genderPanel.add( new JLabel("Your Gender") );
genderPanel.add( genderM ); genderPanel.add( genderF );

// height group
heightA = new JRadioButton("60 to 64 inches", true );
heightB = new JRadioButton("64 to 68 inches", false );
heightC = new JRadioButton("68 to 72 inches", false );
heightD = new JRadioButton("72 to 76 inches", false );
heightE = new JRadioButton("76 to 80 inches", false );

heightGroup = new ButtonGroup();
heightGroup.add( heightA ); heightGroup.add( heightB );
heightGroup.add( heightC ); heightGroup.add( heightD );
heightGroup.add( heightE );

heightPanel = new JPanel();
heightPanel.setLayout(
new BoxLayout( heightPanel, BoxLayout.Y_AXIS ) );
heightPanel.add( new JLabel("Your Height") );
heightPanel.add( heightA ); heightPanel.add( heightB );
heightPanel.add( heightC ); heightPanel.add( heightD );
heightPanel.add( heightE );

// result panel
resultText = new JTextField(7);
resultText.setEditable( false );
resultLabl = new JLabel("Ideal Weight");
resultPanel = new JPanel();
resultPanel.add( resultLabl );
resultPanel.add( resultText );

// content pane
getContentPane().setLayout( new BorderLayout() );
getContentPane().add( genderPanel, BorderLayout.WEST );
getContentPane().add( heightPanel, BorderLayout.EAST );
getContentPane().add( resultPanel, BorderLayout.SOUTH );

//set action command
genderM.setActionCommand("male");
genderF.setActionCommand("female");
heightA.setActionCommand("first");
heightB.setActionCommand("second");
heightC.setActionCommand("third");
heightD.setActionCommand("forth");
heightE.setActionCommand("fifth");

//add action listener
genderM.addActionListener(this);
genderF.addActionListener(this);
heightA.addActionListener(this);
heightB.addActionListener(this);
heightC.addActionListener(this);
heightD.addActionListener(this);
heightE.addActionListener(this);

resultText.setText("137.3");
}

public void actionPerformed(ActionEvent evt){


if(evt.getActionCommand().equals("male"))
sex=28;
else if(evt.getActionCommand().equals("female"))
sex=30;
else if(evt.getActionCommand().equals("first"))
h=62;
else if(evt.getActionCommand().equals("second"))
h=66;
else if(evt.getActionCommand().equals("third"))
h=70;
else if(evt.getActionCommand().equals("forth"))
h=74;
else h=78;


out=(h*h)/sex;
str=String.valueOf(out);
resultText.setText(str);
}



public static void main ( String[] args )
{
IdealWeight weightApp = new IdealWeight() ;
weightApp.setSize( 250, 225 );
weightApp.setVisible( true );
}
}
...全文
139 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kouyiSC 2011-12-19
  • 打赏
  • 举报
回复
genderM.setBackground(Color.white);
因为你的genderM对象没有初始化。。就去设置背景色。。当然不行。。。应该会报错null指针了。。
爱T小毛驴 2011-12-19
  • 打赏
  • 举报
回复
贴报错啊。哪有只贴代码的嘛,这让人怎么帮你排错啊。
oO临时工Oo 2011-12-18
  • 打赏
  • 举报
回复
看不出第九行呢
Lifehopeness 2011-12-18
  • 打赏
  • 举报
回复
哪个是第九行。。。。。。。。。。。
java_迷 2011-12-18
  • 打赏
  • 举报
回复
你把错误贴出来不更明了?

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧