【问题标题】:How to add drawPolygon() to a button listener?如何将 drawPolygon() 添加到按钮侦听器?
【发布时间】:2013-11-15 11:49:26
【问题描述】:

我正在尝试对事件侦听器进行练习。为了绘制一个三角形,我使用了 drawPolygon 方法,但我不知道如何将它添加到 buttonListener 以执行 actionListener。 这是代码的一部分:

public class Triangle extends Figure {
            int x[];
            int y[];
            int n;

            Triangle(boolean f, Color c, int x, int y, int n) {
                super(f, c, x, y);
                int xPoints[];
                int yPoints[];
                int nPoints[];
            }

            @Override
            public void drawMySelf(Graphics g) {

                int xPoints[] = {80, 150, 80};
                int yPoints[] = {80, 150, 150};
                int nPoints = 3;

                Polygon P = new Polygon(x, y, n);
                if (filled)
                    g.fillPolygon(P);
                else g.drawPolygon(P);

            }
        }
@Override
            public void actionPerformed(ActionEvent e) {


                String kommando = e.getActionCommand();

                int width = drawPanel.getWidth();
                int heigth = drawPanel.getHeight();


                if (kommando.equals("Cercle")) {

                    theFigures.add(new Circle(false, Color.BLUE, 20, 20, 50));

                 } else if (kommando.equals("Rektangle")) {
                    theFigures.add(new Rectangle(false, Color.BLACK, 300, 100,
                            150, 100));
                 } else if (kommando.equals("Triangele")) {
                    theFigures.add(___my problem is here___); 
                 }  else if (kommando.equals("Cercle rempli")) {
                     theFigures.add(new Circle(true, Color.YELLOW, 50, 100, 100));
                 }  else if (kommando.equals("Rektangle rempli")) {
                     theFigures.add(new Rectangle(true, Color.GREEN, 500, 50,
                                100, 250));


 }

【问题讨论】:

    标签: java eclipse button event-handling


    【解决方案1】:

    首先,您必须创建一些按钮(用于圆形、矩形等),并通过 setActionCommand 方法配置它们。然后将带有 button.addActionListener 的 actionListener 添加到其中。我认为this 文章会对您有所帮助。

    【讨论】:

    • 这是一个很好的例子,但我的要求是如何添加drawPolygone()她:theFigures.add(new Triangle(.......));看看我的代码。我已经创建了按钮和所有东西。我想在按下按钮时显示一个三角形。否则谢谢你的回答。
    • 怎么样:theFigures.add(new Triangle(false, Color.YELLOW, 50, 100, 100));
    猜你喜欢
    • 1970-01-01
    • 2021-03-21
    • 2015-02-04
    • 2011-08-21
    • 2011-07-20
    • 1970-01-01
    • 2023-03-24
    • 2012-11-11
    • 2019-07-03
    相关资源
    最近更新 更多