在动作侦听器中找不到符号错误

时间:2018-05-18 03:42:15

标签: java compiler-errors actionlistener

对于我们计算机数学课上的最终项目,我们必须制作一个游戏,我的团队正在做tic tac toe

import javax.swing.JFrame;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;



public class Driver extends JFrame 
{

  public static void main(String[] args) 
  {
Driver gst = new Driver();
JFrame frame = new JFrame("Tic Tac Toe");
 frame.setSize(1280, 720);
 frame.setLocation(200, 100);


gst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gst.pack();
gst.setVisible(true);
  }

  public Driver() 
  {
Container pane = getContentPane();
pane.setLayout(new GridLayout(3, 3));
pane.setPreferredSize(new Dimension(600, 600));

////////Buttons////////
//1,1
JButton oneOnebutton = new JButton();
pane.add(oneOnebutton);
oneOnebutton.addActionListener(new oneOneListener());
oneOnebutton.setBackground(Color.WHITE);

//1,2
JButton oneTwobutton = new JButton();
pane.add(oneTwobutton);
oneTwobutton.addActionListener(new oneTwoListener());
oneTwobutton.setBackground(Color.WHITE);

//1,3
JButton oneThreebutton = new JButton();
pane.add(oneThreebutton);
oneThreebutton.addActionListener(new oneThreeListener());
oneThreebutton.setBackground(Color.WHITE);

//2,1
   JButton twoOnebutton = new JButton();
   pane.add(twoOnebutton);
   twoOnebutton.addActionListener(new twoOneListener());
   twoOnebutton.setBackground(Color.WHITE);

//2,2
   JButton twoTwobutton = new JButton();
   pane.add(twoTwobutton);
    twoTwobutton.addActionListener(new twoTwoListener());
   twoTwobutton.setBackground(Color.WHITE); 

//2,3
   JButton twoThreebutton = new JButton();
   pane.add(twoThreebutton);
   twoThreebutton.addActionListener(new twoThreeListener());
   twoThreebutton.setBackground(Color.WHITE); 

//3,1
   JButton threeOnebutton = new JButton();
   pane.add(threeOnebutton);
   threeOnebutton.addActionListener(new threeOneListener());
   threeOnebutton.setBackground(Color.WHITE);

//3,2
   JButton threeTwobutton = new JButton();
   pane.add(threeTwobutton);
   threeTwobutton.addActionListener(new threeTwoListener());
   threeTwobutton.setBackground(Color.WHITE);

//3,3
   JButton threeThreebutton = new JButton();
   pane.add(threeThreebutton);
   threeThreebutton.addActionListener(new threeThreeListener());
   threeThreebutton.setBackground(Color.WHITE);


  }
        int turn = 1;


        private class oneOneListener implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {

                 oneOnebutton.setText("x");




           }
        }

         //******1,2******//
        private class oneTwoListener implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {





           }
        }



        //******1,3*********//
         private class oneThreeListener implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {





           }
        }


        //******2,1*********//
         private class twoOneListener implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {





           }
        }

         //******2,2*********//
         private class twoTwoListener implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {





           }
        }

        //******2,3*********//
         private class twoThreeListener implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {





           }
        }

        //******3,1*********//
         private class threeOneListener implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {





           }
        }


        //******3,2*********//
         private class threeTwoListener implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {





           }
        }

        //******3,3*********//
         private class threeThreeListener implements ActionListener
        {
           public void actionPerformed(ActionEvent e)
           {





           }
        }
   }    

如果你向下滚动到动作监听器,我有一个动作监听器设置为名为oneOne的按钮(我在3x3网格上有1,1)它给了我一个错误,指出指向oneOne并说无法找到符号,我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:2)

oneOnebutton被定义为Constructor Dirver中的局部变量,您无法从Constructor中访问它。

您可以成为Dirver

的班级成员
public class Driver extends JFrame {
    JButton oneOnebutton;
    ....
}

并在Constructor

中发起
public Driver() {
    ....
    oneOnebutton = new JButton();
    ....
}

这可以确保oneOnebutton中可以使用oneOneListener