【发布时间】: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