简体   繁体   English

来自JTextField的按钮的ActionListener,无法解析JTextField

[英]ActionListener from button for JTextField, JTextField cannot be Resolved

I'm trying to get a head start on something I will be working on next semester. 我正在努力抢先准备下学期要学习的内容。 Its basically a template of a cell phone, consisting of a JTextField that displays the buttons pressed. 它基本上是手机的模板,由显示按下按钮的JTextField组成。 My problem is when making my actionlistener, the JTextField (named "numIn") is not being recognized, getting an error saying it cannot be resolved. 我的问题是,在使我的动作侦听器出现问题时,无法识别JTextField(名为“ numIn”),但出现错误,提示无法解决。 Here is the code for the way I have my JPanel of the phone set up: 这是我设置手机的JPanel的代码:

public class Template
{
    // the dial pad button strings
    private static final String[][] BUTTONSTRINGS =
    {
        {"1", "2", "3"},
        {"4", "5", "6"},
        {"7", "8", "9"},
        {"*", "0", "#"}
    };

    private static final Dimension JUNK_SIZE = new Dimension(200, 160);
    private JPanel mainPanel = new JPanel();

    public Template()
    {
        JTextField numIn = new JTextField("Enter Phone Number");
        JTextField numDisplay = new JTextField("PhoneNumber");
        JPanel otherJunkPanel = new JPanel();
        otherJunkPanel.add(numDisplay);
        otherJunkPanel.add(numIn);
        otherJunkPanel.add(new JButton("Send"));
        otherJunkPanel.setPreferredSize(JUNK_SIZE);

        JPanel dialPadPanel = new JPanel(new GridLayout(0, 3));

Here is the two action listeners made, one for the numbers, one for the non numbers: 这是两个操作侦听器,一个用于数字,一个用于非数字:

// action listener for the number buttons only 
NumberButtonListener numberBtnListener = new NumberButtonListener();
// listener for other buttons
NonNumberButtonListener nonNumberBtnListener = new NonNumberButtonListener();
for (int i = 0; i < BUTTONSTRINGS.length; i++)
{
    for (int j = 0; j < BUTTONSTRINGS[i].length; j++)
    {
        String btnString = BUTTONSTRINGS[i][j]; // get the button string from array
        JButton btn = new JButton(btnString); // use it to make button

        // if a number button, add the number button's listener
        if ("012345679".contains(btnString))
        {
            btn.addActionListener(numberBtnListener);
        }
        else
        {
            btn.addActionListener(nonNumberBtnListener);
        }

and here is where i get my error, when i tell the action listener to display the button at the JTextField named "numIn", numIn is not recognized: 这是我得到错误的地方,当我告诉动作侦听器在名为“ numIn”的JTextField上显示按钮时,numIn无法识别:

private class NumberButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {   
        String buttonPressedString = e.getActionCommand();
        numIn.setText("Number Button Pressed: " + buttonPressedString);
        // TODO finish      
    }
}

I am not very strong in coding, as its not my major, so I appreciate any help possible. 我不太擅长编码,因为它不是我的专业,所以我感谢任何可能的帮助。 I should have probably prefaced that much of this code has been frankenstiend from bits and pieces of labs I have done in the past. 我可能应该以我以前做过的一些零碎的实验来作为其中大部分代码的开头。 Thanks! 谢谢!

Your numIn is a local variable on the constructor. 您的numIn是构造函数上的局部变量。

You should declare it as a class variable if you want to use it on other methods. 如果要在其他方法上使用它,则应将其声明为类变量。 (or pass it by argument for methods) (或通过方法的参数传递它)

    JTextField numIn = new JTextField("Enter Phone Number");
    JTextField numDisplay = new JTextField("PhoneNumber");
    public Template()
    {
       .......

you need to put that in class level scope. 您需要将其放在类级别范围内。 currently it is in constructor. 当前它在构造函数中。 so it will not available for out side of the constructor 因此它不适用于构造函数

Try this 尝试这个

private class NumberButtonListener implements ActionListener
 {
 Template parent=null;
 NumberButtonListener(Template parent)
 {
    this.parent = parent;
 }

  public void actionPerformed(ActionEvent e)
  {

    String buttonPressedString = e.getActionCommand();
     parent.numIn.setText("Number Button Pressed: " + buttonPressedString);
    // TODO finish      
  }
 }

Then change this 然后改变这个

 NumberButtonListener numberBtnListener = new NumberButtonListener();

to

 NumberButtonListener numberBtnListener = new NumberButtonListener(this);

Add a new private field to your Template class 向您的Template类添加一个新的私有字段

  private JTextField numIn=null;

In the constructor change to this 在构造函数中更改为此

  numIn = new JTextField("Enter Phone Number");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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