本帖最后由 覃宏海 于 2012-9-17 15:30 编辑
红色部分是问题点
在监听内部怎么再建立监听?
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class MyWindowDemo {
public static void main(String[] args) {
new MyWindowDemo();
}
private Frame f;
private Button but;
private TextField tf;
private TextArea ta;
private Dialog d;
private Label l;
private Button okBut;
MyWindowDemo(){
init();
}
public void init(){
f = new Frame();
f.setBounds(300, 150, 650, 500);
f.setLayout(new FlowLayout());
tf = new TextField(50);
but = new Button("转到");
ta = new TextArea(26,80);
f.add(tf);
f.add(but);
f.add(ta);
ta.setVisible(true);
f.setVisible(true);
myEvent();-------------------------------这里建立监听的引用
}
public void myEvent(){----------------------------监听
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
show();---------------------------------调用show
}
});
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
show();
}
}
});
okBut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
});
}
public void show(){
String text = tf.getText();
ta.setText("");
File dir = new File(text);
if(dir.exists()&&dir.isDirectory()){
String[] names = dir.list();
for(String texts : names){
ta.append(texts+"\r\n");
}
}
else{---------------------------------------------------在else里建立Dialog,如果不在class MyWindowDemo里建立Dialog,那么应该在哪里建议呢?
d = new Dialog(f, "我的警告", true);
d.setBounds(400,200,300,100);
d.setLayout(new FlowLayout());
l = new Label("rwtqwaqdfwefc");
okBut = new Button("确定");
d.add(l);
d.add(okBut);
d.setVisible(true);--------------------------------------------在这里怎么引用蓝色的监听????
}
tf.setText(null);
}
}
|