程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JAVA實現文件選擇器中運行.exe文件

JAVA實現文件選擇器中運行.exe文件

編輯:關於JAVA

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved