与actionListener静态类冲突
问题描述:
我正在寻找一些方向。我想要一个按钮来为绘制在图形内容上的东西提供动力。我以前用jframes和listeners使用过按钮。但不知怎的,听众并未被接受。我相信这与宣布的两个类别有关。有谁能告诉我问题是什么或冲突?与actionListener静态类冲突
我不能使用//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) {}
}
答
,因为你在一个静态方法是不能使用
//f.addWindowListener(this);
//b.addActionListener(this);
。你认为this
指向什么?
答
您可能会发现将main()中的所有代码移动到构造函数中更容易。 Fwiw,我也不知道你为什么要扩展JFrame,但也在你的主要方法中创建一个新的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) {}
}
+0
在分配JFrame f = new JFramePaint1();之后,您也可以在构造函数中执行main方法内的所有东西。 – 2010-11-13 02:10:07
你可以编辑这个更好一点吗? – Jim 2010-11-13 00:45:12
重新格式化的代码;如果不正确请回复。 – trashgod 2010-11-13 01:33:12