简体   繁体   English

无法在Java中添加ActionListener

[英]Can't add ActionListener in Java

Can you please help me with this code ? 您能帮我提供这个代码吗? How can I make it that when the button is clicked, a second button appears? 如何确保单击该按钮时出现另一个按钮? I've already added the actionlisteners and created the second button, but I can't seem to be able to do it. 我已经添加了动作侦听器并创建了第二个按钮,但是我似乎无法做到这一点。 Thank you soooooooo much everyone!!! 非常感谢大家!!!

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Skeleton extends JFrame implements ActionListener {
    
    public static void main(String[] args) {

        JFrame frame = new JFrame("Skeleton");
        JPanel panel = new JPanel();
        JButton button = new JButton("This is a button.");
        JButton button2 = new JButton("Hello");

        frame.setSize(600,600);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        
        frame.setContentPane(panel);
        panel.setLayout(new FlowLayout());
        panel.add(button);        
    }

    public void actionPerformed(ActionEvent e) {

        panel.add(button2); //Whenever I compile with this line 
                            //of code inserted, it tells
                            //me cannot find Button 2 
    }    
}

Thanks again! 再次感谢!

Your code has many issues. 您的代码有很多问题。 First you can't create/build your UI in the main() method you need to create an instance of the class and call the method from there. 首先,您无法在main()方法中创建/构建UI,而您需要创建类的实例并从那里调用该方法。

Also for you to be able to refer to panel and button2 you need to make them class objects not local objects inside the UI method. 同样,为了能够引用panelbutton2还需要使它们成为UI方法内的类对象而不是本地对象。

And you need to at the very least add the ActionListener to the button 而且您至少需要将ActionListener添加到button

Finally you just need to call panel.revalidate() for the panel to show the added button: 最后,您只需要调用panel.revalidate()即可显示添加的按钮:

    public class Skeleton extends JFrame implements ActionListener {

    public static void main(String[] args) {

        new Skeleton().buildUI();
    }

    JPanel panel;
    JButton button2;

    public void buildUI() {

        JFrame frame = new JFrame("Skeleton");
        panel = new JPanel();
        JButton button = new JButton("This is a button.");
        button2 = new JButton("Hello");

        frame.setSize(600, 600);
        frame.setResizable(false);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        frame.setContentPane(panel);

        panel.setLayout(new FlowLayout());
        panel.add(button);

        button.addActionListener(this);

        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        panel.add(button2); 
        panel.revalidate();

    }
  }

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

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