java中的简单GUI问题
我正在尝试用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的 actionPerformed 正在运行所有三个。您需要使用actionPerformed来确定按下了哪个按钮,然后运行相应的命令。
Your actionPerformed is running all three. You need to use actionPerformed to determine which button was pressed, and then run the corresponding command.
您将相同的 ActionListener 添加到所有三个按钮。在 ACtionListener 中,您打开 yahoo google 和 gmail。那么你还期待什么呢?
如果您的 actionPerformed 方法中按下的按钮没有不同,那么这是正确的行为。
有多种可能性可以解决此问题...为每个按钮使用 ACtionListener(例如匿名)
或使用
e.getSource()
来确定在 actionPerformed 方法中按下了哪个按钮。例如:
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:
您无需识别 actionPerformed 事件中的每个按钮。
每次执行该事件时,都会执行所有三个命令
You don't identify each button in your actionPerformed event.
Every time the event is executed all three commands are executed
您可以考虑将您的 a 命名为 gmail,这样更具描述性。
但简而言之,它正在运行所有三个。
One consider naming your a to gmail so it's more descriptive.
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.
您还可以尝试为每个按钮设置 ActionCommand,以便它们都可以使用相同的事件侦听器。如果/当您想添加新按钮时,这也将提高可维护性。
并重新实现您的侦听器方法:
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.
and reimplement your listener method as such: