1

我在.getText()尝试从JTextField. 我一直在寻找解决方案,但找不到。有没有人可以看到我做错了什么?

我想做的是一个弹跳球,可以改变弹性、重力和初始速度等参数。

尝试编译时出现以下消息:

BBPanel.java 中的错误(第 88 行)重力输入无法解析

谢谢!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/////////////////////////////////////////////////////////////////// BBPanel
class BBPanel extends JPanel {
BallInBox n_bb;   // The bouncing ball panel

//========================================================== constructor
/** Creates a panel with the controls and bouncing ball display. */
BBPanel() {
//... Create components
    n_bb = new BallInBox();        
    JButton startButton = new JButton("Start");        
    JButton stopButton  = new JButton("Stop");
    JButton resetButton = new JButton("Reset");
    JButton gveButton = new JButton("GetData");

    JLabel gravityLbl = new JLabel (" set gravity " );
    JLabel velocity_yLbl = new JLabel (" set initial y-dir speed " );
    JLabel elasticityLbl = new JLabel (" set elasticity [0-100%]" );

    JTextField gravity_input = new JTextField(20);
    JTextField velocity_y_input = new JTextField(20);
    JTextField elasticity_input = new JTextField(20);

    //... Add Listeners
     startButton.addActionListener(new StartAction() );
     stopButton.addActionListener(new StopAction()  );
     resetButton.addActionListener(new ResetAction() );
     gveButton.addActionListener(new DataAction()  );

    //... Layout inner panel with three buttons horizontally
     JPanel buttonPanel = new JPanel();
     buttonPanel.setLayout(new FlowLayout());
     buttonPanel.add(startButton);
     buttonPanel.add(stopButton);
     buttonPanel.add(resetButton);

//
JPanel variablePanel = new JPanel();
variablePanel.setLayout(new GridLayout(0,2));

variablePanel.add(gravityLbl);
variablePanel.add(gravity_input);
variablePanel.add(velocity_yLbl);
variablePanel.add(velocity_y_input);
variablePanel.add(elasticityLbl);
variablePanel.add(elasticity_input);
variablePanel.add(gveButton);

    //... Layout outer panel with button panel above bouncing ball
    this.setLayout(new BorderLayout());
    this.add(buttonPanel  , BorderLayout.NORTH);
    this.add(n_bb         , BorderLayout.CENTER);
    this.add(variablePanel, BorderLayout.EAST);

}//end constructor


////////////////////////////////////// inner listener class StartAction
class StartAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
            n_bb.setAnimation(true);
    }
}


//////////////////////////////////////// inner listener class StopAction
class StopAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
            n_bb.setAnimation(false);
    }
}
///////////////////////////////////////// inner listener class ResetAction
class ResetAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    n_bb.resetAnimation(true);
    }
  }
////////////////////////////////////////// inner listener class GravityAction
 class DataAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String gravityIn = gravity_input.getText();
        //System.out.println(gravityIn);
        n_bb.setData(true);
    }
}
}//endclass BBPanel
4

5 回答 5

3

该变量gravity_input仅在 的构造函数范围内本地定义BBPanel。如果你想让它对你的类可见,你需要让它成为一个类成员变量DataAction

public class BBPanel extends JPanel {
   private JTextField gravity_input; // to be initialized...
   ...

旁注:Java 使用驼峰式大小写,这会使gravity_input gravityInput.

于 2013-02-25T12:14:19.003 回答
1

您的问题是范围之一。Gravity_input 在构造函数或方法中声明。要在这些块之外可见,它应该在类中声明,类似于声明 n_bb 字段的方式。

于 2013-02-25T12:14:25.720 回答
1

您的变量gravity_input应声明为实例变量(在构造函数之外),以便以后访问。

您定义它的方式,名称(不是文本字段)在构造函数代码终止(到达右大括号)后立即gravity_input 消失}

于 2013-02-25T12:15:34.923 回答
0

将以下参数传递给 的构造函数DataAction

class DataAction implements ActionListener {
    final JTextField textField;
    final BallInBox bb;
    public DataAction(final JTextField textField, final BallInBox bb) {
        this.textField = textField;
        this.bb = bb;
    }
    public void actionPerformed(ActionEvent e) {
        String gravityIn = textField.getText();
        //System.out.println(gravityIn);
        bb.setData(true);
    }
}

并像这样使用它:

gveButton.addActionListener(new DataAction(gravity_input, n_bb));

然后以类似的方式重构您的其他操作。

于 2013-02-25T12:15:44.273 回答
0

问题是,您的班级DataAction不知道您的gravity_input文本字段。

文本字段gravity_input仅对类可见BBPanel。要使其对类可见DataAction,您必须将文本字段传递给该类,如下所示:

class DataAction implements ActionListener {

    private JTextField gravity_input;

    public DataAction(JTextField txtField) {
        gravity_input = txtField;
    }

    public void actionPerformed(ActionEvent e) {
        String gravityIn = gravity_input.getText();
        //System.out.println(gravityIn);
        n_bb.setData(true);
    }
}

并将您的 BBPanel 构造函数更改为:

BBPanel() {
    //... omitted
    gveButton.addActionListener(new DataAction(gravity_input));
    //... omitted
}
于 2013-02-25T12:15:49.207 回答