![](/img/trans.png)
[英]Java Swing or AWT API - Setting breakpoint on global mouse click event
[英]Setting java awt mouse event without creating class?
我正在做awt工作,但是awt与以前学过的JavaFX相比并不容易,我正在努力为框架中的按钮设置事件,并且我有一个PlayAgain()方法,我的目的是在按钮被点击。 另外:请不要创建内部类来实现某些处理程序,而不要使用awt而不是swing / Fx。
这是我的代码:
public class CircleDraw extends Frame{
int[] diceResults;
public void paint(Graphics g) {
super.paint(g);
//in this part, I just using Graphics drawing some circles.
}
public void PlayAgain() {
//......do something
}
public static void main(String args[]) {
Frame frame = new CircleDraw();
Button button = new Button("again!");//this is the button, I want to set a Event, when clicking the button,my program will call PlayAgain() method
frame.add(button);
button.setBounds(5, 5, 5, 5);
frame.add(button);
frame.setLayout(new FlowLayout());
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.setSize(500, 250);
frame.setVisible(true);
}
}
我记得在JavaFX中确实可以这样写:
button.setMouseClicked(e -> {
//call the method} )
那么awt中是否有类似的功能可以做到这一点?
没有办法创建一个类。 Java的强类型要求这。 但是,您确实有一些选择可能会比其他选择更好。
它们都只是语法糖。 在幕后,总是有一个实现WindowAdapter的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.