package testDemo1; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class FrameTest { public static void main(String[] args) { Frame f = new Frame("My first window");// This window is not displayed by default f.setSize(400, 600);// Set the window size f.setLocation(500, 50);// Set the window display location f.setVisible(true);// Display the window Button but = new Button("my button");// If the button is set to Chinese, garbled characters may appear f.setLayout(new FlowLayout());// The default is a border layout (the button is huge), so you need to set the layout in the main panel f.add(but);//Add button to panel but.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {//There is only such an overridable method System.out.println("Button triggered" + e); } }); } }
The console shows the result: the button is triggered java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=my button,when=1518246610292,modifiers=] on button0