问题描述
- java GUI 的一个问题
-
初学者,在写一个GUI , 设置了几个按钮和对应的几个ActiveListener,监听器(MyFrame内部类),MyFrame设置了一个成员变量flag,初值为0,重写了监听器方法,希望改变flag的值,但是在MyFrame中测试,flag一直为初值0,纠结很久了 万分感谢
解决方案
贴个完整带码看看看看看。
解决方案二:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Test {
public static void main(String[] args) {
new MyFrame("my java");
}
}
class MyFrame extends Frame {
int flag = 0;
MyFrame(String s) {
super(s);
setBounds(200,300,700,400);
setLayout(new BorderLayout());
Panel p1 = new Panel(new GridLayout());
Panel p2 = new Panel(new BorderLayout());
Panel p3 = new Panel(new FlowLayout());
Button b1 = new Button("堆栈");
b1.addActionListener(new MonitorSelect1(this));
System.out.println(flag);
Button b2 = new Button("队列");
b2.addActionListener(new MonitorSelect2(this));
Button b3 = new Button("串");
b3.addActionListener(new MonitorSelect3(this));
Button b4 = new Button("递归");
b4.addActionListener(new MonitorSelect4(this));
Button b5 = new Button("树");
b5.addActionListener(new MonitorSelect5(this));
BufferedReader br = null;
String str = null;
TextArea ta = new TextArea();
Label l1 = new Label("代码区");
Label l2 = new Label("功能区");
Label ru = new Label("入队数据");
TextField num1 = new TextField(10);
Button sure = new Button("确定");
Label chu = new Label("出队数据");
TextField num2 = new TextField(10);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
add(p1,BorderLayout.NORTH);
p2.add(l1,BorderLayout.NORTH);
p2.add(ta,BorderLayout.CENTER);
p2.add(l2,BorderLayout.SOUTH);
add(p2,BorderLayout.CENTER);
p3.add(ru);
p3.add(num1);
p3.add(sure);
p3.add(chu);
p3.add(num2);
add(p3,BorderLayout.SOUTH);
/*try {
br = new BufferedReader(new FileReader("F:/java/TestBuffered/TestBuffered1.txt"));
while((str = br.readLine()) != null) {
ta.append(str+"
");
}
}
catch (IOException e) {
System.out.println("文件读取错误");
}*/
pack();
setVisible(true);
}
class MonitorSelect1 implements ActionListener {
MyFrame mf = null;
MonitorSelect1(MyFrame mf) {
this.mf = mf;
}
public void actionPerformed(ActionEvent e) {
mf.flag = 1;
}
}
class MonitorSelect2 implements ActionListener {
MyFrame mf = null;
MonitorSelect2(MyFrame mf) {
this.mf = mf;
}
public void actionPerformed(ActionEvent e) {
mf.flag = 2;
}
}
class MonitorSelect3 implements ActionListener {
MyFrame mf = null;
MonitorSelect3(MyFrame mf) {
this.mf = mf;
}
public void actionPerformed(ActionEvent e) {
mf.flag = 3;
}
}
class MonitorSelect4 implements ActionListener {
MyFrame mf = null;
MonitorSelect4(MyFrame mf) {
this.mf = mf;
}
public void actionPerformed(ActionEvent e) {
mf.flag = 4;
}
}
class MonitorSelect5 implements ActionListener {
MyFrame mf = null;
MonitorSelect5(MyFrame mf) {
this.mf = mf;
}
public void actionPerformed(ActionEvent e) {
mf.flag = 5;
}
}
}
解决方案三:
Java序列化的一个问题
解决方案四:
public class Test1 {
public static void main(String[] args) {
new MyFrame("my java");
}
}
class MyFrame extends Frame {
int flag = 0;
MyFrame(String s) {
super(s);
Button b = new Button();
b.addActionListener(new Monitor(this));
System.out.println(flag);
}
class Monitor implements ActionListener {
MyFrame mf = null;
Monitor(MyFrame mf) {
this.mf = mf;
}
public void actionPerformed(ActionEvent e) {
mf.flag = 3;
}
}
}
大概就是这样的
解决方案五:
代码里的System.out.println(flag);是在监听器触发之前输出的,应该在监听器里面写System.out.println(flag);
解决方案六:
flag,应该改成静态的,我之前也遇到过这样的问题。
解决方案七:
flag改成静态的就行~
时间: 2024-10-07 12:30:08