本帖最后由 itisdream 于 2014-7-1 15:28 编辑
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.*;
import java.io.File;
public class MyWindowDemo
{
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okbut;
MyWindowDemo()
{
init();
}
public void init()
{
f = new Frame("My window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("转到");
ta = new TextArea(25,70);
d = new Dialog (f,"提示信息—自己",true);
d.setBounds(400, 200, 240, 150);
d.setLayout(new FlowLayout());
okbut = new Button ("确定");
lab = new Label ();
d.add(lab);
d.add(okbut);
f.add(tf);
f.add(but);
f.add(ta);
MyEvent();
f.setVisible(true);
}
private void MyEvent()
{
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e )
{
d.setVisible(true);
}
});
okbut.addKeyListener(new KeyAdapter()
{
public void actionPerformed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
show();
}
}
});
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
show();
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MyWindowDemo();
}
public void show()
{
String dirPath = tf.getText();
File dir = new File(dirPath);
if (dir.exists()&&dir.isDirectory())
{
ta.setText(null);
String []names = dir.list();
for(String name:names)
{
ta.append(name+"\r\n");
}
}
else
{
String info = "错误路径"+dirPath+"请重输";
lab.setText(info);
d.setVisible(true);
}
//ta.setText(dir);
//System.out.println(text);
tf.setText(null);
}
}
|
|