2014年2月23日 09:51:54
成功添加了打开官网的事件,
回头研究下,那个打开url的类
java的System.getProperty()方法可以获取的值
###################################
2014年2月23日 22:16:25 今天搬家,下午很忙。回来整理下。今天的实现了打开url和打开文件的事情效果
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
/*瑞德医疗官网,工作日志*/ private
JPanel getInfoJP() { if(infoJP==null){ infoJP = new
JPanel(); //infoJP.setSize(500, 200); infoJP.setLayout(new
FlowLayout()); JButton ryzurNet =new
JButton("瑞德医疗官网"); infoJP.add(ryzurNet); ryzurNet.setMnemonic(KeyEvent.VK_I); ryzurNet.addActionListener(new
RyzurInternet()); JButton ryzurLog = new
JButton("查看本地日志"); infoJP.add(ryzurLog); ryzurLog.setMnemonic(KeyEvent.VK_I); ryzurLog.addActionListener(new
ReadLog()); } return
infoJP; } |
所需的事件类 如下
打开指定url的页面
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
package
cn.wuwenfu.swing;/* * open url www.ryzur.com.cn * 2014年2月23日 22:01:15 * * */import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public
class RyzurInternet implements
ActionListener { @Override public
void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String url = "www.ryzur.com.cn"; BareBonesBrowserLaunch.openURL(url); } } |
下面这个引用网络上别人写的类,只测试了window,可以正常打开。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 |
package
cn.wuwenfu.swing;///////////////////////////////////////////////////////// //Bare Bones Browser Launch // //Version 1.5 (December 10, 2005) // //支持: Mac OS X, GNU/Linux, Unix, Windows XP// // ///////////////////////////////////////////////////////// import
java.lang.reflect.Method; public
class BareBonesBrowserLaunch { public
static void openURL(String url) { try
{ browse(url); } catch
(Exception e) { } } private
static void browse(String url) throws
Exception { //获取操作系统的名字 String osName = System.getProperty("os.name", ""); // System.out.println(osName); if
(osName.startsWith("Mac OS")) { //苹果的打开方式 Class fileMgr = Class.forName("com.apple.eio.FileManager"); Method openURL = fileMgr.getDeclaredMethod("openURL", new
Class[] { String.class
}); openURL.invoke(null, new
Object[] { url }); } else
if (osName.startsWith("Windows")) { //windows的打开方式。 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "
+ url); } else
{ // Unix or Linux的打开方式 String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"
}; String browser = null; for
(int count = 0; count < browsers.length && browser == null; count++) //执行代码,在brower有值后跳出, //这里是如果进程创建成功了,==0是表示正常结束。 if
(Runtime.getRuntime().exec(new
String[] { "which", browsers[count] }).waitFor() == 0) browser = browsers[count]; if
(browser == null) throw
new Exception("Could not find web browser"); else //这个值在上面已经成功的得到了一个进程。 Runtime.getRuntime().exec(new
String[] { browser, url }); } } } |
另一个事件 打开文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 |
package
cn.wuwenfu.swing;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;/* * click button ,open log file * 2014年2月23日 22:03:49 * */public
class ReadLog implements
ActionListener{ @Override public
void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Runtime rt = Runtime.getRuntime(); try
{ Process p = rt.exec("cmd /c start c:/log.txt"); } catch
(IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }} |
到这里,界面上2个按钮的功能完毕。还有2个按钮,需要负责工程的启动和关闭。明天有空再弄
今天的收获:java的按钮添加事件,很简单,没有之前的复杂,自己感到自信了很多。
另外一个收获:今天看了电子书,<<php实战>>,其中提到一个细节,可以使用while循环,确保程序不会退出。运行错误,则立刻重启。 避免额外写程序 监听和重启当前程序。
github 在尝试使用,英文不会的好多,目前可以阅读源代码了,尝试看了CI的源码,有难度
原文:http://www.cnblogs.com/jsRunner/p/3561651.html