WEB开发网
开发学院WEB开发Jsp -----JAVA实现:文件选择器中运行.exe文件! 阅读

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

 2008-01-05 09:22:10 来源:WEB开发网   
核心提示:import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;class Chooserrun extends JFrame implements ActionListener {JButton button;JText

  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);
}
}

Tags:JAVA 实现 文件

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接