简体   繁体   English

如何将JButton传递给Action Listener类

[英]How to pass JButton to the Action Listener class

I'm a beginner programmer and I am trying to learn how to pass certain objects created in the main class to other classes (in this case the action listener class). 我是一名初学者程序员,我正在尝试学习如何将在主类中创建的某些对象传递给其他类(在本例中为动作侦听器类)。

My question is - How can I pass the button to the action listener class? 我的问题是-如何将按钮传递给动作侦听器类? Here is my code snippet. 这是我的代码段。

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

Thank you very much for your help! 非常感谢您的帮助!

Move 移动

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

To your main method, for example 以您的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); 

}

First you should move the whole thing to a constructor 首先,您应该将整个内容移至构造函数

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

and then you can add the listener: 然后可以添加侦听器:

either 要么

button.addActionListener(new myListener());

or 要么

ActionListener listener = new myListener();
    button.addActionListener(listener);

in the constructor. 在构造函数中。

You add this line in your code after create the instance of button 创建按钮实例后,可以在代码中添加此行

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

and remove the myListener class and add this class myListener class 并删除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);  
     }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM