有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java创建类的独立对象

我想创建一个类中多个相互独立的对象

我在for循环中多次调用类构造函数,但如何识别这些对象呢

我尝试使用一个静态变量来获取for循环索引的值,但是这个变量假定for循环的最后一个索引的值

以下是代码:

美因圭。爪哇

public class Maingui extends JFrame {

    public static JFrame frame;
    public static JButton runButton;

    public Maingui() throws IOException {

        frame = new JFrame("maingui");
        setSize(1024,700);
        setTitle("maingui");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        setLayout(new BorderLayout());

        runButton = new JButton("Run");
        runButton.addActionListener(new RunBtnListener());
        add(runButton);

    }
}

RunBtnListener。爪哇

public class RunBtnListener implements ActionListener {

    ArrayList<SecondGui> menus = new ArrayList<SecondGui>();

    public void actionPerformed(ActionEvent e) {

        for(int i = 0; i < 2; i++) {

            menus.add(new SecondGui(i));

        }

    }

}

第二个GUI。爪哇

public class SecondGui extends JFrame {

    public static int c;
    JFrame frame;
    JButton button;

    public SecondGui(int i) {

        this.c = i;

        frame = new JFrame("Test");
        setSize(1024,700);
        setTitle("Menu");
        setLocationRelativeTo(null);
        setVisible(true);
        this.setLocation(50*i, 50*i);

        // Set layout manager
        setLayout(new BorderLayout());

        button = new JButton("B");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("C: " + c);

            }

        });

        add(button);

    }

}

当我在SecondGuiGUI中单击按钮button时,输出总是1。 但我希望输出将是01基于我在哪个GUI中选择按钮


共 (1) 个答案

  1. # 1 楼答案

    I want to create more than one object of a class that are independent one from the other.

    当前代码在这个循环中创建了两个独立的对象

        for(int i = 0; i < 2; i++) {
    
            menus.add(new SecondGui(i));
        }
    

    但是,当您将其设置为静态时,它不会绑定到您创建的任何实例

    您需要的是一个实例变量来保留SecondGui实例的ID。您可以创建一个新的构造函数并传递循环的索引

    public class SecondGui extends JFrame {
    
        private int c = 0;
        private JFrame frame;
        private JButton button;
    
        public SecondGui(int i) {
    
            this.c = i;
            initialize();
        }
    
        void initialize() {
    
            frame = new JFrame("Test");
            setSize(1024,700);
            setTitle("Menu");
            setLocationRelativeTo(null);
            setVisible(true);
            this.setLocation(50*i, 50*i);
    
            // Set layout manager
            setLayout(new BorderLayout());
    
            button = new JButton("B");
            button.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("C: " + c);
    
                }
    
            });
    
            add(button);
         }
    
    }