0

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

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

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

非常感谢您的帮助!

4

3 回答 3

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

}
于 2013-10-23T01:47:09.753 回答
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);

在构造函数中。

于 2013-10-23T02:02:45.090 回答
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);  
     }
  }
于 2013-10-23T05:59:48.023 回答