来自JTextField的getText()问题

时间:2013-02-25 12:10:44

标签: java swing jframe scope jtextfield

我遇到了.getText()命令试图从JTextField检索数据时遇到的麻烦。我一直在寻找解决方案,但找不到它。有谁能看出我做错了什么?

我正在尝试的是制作一个弹跳球,可以改变弹性,重力和初始速度等参数。

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

  

BBPanel.java中的错误(第88行)String gravityIn = gravity_input.getText();                              gravity_input无法解析

谢谢!

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

5 个答案:

答案 0 :(得分:3)

变量gravity_input仅在BBPanel的构造函数范围内本地定义。如果您希望将其设为DataAction类,则需要将其设为类成员变量。

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

旁注: Java使用camel-case来生成gravity_input gravityInput

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

您定义它的方式,一旦构造函数代码终止,名称(不是文本字段)gravity_input 就会(达到右括号) })。

答案 3 :(得分: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));

然后以类似的方式重构你的其他行为。

答案 4 :(得分: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
}