将变量从方法传递给actionlistener

时间:2014-06-01 06:36:11

标签: java swing jbutton actionlistener

我是Java的新手,我尝试编写一个简单的代码。我遇到了将button00传递给ActionListener的问题,所以我可以编程按钮来做smth。我知道我需要以某种方式访问​​createandshowgui()中的button00,但我不知道如何。任何帮助将不胜感激。谢谢

package gui;

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



public class gui_tryout extends JFrame implements ActionListener {

public int count;
public void menuoption() {

    JMenuBar menu = new JMenuBar ();
    JMenu menufile = new JMenu ("File");
    menufile.setMnemonic(KeyEvent.VK_F); 
    // ^ hot key. if you press ALT+F => File will pop up
    menu.add(menufile);
    JMenu menustats = new JMenu ("Stats");
    menu.add(menustats);
    menustats. setMnemonic (KeyEvent.VK_D);
    setJMenuBar(menu);
    JMenuItem menu_file_new = new JMenuItem ("New");
    menu_file_new.setMnemonic(KeyEvent.VK_N);
    menufile.add(menu_file_new);
    JMenuItem score_display = new JMenuItem("Score");
    menustats.add(score_display);
}



private static void createandshowgui(){

     gui_tryout newframe = new gui_tryout ();
     newframe.setSize(400, 300);
     newframe.setLayout (new GridLayout (3,3));
     newframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     newframe.setVisible(true);

    //Adding Grid to the Window
     JButton button00 = new JButton (" "); //0 0 
     newframe.add(button00);
     JButton button01 = new JButton (" "); //0 1 
     newframe.add(button01);
     JButton button02 = new JButton (" "); //0 2    
     newframe.add(button02);
     JButton button10 = new JButton (" "); //1 0
     newframe.add(button10);
     JButton button11 = new JButton (" "); //1 1
     newframe.add(button11);
     JButton button12 = new JButton (" "); //1 2
     newframe.add(button12);
     JButton button20 = new JButton (" "); //2 0
     newframe.add(button20);
     JButton button21 = new JButton (" "); //2 1
     newframe.add(button21);
     JButton button22 = new JButton (" "); //2 2
     newframe.add(button22);

     newframe.menuoption(); //adding menu part to the code
} 

 public void actionPerformed (ActionEvent e){
    count ++;

    //button00.addActionListener(this);

    //whose turn is it?
    if (count ==1 || count == 3 || count == 5 || count == 7 || count == 9){ 
        //display X
    }
        else if (count == 2 || count == 4 || count == 6 || count == 8)
    {
        //display O
    }

 }

 public static void main(String[] args) {

     SwingUtilities.invokeLater(new Runnable (){
         public void run() {
             createandshowgui();
          }
 });
 }
 }

2 个答案:

答案 0 :(得分:1)

将按钮设为instance variable。然后,您可以在其他类方法中访问它。目前它是一个局部变量,它的范围仅限于该方法。

public class gui_tryout extends JFrame implements ActionListener{
   private Button button00 = new JButton (" ");
   // rest of the code
}

答案 1 :(得分:1)

您没有为任何按钮设置任何动作侦听器,请使用:

 JButton button00 = new JButton (" "); //0 0 
 newframe.add(button00);
 button00.addActionListener(newframe);//Your JFrame implements ActionListener  

此外,你如何做到这一点并不是正确的做法。您需要创建组件,并且应该在类的构造函数中初始化它们,这些构造函数扩展了JFrame。