如何将JButton传递给Action Listener类

时间:2013-10-23 01:44:02

标签: java swing jbutton actionlistener

我是初学程序员,我正在尝试学习如何将主类中创建的某些对象传递给其他类(在本例中是动作侦听器类)。

我的问题是 - 如何将按钮传递给动作侦听器类?这是我的代码段。

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);  
    }
}

非常感谢你的帮助!

3 个答案:

答案 0 :(得分: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); 

}

答案 1 :(得分: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);

在构造函数中。

答案 2 :(得分: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);  
     }
  }