java中的简单GUI问题
我正在用java尝试一个非常简单的GUI。 我刚刚创建了一个带有按钮的小型GUI,当我们单击每个按钮时,它会打开一个网站 所以我有3个按钮: button1=gmail 按钮2=谷歌 按钮3=雅虎 当我点击按钮1时,它会打开gmail、谷歌或雅虎。 其他按钮也有同样的问题 为什么? 下面是我非常简单的代码:java中的简单GUI问题,java,user-interface,awt,Java,User Interface,Awt,我正在用java尝试一个非常简单的GUI。 我刚刚创建了一个带有按钮的小型GUI,当我们单击每个按钮时,它会打开一个网站 所以我有3个按钮: button1=gmail 按钮2=谷歌 按钮3=雅虎 当我点击按钮1时,它会打开gmail、谷歌或雅虎。 其他按钮也有同样的问题 为什么? 下面是我非常简单的代码: import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.logging.Level; i
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来确定按下了哪个按钮,然后运行相应的命令
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);
}
}
您执行的操作正在运行所有三个。您需要使用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中,你可以打开雅虎、谷歌和gmail。那你还指望什么 如果您的actionPerformed方法与按下的按钮相同,则这是正确的行为 解决这个问题有多种可能性。。。为每个按钮使用ACtionListener(例如Anonymous) 或者使用
e.getSource()
确定actionPerformed方法中按下了哪个按钮
例如:
if(e.getSource().equals(a)) {
address = "https://mail.google.com";
}
将相同的ActionListener添加到所有三个按钮。在ACtionListener中,你可以打开雅虎、谷歌和gmail。那你还指望什么 如果您的actionPerformed方法与按下的按钮相同,则这是正确的行为 解决这个问题有多种可能性。。。为每个按钮使用ACtionListener(例如Anonymous) 或者使用
e.getSource()
确定actionPerformed方法中按下了哪个按钮
例如:
if(e.getSource().equals(a)) {
address = "https://mail.google.com";
}
您没有识别actionPerformed事件中的每个按钮
每次执行事件时,所有三个命令都会执行您不会识别actionPerformed事件中的每个按钮
每次执行该事件时,所有三个命令都执行
< P> >考虑将A命名为Gmail,因此更具描述性。 a.addActionListener(new ActionListener() {
void actionPerformed(ActionEvent e) {
p1 = Runtime.getRuntime().exec("cmd /c start https://mail.google.com");
}
});
P< >总之,它运行三个。 < P> >考虑将你的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);
}
}
您还可以尝试为每个按钮设置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);
}
}
你有没有试过用调试器运行这个程序,看看会发生什么?@Babak,查看actionPerformed事件,你就会意识到什么是错误的。请参阅Desktop.browse(URL)。打开浏览器要比处理进程容易得多。您是否尝试过使用调试器运行此操作并查看发生了什么?@Babak,查看actionPerformed事件,您会意识到错误所在。另请参阅Desktop.browse(URL)。打开浏览器要比处理进程容易得多。