调用 ActionEvent
Calling an ActionEvent
我正在学习和创建自定义 JButton/Component。我拥有我需要的大部分东西,除了我不知道如何在我的 ActionListners 上调用 actionPerformed。
代码:
package myProjects;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class LukeButton extends JComponent{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setTitle("Luke");
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LukeButton lb = new LukeButton();
lb.addActionListener(e->{
System.out.println("Success");
});
frame.add(lb);
frame.setVisible(true);
}
private final ArrayList<ActionListener> listeners = new ArrayList<ActionListener>();
public LukeButton(){
}
public void addActionListener(ActionListener e){
listeners.add(e);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Shape rec = new Rectangle2D.Float(10, 10, 60, 80);
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(5));
g2.draw(rec);
g2.setColor(Color.BLUE);
g2.fill(rec);
}
}
有谁知道单击按钮后如何调用 "listeners" ArrayList?感谢您抽出时间。
您需要遍历 ActionListener
列表并调用它们的 actionPerformed
方法,例如...
protected void fireActionPerformed() {
if (!listeners.isEmpty()) {
ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "LukeButton");
for (ActionListener listener : listeners) {
listener.actionPerformed(evt);
}
}
}
现在,您需要定义可能触发 ActionEvent
发生的操作,鼠标单击、按键等,并在它们发生时调用 fireActionPerformed
方法
查看How to Write a Mouse Listener and How to Use Key Bindings了解更多详情
我正在学习和创建自定义 JButton/Component。我拥有我需要的大部分东西,除了我不知道如何在我的 ActionListners 上调用 actionPerformed。
代码:
package myProjects;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class LukeButton extends JComponent{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setTitle("Luke");
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LukeButton lb = new LukeButton();
lb.addActionListener(e->{
System.out.println("Success");
});
frame.add(lb);
frame.setVisible(true);
}
private final ArrayList<ActionListener> listeners = new ArrayList<ActionListener>();
public LukeButton(){
}
public void addActionListener(ActionListener e){
listeners.add(e);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Shape rec = new Rectangle2D.Float(10, 10, 60, 80);
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(5));
g2.draw(rec);
g2.setColor(Color.BLUE);
g2.fill(rec);
}
}
有谁知道单击按钮后如何调用 "listeners" ArrayList?感谢您抽出时间。
您需要遍历 ActionListener
列表并调用它们的 actionPerformed
方法,例如...
protected void fireActionPerformed() {
if (!listeners.isEmpty()) {
ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "LukeButton");
for (ActionListener listener : listeners) {
listener.actionPerformed(evt);
}
}
}
现在,您需要定义可能触发 ActionEvent
发生的操作,鼠标单击、按键等,并在它们发生时调用 fireActionPerformed
方法
查看How to Write a Mouse Listener and How to Use Key Bindings了解更多详情