2013-10-23 73 views
0

我是一名初學者程序員,我正試圖學習如何將在主類中創建的某些對象傳遞給其他類(在本例中爲動作偵聽器類)。如何將JButton傳遞給Action Listener類

我的問題是 - 如何將按鈕傳遞給動作監聽器類?這是我的代碼片段。

public class MaxMinProgram 
{ 

    public static void main(String[] args) 
    { 
     JFrame frame = new JFrame("Max Min Program"); 
     GridLayout myLayout = new GridLayout(1,11); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     int size = 11; 
     JTextField[] fields = new JTextField[size];   
     JPanel panel = new JPanel(); 
     frame.setPreferredSize(new Dimension(500,110)); 
     frame.getContentPane().add(panel); 
     int k = 0; 
     for(k=0;k<fields.length;k++) 
     { 
      fields[k] = new JTextField("", 3); 
      panel.add(fields[k]); 
     } 
     JButton button = new JButton("Randomize"); 
     JButton button2 = new JButton("Max Min"); 
     panel.add(button); 
     panel.add(button2); 
     frame.pack(); 
     frame.setVisible(true); 

    } 
} 

public class myListener implements ActionListener 
{ 
    @Override 
    public void actionPerformed(ActionEvent evt) 
    { 
     ActionListener clickListener = new myListener(); 
     button.addActionListener(clickListener); //this is the line 
     int [ ] numbers = new int [10]; 
     JTextField [] textFields; 
     Random randomize = new Random(); 
     int x = randomize.nextInt(100); 
    } 
} 

非常感謝您的幫助!

回答

2

移動

ActionListener clickListener = new myListener(); 
    button.addActionListener(clickListener); //this is the line 

要將main方法,例如

public static void main(String[] args) 
{ 
    JFrame frame = new JFrame("Max Min Program"); 
    GridLayout myLayout = new GridLayout(1,11); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    int size = 11; 
    JTextField[] fields = new JTextField[size];   
    JPanel panel = new JPanel(); 
    frame.setPreferredSize(new Dimension(500,110)); 
    frame.getContentPane().add(panel); 
    int k = 0; 
    for(k=0;k<fields.length;k++) 
    { 
     fields[k] = new JTextField("", 3); 
     panel.add(fields[k]); 
    } 
    JButton button = new JButton("Randomize"); 
    JButton button2 = new JButton("Max Min"); 

    ActionListener clickListener = new myListener(); 
    button.addActionListener(clickListener); //this is the line 

    panel.add(button); 
    panel.add(button2); 
    frame.pack(); 
    frame.setVisible(true); 

} 
+0

我做到這一點,它給我的錯誤一個「非靜態變量,這不能從靜態上下文中引用」 – Alec

+0

使你的'myListener'類'static' – MadProgrammer

1

首先,你應該對整個事情移到構造

public static void main(String[] args) 
{ 
    new MaxMinProgram(); 
} 

public MaxMinProgram(){ 
    JFrame frame = new JFrame("Max Min Program"); 
    GridLayout myLayout = new GridLayout(1,11); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    int size = 11; 
    JTextField[] fields = new JTextField[size];   
    JPanel panel = new JPanel(); 
    frame.setPreferredSize(new Dimension(500,110)); 
    frame.getContentPane().add(panel); 
    int k = 0; 
    for(k=0;k<fields.length;k++) 
    { 
     fields[k] = new JTextField("", 3); 
     panel.add(fields[k]); 
    } 
    JButton button = new JButton("Randomize"); 
    JButton button2 = new JButton("Max Min"); 
    panel.add(button); 
    panel.add(button2); 
    frame.pack(); 
    frame.setVisible(true); 
} 

,然後你可以添加監聽器:

要麼

button.addActionListener(new myListener()); 

ActionListener listener = new myListener(); 
    button.addActionListener(listener); 
在構造

0

你在你的代碼中加入這一行創建後按鈕

button.addActionListener(new myListener()); 
button1.addActionListener(new myListener()); 

的實例,並刪除myListener的類並添加該類myListener的類

public class myListener implements ActionListener 
    { 
    @Override 
    public void actionPerformed(ActionEvent evt) 
    { 
     int [ ] numbers = new int [10]; 
     JTextField [] textFields; 
     Random randomize = new Random(); 
     int x = randomize.nextInt(100); 
    } 
    } 
相關問題