So fügen Sie einer Schaltfläche einen Befehl hinzu

// import the just the libraries that you gonna need dont be like me
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class App {
    public static void main(String[] args) {
        JFrame f = new JFrame("Test");	//make a window
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600,300);
        
        JButton bhelloworld = new JButton("Hello World!");	//make the button

        ActionListener l = new ActionListener() {	//make the listener (l is its name)
            public void actionPerformed(ActionEvent event) {	//make the command here
                if (event.getActionCommand() == "func1") {	//to specify what the button gonna do put this if (the "event.getActionCommand() == "func1"" is checking the setActionCommand() command)
                    System.out.println("Button pressed");
                }
            }
        };	//these ; are needed :v

        bhelloworld.setSize(new Dimension(40,40));
        f.add(bhelloworld);	//add your button
        bhelloworld.setActionCommand("func1");	//optional
        bhelloworld.addActionListener(l);	//determine which action listener you gonna use

        f.setVisible(true);	//show the screen
    }
}
T H E