服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

-----java实现:文件选择器中运行.exe文件!


  import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;


class chooserrun extends jframe implements actionlistener
{
jbutton button;
jtextarea text;
jtextpane textpane;
fileinputstream readfile;
jscrollpane scroll;
// container con;
jpanel con;
jfilechooser chooser=new jfilechooser();
//==========================================

public chooserrun()
{
super("有文件选择器的窗口");
button=new jbutton("打开文件选择器");
button.addactionlistener(this);
textpane=new jtextpane();
jfilechooser chooser=new jfilechooser();
scroll=new jscrollpane(textpane);
// jfilechooser chooser=new jfilechooser();
//不能在此处初始化!
this.setsize( new dimension(200,200));
setvisible(true);
con=(jpanel)this.getcontentpane();
con.add(button,borderlayout.north);
con.add(scroll,borderlayout.center);
addwindowlistener(new windowadapter() {
public void windowclosing(windowevent e) {
dispose();system.exit(0);}});
}
//====================================================
public void actionperformed(actionevent e)
{
if(e.getsource()==button)
{
string s;
int state =chooser.showopendialog(null);
file file=chooser.getselectedfile();
{
try
{ runtime ce=runtime.getruntime();
ce.exec(file.tostring());
}
catch(ioexception e1)
{
e1.printstacktrace();
}
}
}
}
//--------------------------------------
}
import javax.swing.uimanager;
import java.awt.*;
public class mytest
{
public static void main(string args[])
{
chooserrun run=new chooserrun();
run.pack();
dimension screensize = toolkit.getdefaulttoolkit().getscreensize();
dimension framesize = run.getsize();
if (framesize.height > screensize.height) {
framesize.height = screensize.height;
}
if (framesize.width > screensize.width) {
framesize.width = screensize.width;
}
run.setlocation((screensize.width - framesize.width) / 2, (screensize.height - framesize.height) / 2);
}
}

扫描关注微信公众号