java中的简单GUI问题

发布于 2024-10-22 01:59:48 字数 2110 浏览 6 评论 0原文

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

所以我有 3 个按钮: 按钮1=gmail 按钮2 = 谷歌 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) {}


}

谢谢

i'm trying a very simple GUI in java.
i've just created a small GUI with buttons and when we click each button, it opens a website.

So i have have 3 buttons:
button1 = gmail
button2 = google
button3 = yahoo

when i click on button1 sometimes it opens gmail or google or yahoo.
The same problem with other button too.

Why?

Here is my very simple code:

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


}

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

英雄似剑 2024-10-29 01:59:48

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

Your actionPerformed is running all three. You need to use actionPerformed to determine which button was pressed, and then run the corresponding command.

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);
    }
}
仅此而已 2024-10-29 01:59:48

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

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

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

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

例如:

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

You add the same ActionListener to all three buttons. And in the ACtionListener you open yahoo google and gmail. So what else did you expect?

If you don't differ in your actionPerformed method which button was pressed, then this is the correct behaviour.

There are various possibilities to solve this issue... use a ACtionListener for each button (for example anonymoous)

Or use e.getSource() to determine which button was pressed in the actionPerformed method.

For example:

if(e.getSource().equals(a)) {
   address = "https://mail.google.com";
}
奈何桥上唱咆哮 2024-10-29 01:59:48

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

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

You don't identify each button in your actionPerformed event.

Every time the event is executed all three commands are executed

安人多梦 2024-10-29 01:59:48

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

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

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

One consider naming your a to gmail so it's more descriptive.

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

But in short it's running all three.

绳情 2024-10-29 01:59:48

如果你想做三件不同的事情,你要么需要三个不同的动作监听器(每个按钮一个),每个监听器做一件事,要么需要一个动作监听器(一个用于所有按钮),它尝试确定哪个按钮被按下并执行基于哪个按钮调用它的东西。

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

If you want to do three different things, you either need three different action listeners (one for each button) which each do one thing, or one action listener (one for all buttons) which makes an attempt to determine which button was pressed and does something based on which button called it.

What you have right now is one action listener that does all three things without regard as to which button was pressed.

执手闯天涯 2024-10-29 01:59:48

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

You could also try setting the ActionCommand for each button so that they can all use the same event listener. This would also improve maintainability if/when you want to add a new button.

    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" );

and reimplement your listener method as such:

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文