java - Java中的简单GUI问题

标签 java user-interface awt

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


}

谢谢

最佳答案

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

关于java - Java中的简单GUI问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5331359/

相关文章:

java - 无法在 Fedora 25 上启动 SQL Developer

user-interface - 如何在 Linux 上记录 GUI 单击的底层 CLI 操作?

sql - SSRS设计器中的数据集面板(报告数据)消失了

java - 在 Swing 中使用 KeyListener 的 3 个方法时感到困惑吗?

java - 结合 guava eventbus 和 AWT 事件线程处理的最佳方式

java - 在 Java 中同步共享静态对象的正确方法是什么?

java - 如何为oracle xe创建dsn?

java - 根据配置文件在 Spring 中加载属性文件

android - NinePatch Images 的主要来源?

java - 居中整个窗口Java