Java初始化问题 [英] Java initialization question

查看:94
本文介绍了Java初始化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPanel p2 = new JPanel(new GridLayout(6,1));
ButtonGroup tubtype = new ButtonGroup();
JRadioButton roundrButton = new JRadioButton("Round", true);
tubtype.add(roundrButton);
JRadioButton ovalrButton = new JRadioButton("Oval", false);
tubtype.add(ovalrButton);
calcButton = new JButton("Calculate Volume");
exitButton = new JButton("Exit");
hlength = new JTextField(5);
hwidth = new JTextField(5);
hdepth = new JTextField(5);
hvolume = new JTextField(5);
lengthLabel = new JLabel("Enter the tub's length (ft):");
widthLabel = new JLabel("Enter the tub's width (ft):");
depthLabel = new JLabel("Enter the tub's depth (ft):");
volumeLabel = new JLabel("The tub's volume (ft^3):");
p2.add(roundrButton);
p2.add(ovalrButton);
p2.add(lengthLabel);
p2.add(hlength);
p2.add(widthLabel);
p2.add(hwidth);
p2.add(depthLabel);
p2.add(hdepth);
p2.add(volumeLabel);
p2.add(hvolume);
p2.add(calcButton);
p2.add(exitButton);
tab.addTab( "Hot Tubs", null, p2, " Panel #1" );

calcButtonHandler2 ihandler =new calcButtonHandler2();
calcButton.addActionListener(ihandler);
exitButtonHandler ghandler =new exitButtonHandler();
exitButton.addActionListener(ghandler);
FocusHandler hhandler =new FocusHandler();
hlength.addFocusListener(hhandler);
hwidth.addFocusListener(hhandler);
hdepth.addFocusListener(hhandler);
hvolume.addFocusListener(hhandler);

// add JTabbedPane to container
getContentPane().add( tab );
setSize( 550, 500 );
setVisible( true );
} 
public class calcButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {
DecimalFormat num =new DecimalFormat(",###.##");
double sLength, sWidth, sdepth, Total;

sLength = Double.valueOf(plength.getText());

sWidth =Double.valueOf(pwidth.getText());

sdepth =Double.valueOf(pdepth.getText());

if(e.getSource() == pcalcButton) {
Total = sLength * sWidth * sdepth;
pvolume.setText(num.format(Total));
try{
String value=pvolume.getText();
File file = new File("output.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Length= "+sLength+", Width= "+sWidth+", Depth= "+sdepth+" so the volume of Swimming  Pool is "+value);
out.newLine();
out.close();
}
catch(Exception ex){}
}
}
}

public class calcButtonHandler2 implements ActionListener {

public void actionPerformed(ActionEvent g) {
DecimalFormat num =new DecimalFormat(",###.##");
double cLength, cWidth, cdepth, Total;

cLength = Double.valueOf(hlength.getText());

cWidth = Double.valueOf(hwidth.getText());

cdepth = Double.valueOf(hdepth.getText());

try
{
AbstractButton roundrButton;
if(roundrButton.isSelected())
{

Total = Math.PI * Math.pow(cLength / 2.0, 2) * cdepth;
}
else 
{
Total = Math.PI * Math.pow(cLength * cWidth, 2) * cdepth;
}
hvolume.setText(""+num.format(Total));
}

catch(Exception ex){}
}
}
}



class exitButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent g){
        System.exit(0);
    }
}
class FocusHandler implements FocusListener {
    public void focusGained(FocusEvent e) {
    }
    public void focusLost(FocusEvent e) {
    }


public static void main( String args[] )
{
    Final tabs = new Final();
    tabs.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

我收到一条错误消息,提示我的roundrButton可能尚未初始化.请帮忙.

I am getting an error that says my roundrButton may not have been initialized. Please help.

推荐答案

问题出在这里:

try
{
    AbstractButton roundrButton;
    if(roundrButton.isSelected())
    {

您要声明一个名为roundrButton的变量,然后尝试在其上调用.isSelected().但是,您永远不会为roundButton分配任何内容,因此它将为null.

You're declaring a variable called roundrButton, and then attempting to call .isSelected() on it. You never assign anything to roundButton, though, so it is going to be null.

似乎您可能想引用您先前声明的roundrButton.您是否考虑过将它作为班上的一个领域?

It seems like you might want to be referring to the roundrButton that you declared earlier. Have you considered making it a field of your class?

这篇关于Java初始化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆