2011-03-16 102 views
1

我想在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) {} 


} 

謝謝

+0

你有沒有試着用調試器來運行這一點,看看會發生什麼? – 2011-03-16 20:38:55

+0

@Babak,查看actionPerformed事件,你會發現什麼是錯的 – 2011-03-16 20:43:52

+1

參見Desktop.browse(URL)。打開瀏覽器比處理流程容易得多。 – 2011-03-16 20:50:21

回答

4

你的actionPerformed運行所有三個。您需要使用actionPerformed來確定按下哪個按鈕,然後運行相應的命令。

public void actionPerformed(ActionEvent e) 
{ 
    String address = ""; 
    if(e.getSource() == a) address = "https://mail.google.com"; 
    else if(e.getSource() == b) address = "https://google.com"; 
    else if(e.getSource() == c) address = "https://yahoo.com"; 
    else return; // not one of the three buttons, get out! 
    try 
    { 
     // only NEED to store the process if you want to do something with it later 
     // I just let mine dangle :) it works for me! 
     Runtime.getRuntime().exec("cmd /c start " + address); 

    } 
    catch (IOException ex) 
    { 
     Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
3

您添加相同的ActionListener所有三個按鈕。在ACtionListener中,你打開雅虎谷歌和Gmail。那麼你還期望什麼?

如果你在actionPerformed方法中沒有按下哪個按鈕,那麼這是正確的行爲。

有各種可能性來解決這個問題...使用的ActionListener爲每個按鈕(例如anonymoous)

或者使用e.getSource()確定actionPerformed方法哪個按鈕被按下。

例如:

if(e.getSource().equals(a)) { 
    address = "https://mail.google.com"; 
} 
2

你不要在你的actionPerformed事件中標識每個按鈕。

每次執行事件的所有三個命令被執行

1

一個考慮命名您的一到Gmail所以它更具描述性的時間。

a.addActionListener(new ActionListener() { 
    void actionPerformed(ActionEvent e) { 
     p1 = Runtime.getRuntime().exec("cmd /c start https://mail.google.com"); 
    } 
    }); 

但總而言之,它的所有三個運行。

0

如果你想要做三件事情,你需要三個不同的動作監聽器(每個按鈕一個)或者一個動作監聽器(一個用於所有按鈕),它試圖確定哪個按鈕被按下,並根據哪個按鈕來調用它。

你現在所擁有的是一個動作監聽器,它可以完成所有三件事情,而不用考慮按下了哪個按鈕。

0

您也可以嘗試設置每個按鈕的ActionCommand,以便它們都可以使用相同的事件偵聽器。如果/當你想添加一個新按鈕,這也可以提高可維護性。

a = new Button("Gmail"); 
    a.setActionCommand("https://gmail.com"); 
    b = new Button ("Google"); 
    b.setActionCommand("https://google.com"); 
    c = new Button ("Yahooooo"); 
    c.setActionCommand("https://yahoo.com"); 

,並重新實現你的監聽方法,例如:

public void actionPerformed(ActionEvent e) 
{ 
    try 
    { 

     { 
      Runtime.getRuntime().exec("cmd /c start " + e.getActionEvent()); 
     } 
    } 
    catch (IOException ex) 
    { 
     Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex); 
    } 
}