gpt4 book ai didi

Java ActionListener 将变量返回到包含 ActionListener 的方法

转载 作者:行者123 更新时间:2023-11-29 03:07:54 27 4
gpt4 key购买 nike

我有一个这样的方法,它被赋予一个 JButton 数组,并在它们被按下时返回它们的文本:

public static String foo(JButton[] buttons) {
for (JButton i : buttons) {
i.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
return i.getText();
}
});
}
}

但是,当然,这段代码不会编译,因为我将一个变量返回给一个 null 方法。那么,我如何让 i.getText() 也通过 foo() 方法返回其输出?


编辑,所有代码:

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JCustomFrame {
public static void showMessageFrame(String title, String message,
String[] textOnButtons, ImageIcon icon) {
final JFrame frame = new JFrame();
JPanel panel = new JPanel();

panel.setLayout(new GridBagLayout());
panel.setBackground(Color.WHITE);

GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.RELATIVE;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(5, 5, 5, 5);

JLabel messageLabel = new JLabel(message);
messageLabel.setFont(messageLabel.getFont().deriveFont(16.0f));

panel.add(messageLabel, c);

c.gridy = 1;
c.gridx = 0;
for (int i = 0; i < textOnButtons.length; i++) {
JButton button = new JButton(textOnButtons[i]);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
return ((JButton) arg0.getSource()).getText();
frame.dispose();
}
});
button.setFont(button.getFont().deriveFont(16.0f));
panel.add(button, c);
c.gridx++;
}

if (icon == null) {
frame.setIconImage(new BufferedImage(1, 1,
BufferedImage.TYPE_INT_ARGB_PRE));
} else {
frame.setIconImage(icon.getImage());
}
frame.add(panel);
frame.setTitle(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}

public static void main(String[] args) {
JCustomFrame.showMessageFrame("Test Frame",
"Do you really want to do this?", new String[] { "Hell No",
"Sure, Why Not" }, null);
}
}

最佳答案

这个说法没有意义:

So, how would I have i.getText() return its output too the foo() method?

foo() 方法在 ActionListeners 被添加到按钮后不再运行,并且肯定会在用户按下按钮时结束,根据事件规则-驱动编程。相反,尽管您可以让 ActionListeners 更改类的状态,但任何类都应该足够了。例如:

class FooClass {
private String text;

public void foo(JButton[] buttons) {
for (JButton i : buttons) {
i.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
text = e.getActionCommand();
}
});
}
}
}

如果您需要有关可行解决方案的更多详细信息,请告诉我们有关您的实际程序和具体问题的更多详细信息。

现在如果你真的需要一个方法来返回按下按钮的值,你需要通过通知机制和回调方法来实现,但是解决方案的细节将取决于实际的细节问题和代码。


编辑

您正在尝试模拟 JOptionPane。您的解决方案是使用 JOptionPane,向其添加 JPanel,或者使用模态 JDialog 创建您自己的 JDialog:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JCustomFrame2 {

public static String showMessageFrame(Window owner, String title,
String message, String[] textOnButtons, ImageIcon icon) {
final JDialog dialog = new JDialog(owner);
StringBuilder sb = new StringBuilder();

// make it application modal!
dialog.setModalityType(ModalityType.APPLICATION_MODAL);
JPanel panel = new JPanel();

panel.setLayout(new GridBagLayout());
panel.setBackground(Color.WHITE);

GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.RELATIVE;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(5, 5, 5, 5);

JLabel messageLabel = new JLabel(message);
messageLabel.setFont(messageLabel.getFont().deriveFont(16.0f));

panel.add(messageLabel, c);

c.gridy = 1;
c.gridx = 0;
for (int i = 0; i < textOnButtons.length; i++) {
JButton button = new JButton(textOnButtons[i]);
button.addActionListener(new ButtonListener(sb));
button.setFont(button.getFont().deriveFont(16.0f));
panel.add(button, c);
c.gridx++;
}

if (icon == null) {
dialog.setIconImage(new BufferedImage(1, 1,
BufferedImage.TYPE_INT_ARGB_PRE));
} else {
dialog.setIconImage(icon.getImage());
}
dialog.add(panel);
dialog.setTitle(title);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.pack();
dialog.setVisible(true);

return sb.toString();
}

private static class ButtonListener implements ActionListener {
private StringBuilder sb;

public ButtonListener(StringBuilder sb) {
this.sb = sb;
}


@Override
public void actionPerformed(ActionEvent e) {
sb.append(e.getActionCommand());
Component component = (Component) e.getSource();
Window win = SwingUtilities.getWindowAncestor(component);
if (win != null) {
win.dispose();
}
}
}

public static String showMessageFrame(String title,
String message, String[] textOnButtons, ImageIcon icon) {
return showMessageFrame(null, title, message, textOnButtons, icon);
}


public static void main(String[] args) {
String result = JCustomFrame2.showMessageFrame("Test Frame",
"Do you really want to do this?", new String[] { "Hell No",
"Sure, Why Not" }, null);

System.out.println(result);
}
}

关于Java ActionListener 将变量返回到包含 ActionListener 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31126011/

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