开发者

Simple GUI question in java

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) {}
    p开发者_开发知识库ublic 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


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


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


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

Every time the event is executed all three commands are executed


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.


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.


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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜