2011-03-16 68 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); 
    } 
}