黑马程序员技术交流社区
标题:
GUI代码求助
[打印本页]
作者:
陈红建
时间:
2012-8-9 09:03
标题:
GUI代码求助
本帖最后由 陈红建 于 2012-8-9 09:05 编辑
import java.awt.*;
import java.awt.event.*;
import java.io.*;
//这是一个窗体现实目录中的文件
class MyWin extends WindowAdapter //继承WindowAdapter子类,并覆盖他的windowClosing方法
{
public void windowClosing(WindowEvent e)//窗口事件处理
{
System.out.println("我被关闭了!");
System.exit(0);
}
public void windowActivated(WindowEvent e)
{
System.out.println("我被激活了!");
}
}
class CreateWindow
{
private String WindowName;
private int width;
private int height;
private int x;
private int y;
private Frame f;
private TextArea ta;
private TextField text;
private Button b;
private Dialog dia;
private Label lab;
private Button OK;
Runtime run=Runtime.getRuntime();//添加可执行
Process p=null;
class KeyOK implements KeyListener
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
dia.setVisible(false);
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
class KeyText implements KeyListener
{
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
String dirpath=text.getText();
File dir=new File(dirpath);
if(e.getKeyCode()==KeyEvent.VK_ENTER)
if(dir.exists() && dir.isDirectory())
{
ta.setText("");
String names[]=dir.list();
for(String name:names)//从 names数组里面取值赋给name
{
ta.append(name+"\r\n");
}
}
else if (dir.isFile())
{
try {
p=run.exec("cmd /c start "+dirpath);
} catch (IOException e1) {
e1.printStackTrace();
}
}
else
{
String info=dirpath+"输入错误";
lab.setText(info);
dia.setVisible(true);
}
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
CreateWindow(String WindowName,int width,int height,int x,int y)
{
this.WindowName=WindowName;
this.width=width;
this.height=height;
this.x=x;
this.y=y;
}
public void CreateWindows()
{
f=new Frame(WindowName);
f.setBounds(x, y, width, height);
f.setLayout(new FlowLayout(1));//设置布局
ta=new TextArea(20,60);
text=new TextField(30);
b=new Button("转到");
dia=new Dialog(f,"提示信息",true);//初始化一个对话框true设置消息窗口为模态显示就是独占方式显示
dia.setLayout(new FlowLayout());
dia.setBounds(400, 200, 240,150 );
lab=new Label();
OK=new Button("确定");
b.addKeyListener(new KeyText());
text.addKeyListener(new KeyText());
OK.addKeyListener(new KeyOK());
OK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dia.setVisible(false);
}
});
dia.addWindowListener(new WindowListener()
{
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
dia.setVisible(false);
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
b.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String dirpath=text.getText();
File dir=new File(dirpath);
if(dir.exists() && dir.isDirectory())
{
ta.setText("");
String names[]=dir.list();
for(String name:names)//从 names数组里面取值赋给name
{
ta.append(name+"\r\n");
}
}
else if (dir.isFile())
{
<FONT color=red> //我想要在这里是实现一个运行可执行文件的代码应该怎么做啊
//还有判断文件的类型
//在这里可以运行一些例如图片什么的</FONT><FONT color=blue> 搞什么啊 代码都加不上颜色?</FONT> }
else
{
String info=dirpath+"输入错误";
lab.setText(info);
dia.setVisible(true);
}
}
}
);
dia.add(lab);
dia.add(OK);
f.add(text);
f.add(b);
f.add(ta);
f.addWindowListener(new MyWin());
f.setVisible(true);
}
}
class TextDemo
{
public static void main(String args[])
{
CreateWindow c= new CreateWindow("我的窗口",500,400,100,300);//窗体标题,长,宽,x,y显示坐标
c.CreateWindows();
}
}
复制代码
作者:
陈红建
时间:
2012-8-17 13:33
申请加分
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2