2016-06-08 84 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答案,如果要涉及到那些按鈕。

相關問題