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 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) {}
}
You can’t use
because you’re in a static method. What do you expect
thisto point to?