如何将 ActionListener 添加到添加到 JPanel 的 JButton?

发布于 2024-11-18 08:30:39 字数 2390 浏览 1 评论 0原文

我的目标是制作油漆类型的应用程序。

谢谢你的建议.. 但代码中仍然存在错误。

在 netbeans IDE 中,button1.addActionListener(panel); 行中出现错误:“类中的方法 addActionListener 无法应用于给定类型”

所以我使用它的建议将其转换为 ActionListener。所以该行是button1.addActionListener((ActionListener) panel); 但仍然在下面的 'if 语句' 中出现错误:找不到符号变量 'button1'

再次感谢您的帮助。 这是更改后的代码。我更改了变量名称并使用适配器类来使代码更简单。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class r extends JPanel
{

public int x1,x2,y1,y2;

public static double SWITCH;

public r()

{

    setBackground(Color.WHITE);

    addMouseListener(new MouseAdapter()

    {

         public void mousePressed(MouseEvent m)

         {

         x1=m.getX();

         y1=m.getY();

         repaint();

         }

         public void mouseReleased(MouseEvent m)

         {

         x1=x2=y1=y2=0;

         repaint();

         }

    });

    addMouseMotionListener(new MouseMotionAdapter()

    {

        public void mouseDragged(MouseEvent m)

        {

           x2=m.getX();

           y2=m.getY();

           repaint();

        }

    });

  }

  public void paintComponent(Graphics g)

  {

    super.paintComponent(g);

    if(SWITCH == 2)

    {

        g.drawRect(x1, y1, x2, y2);

    }

    else if (SWITCH == 3)

    {

        g.drawOval(x1,y1,x2,y2);

    }

    else

    {

        g.drawString("qwe", x1, y1);

    }

 }

 }

 public class q extends JFrame implements ActionListener
 {
 public static void main(String[] args)

 {

    q window = new q();

    window.setVisible(true);

    window.setSize(1024, 800);

    window.setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container cont = window.getContentPane();

    cont.setLayout(new GridLayout(2,2));

    r panel = new r();

    JPanel BPanel = new JPanel();

    cont.add(panel);

    cont.add(BPanel);

    BPanel.setBackground(Color.blue);

    JButton button1,button2;

    button1 = new JButton("Rect");

    button2 = new JButton("Oval");

    BPanel.add(button1);

    BPanel.add(button2);

    button1.addActionListener((ActionListener) panel);

    button2.addActionListener((ActionListener) panel);

}

public void actionPerformed(ActionEvent a)

{

    Object obj;

    obj=a.getSource();

    if (obj== button1)

    {

        SWITCH = 2;

        repaint();

    }

    else

    {

       SWITCH = 3;

       repaint();

    }

}

}

My aim is to make a paint type application.

Thank you for your suggestions..
but there is still error in the code.

In netbeans IDE error : 'method addActionListener in class cannot be applied to given types' occurs in the line button1.addActionListener(panel);

so i used it suggestion to cast it to ActionListener. so the line is button1.addActionListener((ActionListener) panel);
but still in the below 'if statement' error occurs : cannot find symbol variable 'button1'

Thanks again for your help.
here is changed code. i changed the variable names and used adapter class to make code simpler.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class r extends JPanel
{

public int x1,x2,y1,y2;

public static double SWITCH;

public r()

{

    setBackground(Color.WHITE);

    addMouseListener(new MouseAdapter()

    {

         public void mousePressed(MouseEvent m)

         {

         x1=m.getX();

         y1=m.getY();

         repaint();

         }

         public void mouseReleased(MouseEvent m)

         {

         x1=x2=y1=y2=0;

         repaint();

         }

    });

    addMouseMotionListener(new MouseMotionAdapter()

    {

        public void mouseDragged(MouseEvent m)

        {

           x2=m.getX();

           y2=m.getY();

           repaint();

        }

    });

  }

  public void paintComponent(Graphics g)

  {

    super.paintComponent(g);

    if(SWITCH == 2)

    {

        g.drawRect(x1, y1, x2, y2);

    }

    else if (SWITCH == 3)

    {

        g.drawOval(x1,y1,x2,y2);

    }

    else

    {

        g.drawString("qwe", x1, y1);

    }

 }

 }

 public class q extends JFrame implements ActionListener
 {
 public static void main(String[] args)

 {

    q window = new q();

    window.setVisible(true);

    window.setSize(1024, 800);

    window.setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container cont = window.getContentPane();

    cont.setLayout(new GridLayout(2,2));

    r panel = new r();

    JPanel BPanel = new JPanel();

    cont.add(panel);

    cont.add(BPanel);

    BPanel.setBackground(Color.blue);

    JButton button1,button2;

    button1 = new JButton("Rect");

    button2 = new JButton("Oval");

    BPanel.add(button1);

    BPanel.add(button2);

    button1.addActionListener((ActionListener) panel);

    button2.addActionListener((ActionListener) panel);

}

public void actionPerformed(ActionEvent a)

{

    Object obj;

    obj=a.getSource();

    if (obj== button1)

    {

        SWITCH = 2;

        repaint();

    }

    else

    {

       SWITCH = 3;

       repaint();

    }

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝咒 2024-11-25 08:30:39

我认为你缺乏基本的Java概念。

在任何静态上下文中都没有“this”引用,即 static void main。

将“this”替换为“r”、“z3”的实例。

b1.addActionListener(z3);
b2.addActionListener(z3);

除此之外,你的 UI 代码确实到处都是,但这是另一个问题的讨论。

I think your basic Java concepts are lacking.

There is no "this" reference in any static context, i.e. static void main.

Replace "this" with your instance of "r", "z3".

b1.addActionListener(z3);
b2.addActionListener(z3);

Other than that, your UI code is really all over the place, but that's a discussion for another question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文