The editor below will bring you an article on how to add a button in Java and add a method to respond to the event (recommended). The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look
Let’s learn about Java containers, panels, etc. from Baidu
private Button LogInbtn = new Button("登陆"); final static JFrame buyerpagemain = new JFrame(); final Container contentPane = buyerpagemain.getContentPane(); contentPane.add(LogInbtn);// 登陆按钮 LogInbtn.setBounds(690, 12, 40, 30); LogInbtn.setBackground(Color.green); LogInbtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new LoginMain("登陆"); //buyerpagemain.dispose(); } });
The implementation of the LoginMain class:
public class LoginMain extends JFrame { public static void main(String[] args) { new LoginMain("登陆"); } }
Just implement the specific functions you want to achieve by yourself!
The above is the detailed content of Methods to add buttons and respond to events in java. For more information, please follow other related articles on the PHP Chinese website!