繁体   English   中英

Java中的简单GUI问题

[英]Simple GUI question in java

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

所以我有3个按钮:button1 = gmail button2 = google button3 = yahoo

当我单击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) {}


}

谢谢

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

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

如果您的actionPerformed方法没有什么不同,那么按下的按钮就是正确的行为。

解决此问题的方法有很多种...为每个按钮使用ACtionListener(例如匿名)

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

例如:

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

您无法在actionPerformed事件中标识每个按钮。

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

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

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

简而言之,它同时运行着这三个。

如果要执行三种不同的操作,则需要三个不同的动作侦听器(每个按钮一个),每个动作侦听器或一个动作侦听器(对于所有按钮一个)试图确定按下哪个按钮并执行基于哪个按钮调用它的东西。

您现在拥有的是一个动作监听器,它可以完成所有三件事,而与按下哪个按钮无关。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM