gpt4 book ai didi

java - 监听器调用另一个监听器

转载 作者:行者123 更新时间:2023-12-04 06:10:48 25 4
gpt4 key购买 nike

在使用 Swing 创建菜单的类中,我添加了 4 个项目,每个项目负责绘制一个不同颜色的圆圈。我已经为该菜单的每个项目分配了一个 ActionListener 来指定所需圆圈的颜色。我需要在面板中的任意位置单击鼠标,然后在那里创建圆圈。为此,我创建了另一个类 ScrollPane,它实现了 mouseListener 并负责绘制圆圈。不过,我不知道如何将 mouseListener 触发到将完成这项工作的第二个类。这应该只在菜单的监听器被调用后完成。我显然需要一个对类 ScrollPane 的引用,然后在那个 mouseListener 上分配。

但是,我不知道应该对 addMouseListener 方法使用什么样的参数。或者这是在 ScrollPane 类中声明的?如果所有任务都在一个实现了两个监听器的类中执行,我认为这很简单,但是如果我想将它们分开怎么办? actionPerformed 还应该做什么?
这是相关的代码:

public class CreateMenu implements ActionListener {
private ScrollPane scrollPane;

public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
if(source.getText() == "Green Circle")
this.scrollPane.setColor(Color.green);
else if(source.getText() == "Blue Circle")
this.scrollPane.setColor(Color.blue);
else if(source.getText() == "Red Circle")
this.scrollPane.setColor(Color.red);
else if(source.getText() == "Yellow Circle")
this.scrollPane.setColor(Color.yellow);
else
return;
}
}

.
public class ScrollPane extends JPanel implements MouseListener {
private Dimension area;
private Vector<Circle> circles;
private Color color;
private JPanel drawingPane;

public ScrollPane() {
super(new BorderLayout());

area = new Dimension(0,0);
circles = new Vector<Circle>();

//Set up the drawing area.
drawingPane = new DrawingPane();
drawingPane.setBackground(Color.white);
drawingPane.addMouseListener(this);

//Put the drawing area in a scroll pane.
JScrollPane scroller = new JScrollPane(drawingPane);
scroller.setPreferredSize(new Dimension(200,200));

add(scroller, BorderLayout.CENTER);
}

public class DrawingPane extends JPanel {
protected void paintComponent(Graphics g, Color color ) {
super.paintComponent(g);

Rectangle rect;
for (int i = 0; i < circles.size(); i++) {
rect = circles.elementAt(i).getRect();
g.setColor(circles.elementAt(i).getTheColor());
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
}

public void mouseReleased(MouseEvent e) {
final int W = 100;
final int H = 100;
boolean changed = false;

if(SwingUtilities.isLeftMouseButton(e)) {
int x = e.getX() - W/2;
int y = e.getY() - H/2;
if (x < 0) x = 0;
if (y < 0) y = 0;
Rectangle rect = new Rectangle(x, y, W, H);
Circle newCircle = new Circle(rect, this.color);
circles.addElement(newCircle);
drawingPane.scrollRectToVisible(rect);

int this_width = (x + W + 2);
if (this_width > area.width) {
area.width = this_width; changed=true;
}

int this_height = (y + H + 2);
if (this_height > area.height) {
area.height = this_height; changed=true;
}
}
if (changed) {
//Update client's preferred size because
//the area taken up by the graphics has
//gotten larger or smaller (if cleared).
drawingPane.setPreferredSize(area);

//Let the scroll pane know to update itself
//and its scrollbars.
drawingPane.revalidate();
}
drawingPane.repaint();
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}


public void setColor(Color color) {
this.color = color;
}

public JPanel getDrawingPane() {
return drawingPane;
}
}

最佳答案

不要尝试重新发送鼠标监听器调用,如果这是您想要做的。你不是老鼠。相反,让鼠标监听器方法简单地调用执行实际工作的对象上的其他方法。你的鼠标监听程序应该是微不足道的“如果这个,调用这个,如果那个,调用那个。”

然后,如果对象之间存在关系,让其中一个接收鼠标监听器点击。然后调用应受该单击影响的所有对象上的方法。

我过去所做的,例如,将网格放置在另一个摆动对象内时,是创建我自己的听众模式。然后,当对象 X 发生需要传递给对象 Y 的事情时,我使用我的监听器模式。这样,每当调用给定方法时,需要完成哪些工作就很清楚了。而且它的可重用性更高。因此,如果它不是鼠标单击而是触发某些内容的菜单,我就不必重复逻辑。

对不起,如果这不是很清楚。

关于java - 监听器调用另一个监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7811653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com