java 模拟用户点击事件
如何调用button的actionlistener,实现模拟点击按键?即不点击button,实现button的点击事件...
如何调用button的actionlistener,实现模拟点击按键?即不点击button,实现button的点击事件
展开
1个回答
展开全部
调用按钮的 doClick() 方法,下例演示了 button2 模拟点击 button1 按钮:
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class App extends JFrame {
public App() 迅培{
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
JButton button1 = new JButton("Button1");
button1.addActionListener(e ->亩孙唯 JOptionPane.showMessageDialog(App.this, "我是Button1"));
this.add(button1);
JButton button2 = new JButton("Button2");
button2.addActionListener(e -> button1.doClick());
this.add(button2);
}
public static void 凯握main(String[] args) {
new App().setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询