作者TonyQ (骨头)
看板java
标题Re: [问题] Java视窗可以这样做吗?
时间Tue Aug 7 19:55:39 2007
※ 引述《RWA (基本上我是个演员)》之铭言:
: 假设我原有的程式都已完成
: 并且都可以在command line下执行
: 想请问我可以用制作视窗介面的方式
: 制作一个按钮 当按下去後 就去跑我在command line下的指令
: 例如按下後依序:
: 执行 1.java Server ip port
: 执行 2.java Client ip port
: .
: .
: 有点像shell script的写法,而操作方式像php的system("command")呼叫
: 感觉逻辑怪怪的XD 只是想说做出一个视窗让使用者方便执行程式
: 请问这是可行的吗?
: ps:我有试过写bat档,可行 但我只是纯粹想用视窗方式完成..:)
这不是很困难的
首先GUI的部份你就自己去努力吧XD
架Frame 用container add button
写ActionListener
以上简单带过
然後你在对应的触发上面写上 Runtime相关的处理
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html
你也可以用bat先写好 再用Runtime去做
如果你需要另外输入arg
就先取得exec之後的 Process的输入串流
如果你需要取得程式跑在画面上的结果
你就取得Process的输出串流
我底下附上一个以前用 Runtime操作ipconfig取卡号的sample:P
---
import java.io.*;
class getMac2{
private String getMacAddressIP(){
String str="";
String macAddress="";
try {
//执行 "ipconfig /all "
Process pp= Runtime.getRuntime().exec ("ipconfig /all");
//取得输入串流
InputStreamReader ir = new InputStreamReader(
pp.getInputStream());
BufferedReader input = new BufferedReader (ir);
//用来判断是否为区网的网卡号码
boolean check =false;
for (int i = 1; i<100; i++)
{
str=input.readLine();
if (str!=null){
//通过区网网段
if(str.indexOf("Ethernet adapter")!=-1) check=true;
//取得网路卡号码
if(check&&str.indexOf("Physical Address")!=-1){
macAddress=str.substring(str.indexOf(":")+2,str.length());
break;
}
}
}
}catch (IOException ex) {}
//回传
return macAddress;
}
public static void main(String[] args)
{
getMac2 getmac;
getmac=new getMac2();
String mac="";
mac=getmac.getMacAddressIP();
//输出
System.out.println(mac);
}
}
--
▄▅▆▇███▇▆▅▄▃ ╰┼╯─╮ ╮
◥███████████◣ ╰┼╯=│=│
◥██████───────◣ *. ╯ ╯ ╯ の 物 语 .*
◥███████──────◣ ~ ◢◣ ◢◣
◥██████───────◤ ◥◤* 空白的世界.翼
*◥◤
◥██▁▂▃▄▅▆▇███▆▅▄▃▂▂
~telnet://tony1223.no-ip.info
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.134.27.68