【问题标题】:Simple GUI question in javajava中的简单GUI问题
【发布时间】:2013-11-04 00:32:14
【问题描述】:

我正在 java 中尝试一个非常简单的 GUI。 我刚刚创建了一个带有按钮的小 GUI,当我们单击每个按钮时,它会打开一个网站。

所以我有 3 个按钮: 按钮1 = gmail 按钮2 = 谷歌 button3 = 雅虎

当我点击 button1 时,有时它会打开 gmail 或 google 或 yahoo。 其他按钮也有同样的问题。

为什么?

这是我非常简单的代码:

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) {}


}

谢谢

【问题讨论】:

  • 你试过用调试器运行它,看看会发生什么?
  • @Babak,看看 actionPerformed 事件,你就会意识到哪里错了
  • 另见 Desktop.browse(URL)。打开浏览器比处理进程要容易得多。

标签: java user-interface awt


【解决方案1】:

您的 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);
    }
}

【讨论】:

    【解决方案2】:

    您将相同的 ActionListener 添加到所有三个按钮。然后在 ACtionListener 中打开 yahoo google 和 gmail。那么你还期待什么?

    如果您的 actionPerformed 方法没有区别按下哪个按钮,那么这是正确的行为。

    解决这个问题有多种可能...为每个按钮使用一个 ACtionListener(例如匿名)

    或使用e.getSource() 来确定在actionPerformed 方法中按下了哪个按钮。

    例如:

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

    【讨论】:

      【解决方案3】:

      您无法识别 actionPerformed 事件中的每个按钮。

      每次执行事件时都会执行所有三个命令

      【讨论】:

        【解决方案4】:

        可以考虑将您的 a 命名为 gmail,以便更具描述性。

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

        但简而言之,它正在运行所有三个。

        【讨论】:

          【解决方案5】:

          如果你想做三件不同的事情,你要么需要三个不同的动作侦听器(每个按钮一个),每个都做一件事,要么需要一个动作侦听器(一个用于所有按钮)来尝试确定哪个按钮是按下并根据调用它的按钮执行某些操作。

          您现在拥有的是一个动作侦听器,它可以完成所有三件事,而无需考虑按下了哪个按钮。

          【讨论】:

            【解决方案6】:

            您还可以尝试为每个按钮设置 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);
                }
            }
            

            【讨论】:

              猜你喜欢
              • 2012-09-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多