Java初始化问题

发布于 2024-11-30 16:17:42 字数 3294 浏览 2 评论 0原文

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 可能尚未初始化。请帮忙。

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 );
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一直在等你来 2024-12-07 16:17:42

问题在于这些行:

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

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

看来您可能想引用您之前声明的 roundrButton 。你有没有考虑过把它作为你班级的一个领域?

The problem is these lines here:

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

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.

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?

因为看清所以看轻 2024-12-07 16:17:42

错误发生在这里:

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

您声明了一个名为 roundrButton 的变量,但没有为其分配任何对象。

然后你调用了一个方法,好吧,谁知道呢?没有可调用该方法的对象,因为变量 roundrButton 从未收到值。

The error happens here:

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

You declared a variable called roundrButton but did not assign any object to it.

Then you called a method on, well, who knows? There is no object on which to call the method, because the variable roundrButton never received a value.

叹梦 2024-12-07 16:17:42

我猜你的问题出在 calcButtonHandler2 类的 actionPerformed 方法中:

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

roundrButton 确实没有初始化,它会在运行时导致 NullPointerException 。

I guess your problem is in method actionPerformed of class calcButtonHandler2 :

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

roundrButton is indeed not initialized and it will lead to a NullPointerException at runtime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文