Java ActionListener将变量返回到包含ActionListener的方法
我有一个这样的方法,它被赋予一个Java ActionListener将变量返回到包含ActionListener的方法,java,swing,return,listener,Java,Swing,Return,Listener,我有一个这样的方法,它被赋予一个JButton数组,并在按下它们时返回它们的文本: public static String foo(JButton[] buttons) { for (JButton i : buttons) { i.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {
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);
}
}
导入java.awt.Color;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.image.buffereImage;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
公共类JCustomFrame{
公共静态void showMessageFrame(字符串标题、字符串消息、,
字符串[]文本按钮,图像图标(图标){
最终JFrame=新JFrame();
JPanel面板=新的JPanel();
panel.setLayout(新的GridBagLayout());
面板.立根背景(颜色.白色);
GridBagConstraints c=新的GridBagConstraints();
c、 锚点=GridBagConstraints.EAST;
c、 fill=GridBagConstraints.RELATIVE;
c、 gridx=0;
c、 gridy=0;
c、 插图=新插图(5,5,5,5);
JLabel messageLabel=新的JLabel(消息);
messageLabel.setFont(messageLabel.getFont().deriveFont(16.0f));
panel.add(messageLabel,c);
c、 gridy=1;
c、 gridx=0;
对于(int i=0;i
这句话毫无意义:
那么,如何让I.getText()也通过foo()方法返回其输出呢
方法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创建自己的:
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.awt.Color;
导入java.awt.Component;
导入java.awt.Dialog.ModalityType;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.Window;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.image.buffereImage;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JDialog;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类JCustomFrame2{
公共静态字符串showMessageFrame(窗口所有者、字符串标题、,
字符串消息、字符串[]文本按钮、图像图标){
最终JDialog对话框=新JDialog(所有者);
StringBuilder sb=新的StringBuilder();
//让它成为应用程序模式!
setModalityType(ModalityType.APPLICATION_MODAL);
JPanel面板=新的JPanel();
panel.setLayout(新的GridBagLayout());
面板.立根背景(颜色.白色);
GridBagConstraints c=新的GridBagConstraints();
c、 锚点=GridBagConstraints.EAST;
c、 fill=GridBagConstraints.RELATIVE;
c、 gridx=0;
c、 gridy=0;
c、 插图=新插图(5,5,5,5);
JLabel messageLabel=新的JLabel(消息);
messageLabel.setFont(messageLabel.getFont().deriveFont(16.0f));
panel.add(messageLabel,c);
c、 gridy=1;
c、 gridx=0;
对于(int i=0;ipublic void doSomething(){
JButton[] someButtons = ...;//whereever you create the buttons
System.out.println(foo(someButtons));
}
public static String foo(JButton[] buttons){
final String someString = "";
final Object lock = new Object();
for(JButton b : buttons){
b.addActionListener(e -> {
someString.concat(b.getName());
synchronized(lock){
lock.notifyAll();
}
});
}
synchronized(lock){
try{
lock.wait();
}catch(InterruptedException e){}
}
return someString;
}