开发者

conflict with actionListener static classes

I'm looking for some orientation. I want a button to power the stuff that is painted in the graphics content. I have used buttons before with jframes and listeners. But somehow the listener is not being accepted. I am sure it has to do with the two classes declared. Could anybody tell me what the problem is or the conflict?

I can't use //f.addWindowListener(this); //b.addActionListener(this); It marks error, that's why they are written as comments... =S

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

/* implements ActionListener*/
class JFramePaint1 extends JFrame implements ActionLi开发者_如何学JAVAstener {

    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) {}
}


You can't use

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

because you're in a static method. What do you expect this to point to?


You might find it easier to move all the code you have in main() into a constructor. Fwiw, I also don't know why you're extending JFrame but also creating a new instance of JFrame in your main method. I think you want something like this (I haven't tested it, but it's the general idea).

FWIW, you might want to read a good Java language tutorial to properly understand what "static" means.

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) {}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜