2016-06-08 82 views
3

如何在代码中调用一个按钮来执行操作?我知道要使用动作侦听器,但如果按钮没有如下所示的变量名称呢?如何调用按钮在我的代码中执行操作?

我是Java新手,请耐心等待。

public class Game 
{ 
    public static void main (String[] args) 
    { 
     JFrame frame = new JFrame ("Game"); 

     frame.setLayout(new GridLayout(7, 6)); 

     for(int i = 0; i < 42; i++) 
     { 
      frame.add(new JButton()); // This is the part I'm talking about!!! 
     } 

     frame.getContentPane().add(new gameBoard()); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

谢谢。

+0

通常按钮的动作比按钮重要得多。我经常匿名创建按钮,不关心给他们赋予变量名称或引用它们,而是引用传入按钮的Action(AbstractAction派生对象)。 –

回答

3

如果你想一个ActionListener添加到每个将JButton的对象,你将不得不做这样的事情:

private void createButtons(){ 
     for(int i=0;i<42;i++){ 
       JButton button = new JButton("Name"); 
       button.addActionListener(YourActionListenerHere); 
       frame.add(button); 
     } 
    } 

在一段代码首先我做的是为你创建按钮,但我把它作为一个变量的形式,通过我的代码在这里命名为'button':JButton button = new JButton("Name");,然后我添加一个ActionListener到按钮(我假设你已经有一个实现ActionListener的类),最后我将它添加到框架与frame.add(button);

3

在你的问题是复杂一点的希望大点是,你是匿名添加按钮帧...

我建议你去尝试这样一个事实:

  1. 创建Jbutton将
  2. 列表通过索引获取按钮,添加监听
  3. 将它添加到框架

例子:

public static void main(String[] args) { 
int _k = 3; 
JFrame frame = new JFrame("Game"); 
frame.setLayout(new GridLayout(7, 6)); 
List<JButton> jbl = new ArrayList<>(); 
    for (int i = 0; i < _k; i++) { 
     jbl.add(new JButton(":)" + i)); 
     jbl.get(i).addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println(((JButton) e.getSource()).getText()); 
      } 
     }); 
     frame.add(jbl.get(i)); // This is the part I'm talking 
    } 
frame.getContentPane().add(new gameBoard()); 
frame.pack(); 
frame.setVisible(true); 
} 
+0

我想初始化一个按钮对象,然后对它进行操作,然后最后添加到列表看起来比从列表中退出以执行这些操作更清洁一些,不是吗? –

2
package test; 

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 

public class Game 
{ 
    public static void main (String[] args) 
    { 
     JFrame frame = new JFrame ("Game"); 

     frame.getContentPane().setLayout(new GridLayout(7, 6)); 


     for(int i = 0; i < 42; i++) 
     { 
      String buttonName = "Button "+ i; 

      //create the button 
      JButton button = new JButton(buttonName); 

      //add an action listener to it so it can do something 
      button.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 

        //... here goes what ever the button needs to do 
        System.out.println(buttonName+" clicked"); 

       } 
      }); 

      frame.getContentPane().add(button); //add the button 
     } 

     //you need to change the layout if you want to add more 
     //frame.getContentPane().add(new gameBoard()); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

这是它的外观: enter image description here

实际上你可能需要refrences所有butons存储为@ΦXoce웃Пepeúpa答案,如果要涉及到那些按钮。

相关问题