我想在java中一個非常簡單的GUI。 我剛剛創建了一個帶按鈕的小圖形用戶界面,當我們點擊每個按鈕時,它會打開一個網站。在java中的簡單GUI問題
所以我必須有3個按鈕: 按鈕1 = Gmail的 BUTTON2 =谷歌 BUTTON3 =雅虎
當我點擊Button1的,有時它會打開Gmail或谷歌或雅虎。 其他按鈕的問題也一樣。
爲什麼?
這裏是我的代碼非常簡單:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Gui extends Frame implements WindowListener,ActionListener {
//TextField text = new TextField(20);
Button a, b, c;
Process p1, p2, p3;
//private int numClicks = 0;
public static void main(String[] args) {
Gui myWindow = new Gui("Miquelon's");
myWindow.setSize(350,100);
myWindow.setVisible(true);
}
public Gui(String title) {
super(title);
setLayout(new FlowLayout());
addWindowListener(this);
a = new Button("Gmail");
b = new Button ("Google");
c = new Button ("Yahooooo");
add(a);
add(b);
add(c);
//add(text);
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
try
{
{
p1 = Runtime.getRuntime().exec("cmd /c start https://mail.google.com");
p2 = Runtime.getRuntime().exec("cmd /c start https://google.com");
p3 = Runtime.getRuntime().exec("cmd /c start https://yahoo.com");
}
}
catch (IOException ex)
{
Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}
謝謝
你有沒有試着用調試器來運行這一點,看看會發生什麼? – 2011-03-16 20:38:55
@Babak,查看actionPerformed事件,你會發現什麼是錯的 – 2011-03-16 20:43:52
參見Desktop.browse(URL)。打開瀏覽器比處理流程容易得多。 – 2011-03-16 20:50:21