gpt4 book ai didi

java - 按钮的 ActionListener

转载 作者:行者123 更新时间:2023-12-04 20:48:57 26 4
gpt4 key购买 nike

因此,我为我的一个类开发了一个程序,以便在单击按钮时使用 ActionListener 处理事件。我的程序可以正常工作,但是当单击任何按钮时,您必须多次单击输入按钮才能注册答案并移至程序的下一级。

示例 1:警报按钮打开 2 有时 3 “嘿,你身上有问题!”消息对话框。

示例 2:是/否提示您 3-4 次将您的是/否答案返回到 txt。

示例 3:在将输入返回到 txt 之前,颜色需要 3/4 次点击。

(我想你明白了……)

我这辈子都搞不懂为什么它不接受一个输入然后继续...

我的程序代码供您审阅...在此先感谢您。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class MessageBoxes{

private JButton alert = new JButton("Alert");
private JButton yesNo = new JButton("Yes/No");
private JButton color = new JButton("Color");
private JButton vals = new JButton("3 Vals");
private JButton input = new JButton("Input");

private JTextField txt = new JTextField(15);

private JFrame frame;

private ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent e){

alert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
JOptionPane.showMessageDialog(null,
"There's a bug on you!", "Hey!",
JOptionPane.ERROR_MESSAGE);
}
});

yesNo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
int val = JOptionPane.showConfirmDialog(null,
"Choose yes or no", "Your Call...",
JOptionPane.YES_NO_OPTION);

if(val != JOptionPane.CLOSED_OPTION){
if(val == 0){
txt.setText("Yes");
}
else{
txt.setText("No");
}
}
}
});

color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
Object[] options = {"Red", "Green"};

int sel = JOptionPane.showOptionDialog(null,
"Choose a Color!", "Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null,
options, options[0]);

if(sel != JOptionPane.CLOSED_OPTION){
txt.setText("Color Selected: " + options[sel]);
}
}
});

vals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
Object[] selections = {"First", "Second", "Third"};

Object val = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE,
null, selections, selections[0]);

if(val != null){
txt.setText(val.toString());
}
}
});

input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
String val = JOptionPane.showInputDialog("How mant fingers do you see?");

txt.setText(val);
}
});
}
};



public MessageBoxes(){

frame = new JFrame("Title");

frame.setSize(250, 250);

frame.getContentPane().setLayout(new FlowLayout());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(alert);
frame.getContentPane().add(yesNo);
frame.getContentPane().add(color);
frame.getContentPane().add(vals);
frame.getContentPane().add(input);
frame.getContentPane().add(txt);

frame.setVisible(true);

}

public void launchJFrame(){

alert.addActionListener(al);
yesNo.addActionListener(al);
color.addActionListener(al);
vals.addActionListener(al);
input.addActionListener(al);

}

public static void main(String [] args){

MessageBoxes messageBoxes = new MessageBoxes();
messageBoxes.launchJFrame();
}
}

最佳答案

问题是,每次在 ActionListener al 中调用 actionPerformed 时,它都会注册新的 ActionListener s 用你的按钮

private ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {

alert.addActionListener(new ActionListener() {
//...
});

yesNo.addActionListener(new ActionListener() {
//...
});

color.addActionListener(new ActionListener() {
//...
});

vals.addActionListener(new ActionListener() {
//...
});

input.addActionListener(new ActionListener() {
//...
});
}
};

因此,每次调用 actionPerformed 方法时,每个按钮都会注册一个新的 actionListener

相反,您可以使用 if-else 语句来确定事件的来源,例如...

Object source = e.getSource();
if (source == alert) {
JOptionPane.showMessageDialog(null,
"There's a bug on you!", "Hey!",
JOptionPane.ERROR_MESSAGE);
} else if (...

或者您可以完全摆脱 ActionListener al 并简单地将各个 ActionListener 注册到 launchJFrame 中的按钮 方法...

public void launchJFrame() {

alert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
}
});

yesNo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
int val = JOptionPane.showConfirmDialog(null,
"Choose yes or no", "Your Call...",
JOptionPane.YES_NO_OPTION);

if (val != JOptionPane.CLOSED_OPTION) {
if (val == 0) {
txt.setText("Yes");
} else {
txt.setText("No");
}
}
}
});

color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
Object[] options = {"Red", "Green"};

int sel = JOptionPane.showOptionDialog(null,
"Choose a Color!", "Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null,
options, options[0]);

if (sel != JOptionPane.CLOSED_OPTION) {
txt.setText("Color Selected: " + options[sel]);
}
}
});

vals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
Object[] selections = {"First", "Second", "Third"};

Object val = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE,
null, selections, selections[0]);

if (val != null) {
txt.setText(val.toString());
}
}
});

input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
String val = JOptionPane.showInputDialog("How mant fingers do you see?");

txt.setText(val);
}
});

}

关于java - 按钮的 ActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25355587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com