与actionListener静态类冲突

时间:2010-11-13 00:43:45

标签: java swing graphics jbutton

我正在寻找一些方向。我想要一个按钮来支持图形内容中绘制的内容。我之前使用过jframe和监听器的按钮。但不知何故,听众没有被接受。我确信它与声明的两个类有关。谁能告诉我问题是什么或冲突?

我不能使用//f.addWindowListener(this);   //b.addActionListener(this); 它标志着错误,这就是为什么它们被写成评论...... = S

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/* implements ActionListener*/
class JFramePaint1 extends JFrame implements ActionListener {

    public static int activa = 0;
    public static JButton b = new JButton("b");

    public static void main(String[] a) {

        JFrame f = new JFrame();
        f.setTitle("Drawing Graphics in Frames");
        f.setSize(800, 650);
        f.setLocation(200, 50);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setContentPane(new ContentComponent());
        f.getContentPane().add(b);

        //f.addWindowListener(this);
        //b.addActionListener(this);

        f.setVisible(true);
    }

    static class ContentComponent extends JPanel {

        public void paint(Graphics g) {
            g.setColor(Color.RED);
            g.fillRect(0, 40, 800, 650);
            if (activa == 1) {

                g.setColor(Color.BLACK);
                g.drawRect(40, 40, 150, 80);
                int x = 40;
                int y = 40;
                for (int i = 0; i < 4; i++) {

                    g.drawRect(x + 10, y + 10, 150, 80);
                    x = x + 10;
                    y = y + 10;
                }
            }
        }//del paint
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b) {
            System.out.println("entro");
        }
    }

    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    public void windowOpened(WindowEvent e) {}

    public void windowClosed(WindowEvent e) {}

    public void windowActivated(WindowEvent e) {}

    public void windowDeactivated(WindowEvent e) {}

    public void windowIconified(WindowEvent e) {}

    public void windowDeiconified(WindowEvent e) {}
}

2 个答案:

答案 0 :(得分:2)

你不能使用

  //f.addWindowListener(this);
  //b.addActionListener(this);

因为你是一个静态的方法。您期望this指向什么?

答案 1 :(得分:1)

您可能会发现将main()中的所有代码移动到构造函数中会更容易。 Fwiw,我也不知道为什么要扩展JFrame,还要在main方法中创建一个新的JFrame实例。我想你想要这样的东西(我没有测试过,但这是一般的想法)。

FWIW,您可能希望阅读一本优秀的Java语言教程,以正确理解“静态”的含义。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/* implements ActionListener*/
class JFramePaint1 extends JFrame implements ActionListener {

    private int activa = 0;
    private JButton b = new JButton("b");

    JFramePaint1() {
        setTitle("Drawing Graphics in Frames");
        setSize(800, 650);
        setLocation(200, 50);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setContentPane(new ContentComponent());
        getContentPane().add(b);

        addWindowListener(this);
        addActionListener(this);
    }

    public static void main(String[] a) {
      new JFramePaint1().setVisible(true);
    }

    class ContentComponent extends JPanel {

        public void paint(Graphics g) {
            g.setColor(Color.RED);
            g.fillRect(0, 40, 800, 650);
            if (activa == 1) {

                g.setColor(Color.BLACK);
                g.drawRect(40, 40, 150, 80);
                int x = 40;
                int y = 40;
                for (int i = 0; i < 4; i++) {

                    g.drawRect(x + 10, y + 10, 150, 80);
                    x = x + 10;
                    y = y + 10;
                }
            }
        }//del paint
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b) {
            System.out.println("entro");
        }
    }

    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    public void windowOpened(WindowEvent e) {}

    public void windowClosed(WindowEvent e) {}

    public void windowActivated(WindowEvent e) {}

    public void windowDeactivated(WindowEvent e) {}

    public void windowIconified(WindowEvent e) {}

    public void windowDeiconified(WindowEvent e) {}
}