[英]Simple GUI question in java
我正在嘗試使用Java創建非常簡單的GUI。 我剛剛創建了一個帶有按鈕的小型GUI,當我們單擊每個按鈕時,它將打開一個網站。
所以我有3個按鈕:button1 = gmail button2 = google 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) {}
}
謝謝
您的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);
}
}
您將相同的ActionListener添加到所有三個按鈕。 然后在ACtionListener中打開yahoo google和gmail。 那你還期望什么呢?
如果您的actionPerformed方法沒有什么不同,那么按下的按鈕就是正確的行為。
解決此問題的方法有很多種...為每個按鈕使用ACtionListener(例如匿名)
或使用e.getSource()
確定actionPerformed方法中按下了哪個按鈕。
例如:
if(e.getSource().equals(a)) {
address = "https://mail.google.com";
}
您無法在actionPerformed事件中標識每個按鈕。
每次執行事件時,都會執行所有三個命令
可以考慮將您的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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.