简体   繁体   中英

Problems with getText() from JTextField

I'm having troubles with the .getText() command trying to retrieve data from a JTextField . I've been looking for the solution but couldn't find it. Is there anybody who can see what I'm doing wrong?

What I'm trying is to make is a bouncing ball which parameters such as elasticity, gravity and initial speed can be changed.

When trying to compile the following message appears:

ERROR in BBPanel.java (at line 88) String gravityIn = gravity_input.getText(); gravity_input cannot be resolved

Thanks!

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

The variable gravity_input is only defined locally in the scope of the constructor of BBPanel . You need to make it a class member variable if you wish to make it visible to your DataAction class.

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

Side note: Java uses camel-case which would make gravity_input gravityInput .

Your problem is one of scope. The gravity_input is declared inside of a constructor or method. To be visible outside of these blocks, it should be declared in the class similar to how you declare the n_bb field.

Your variable gravity_input should be declared as an instance variable (outside the constructor), for it to be later accessed.

The way you're defining it, the name (not the text field) gravity_input dies as soon as the constructor code terminates (reaches closing brace } ).

pass parameters as follows into constructor of 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);
    }
}

and use it like this:

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

Then refactor your other actions in a similar way.

The problem is, that your class DataAction doesn't know about your gravity_input textfield.

The textfield gravity_input is only visible to the class BBPanel . To make it visible to the class DataAction , you have to pass the textfield to that class like this:

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

and change your BBPanel constructor to this:

BBPanel() {
    //... omitted
    gveButton.addActionListener(new DataAction(gravity_input));
    //... omitted
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM