gpt4 book ai didi

java - 无法设法在两个类之间使用 ActionListener

转载 作者:搜寻专家 更新时间:2023-11-01 03:25:58 25 4
gpt4 key购买 nike

我尝试实现两个类,一个专用于界面,另一个专用于允许用户在其上绘制形状的 Canvas 。

所以基本上,让我们使用我的按钮之一,这是我的 Jbutton bCircle,我想在其中初始化匿名类中的操作。我只放了具体的一 block :

public class Application extends JFrame implements ActionListener {

toolBar.add(Box.createHorizontalGlue());
toolBar.add(Box.createVerticalGlue());
toolBar.add(bSquare, toolBar.getComponentCount() - 1);
toolBar.add(bCircle, toolBar.getComponentCount() - 1);
bCircle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
//Action ?????
}
});
toolBar.add(bRectangle, toolBar.getComponentCount() - 1);
toolBar.add(bTriangle, toolBar.getComponentCount() - 1);
toolBar.add(bErase, toolBar.getComponentCount() - 1);
toolBar.setOrientation(SwingConstants.VERTICAL);
container.add(toolBar, BorderLayout.WEST);
}

在另一个类中,这里是我希望 Jbutton bCircle 的选定状态在我单击 Canvas 时执行的操作:

public class DrawingCanvas extends JPanel implements MouseListener, MouseMotionListener {

public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == bCircle) {
shapes.add(new Circle(e.getX(),e.getY()));
}
repaint();
}
}

shapes 是一个存储形状的 ArrayList,使 Canvas 更易于清洁,Circle 是一个仅包含圆形绘制的类。

最后,目的是让用户先点击代表四种形状的四个按钮,然后通过在 Canvas 上按下鼠标来绘制它们。但是我不知道如何链接这两个类,请帮助我,

提前致谢

P.S:我第二次更新的完整应用程序类(接口(interface)):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

public class Application extends JFrame implements ActionListener {

{
//Set appearance Look & Feel for the window
try { javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception e) {
e.printStackTrace();
}
}

private DrawingCanvas canvas = new DrawingCanvas();
private JPanel container = new JPanel();
private JPanel commandes = new JPanel();
private JMenuBar menu = new JMenuBar();
private JMenu newFile = new JMenu();
private JMenu open = new JMenu();
private JMenu save = new JMenu();
private JFileChooser fileChooser;
private JToolBar toolBar = new JToolBar();

private JButton bSquare = new JButton("Square");
private JButton bRectangle = new JButton("Rectangle");
private JButton bCircle = new JButton("Circle");
private JButton bTriangle = new JButton("Triangle");
private JButton bErase = new JButton("CLEAR");

//public static boolean bIsSelected = false;

public Application(){

this.setTitle("Animation");
this.setSize(579, 432);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);

container.setBackground(Color.white);
container.setLayout(new BorderLayout());
container.add(canvas, BorderLayout.CENTER);
this.setContentPane(container);

toolBar.add(Box.createHorizontalGlue());
toolBar.add(Box.createVerticalGlue());
toolBar.add(bSquare, toolBar.getComponentCount() - 1);
toolBar.add(bCircle, toolBar.getComponentCount() - 1);

bCircle.addActionListener(new SampleActionListener(canvas, new Circle()));

toolBar.add(bRectangle, toolBar.getComponentCount() - 1);
toolBar.add(bTriangle, toolBar.getComponentCount() - 1);
toolBar.add(bErase, toolBar.getComponentCount() - 1);
bErase.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent erase){
canvas.getShapes().clear();
repaint();
}
});
toolBar.setOrientation(SwingConstants.VERTICAL);
container.add(toolBar, BorderLayout.WEST);

menu.add(newFile);
newFile.setIcon(new ImageIcon("src/images/new.png"));
menu.add(open);
open.setIcon(new ImageIcon("src/images/open.png"));
menu.add(save);
save.setIcon(new ImageIcon("src/images/save.png"));
this.setJMenuBar(menu);

}

public class SampleActionListener implements ActionListener {

private final DrawingCanvas canvas2;
private final ShapeGenerator shapeGenerator;

public SampleActionListener(DrawingCanvas canvas, ShapeGenerator shapeGenerator) {
this.canvas2 = canvas;
this.shapeGenerator = shapeGenerator;
}

public void actionPerformed(ActionEvent event) {
this.canvas2.setShapeGenerator(shapeGenerator);
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}

public static void main(String[] args) {
// TODO Auto-generated method stub
Application App = new Application();

}
}

这是 Canvas 的类,也是第二次上传:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JPanel;



public class DrawingCanvas extends JPanel implements MouseListener {

private ArrayList<Shape> shapes = new ArrayList<>();
private Shape shapeUnderMouse = null;

public ArrayList<Shape> getShapes() {
return shapes;
}

public void setShapes(ArrayList<Shape> shapes) {
this.shapes = shapes;
}

public DrawingCanvas() {
super();
addMouseListener(this);
}

public void paint(Graphics g){
for(int i = 0;i < shapes.size();i++){
shapes.get(i).paint(g);
}
}


@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void addShape(Shape anyShape)
{
shapes.add(anyShape);
}

@Override
public void mousePressed(MouseEvent e) {

shapes.add(this.shapeGenerator.getGeneratedShape(e.getX(), e.getY()));
repaint();
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void setShapeGenerator(ShapeGenerator shapeGenerator) {
// TODO Auto-generated method stub

}

现在,类的形状:

import javax.swing.JPanel;

public class Shape extends JPanel{

protected int posX;
protected int posY;

/*public Shape(int posX, int posY) {
super();
this.posX = posX;
this.posY = posY;
}*/

public int getPosX() {
return posX;
}

public void setPosX(int posX) {
this.posX = posX;
}

public int getPosY() {
return posY;
}

public void setPosY(int posY) {
this.posY = posY;
}

}

我的小圈类:

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Float;

import javax.swing.JPanel;

public class Circle extends Shape implements ShapeGenerator{

/*public Circle(int posX, int posY) {
super(posX, posY);
// TODO Auto-generated constructor stub
}*/

public void paint(Graphics g){
g.fillOval(posX,posY,40,40);
g.setColor(Color.blue);
}

public Shape getGeneratedShape(int x, int y) {
return new Ellipse2D.Float(x, y, 10, 10); //--> does not work, ask me to change method type to a float
return new Circle();
}

}最后是接口(interface),所有类都在单独的类中:
公共(public)接口(interface) ShapeGenerator {

Shape getGeneratedShape(int posX, int posY);

}

最佳答案

事实上,这很容易。不要使用匿名类。

例如,对于您的代码,您只需附加一个已定义的监听器。例如:

bCircle.addActionListener(new SampleActionListener(pan, new CircleShapeGenerator()));

这是实现 ActionListener 接口(interface)的 SampleActionListener

public class SampleActionListener implements ActionListener {

private final DrawingCanvas canvas;
private final ShapeGenerator shapeGenerator;

public SampleActionListener(DrawingCanvas canvas, ShapeGenerator shapeGenerator) {
this.canvas = canvas;
this.shapeGenerator = shapeGenerator;
}

public void actionPerformed(ActionEvent event) {
this.canvas.setShapeGenerator(shapeGenerator);
}
}

这是 CircleShapeGenerator 类

public class CircleShapeGenerator implements ShapeGenerator {
public Shape getGeneratedShape(int x, int y) {
return new Ellipse2D.Float(x, y, 10, 10);
}
}

和接口(interface) ShapeGenerator

public interface ShapeGenerator {
Shape getGeneratedShape(int x, int y);
}

而且,如果您想在矩形按钮上添加 Action 监听器,现在真的很容易。创建这个类:

public class RectangleShapeGenerator implements ShapeGenerator {
public Shape getGeneratedShape(int x, int y) {
return new Rectangle2D.Float(x, y, 10, 10);
}
}

在你的应用类中,添加以下代码

toolBar.add(bRectangle, toolBar.getComponentCount() - 1);
bRectangle.addActionListerner(pan, new RectangleShapeGenerator() )

对于在 DrawingCanvas 中绘制的方法,我认为您应该使用这样的代码:

for (Shape s : shapes) {
Graphics2D g2 = (Graphics2D) g;
g2.draw(s);
}

BR,帕图什

对不起我的英语,我知道它不是很完美......

关于java - 无法设法在两个类之间使用 ActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13783370/

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