java - 在我的对话框中按下按钮时未调用 actionPerformed
问题描述
当我单击我为确定和取消添加的按钮时,actionPerformed 不会被调用代码:
void testServerFlags()
{
dlgEmpty mdlgCreateBot = new dlgEmpty(null);
mdlgCreateBot.show();
ted=0;
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import Indicator.cGenIndicator;
import Indicator.cIndicatorUtil;
import MyLib.cLib;
public class dlgEmpty extends JDialog implements ActionListener
{
Box parPan, topPan, butPan,centerPan;;
public dlgEmpty(JFrame parent)
{
super(parent,"Indicators", true);
Container content = getContentPane();
content.setLayout(new BorderLayout());
butPan = Box.createHorizontalBox();
JButton okBut = new JButton("OK");
okBut.setActionCommand("ok");
butPan.add(okBut, BorderLayout.SOUTH);
JButton CancelBut = new JButton("Cancel");
CancelBut.setActionCommand("Cancel");
butPan.add(CancelBut, BorderLayout.SOUTH);
content.add(butPan, BorderLayout.SOUTH);
centerPan = Box.createVerticalBox();
content.add(centerPan, BorderLayout.CENTER);
pack();
show();
}
boolean isOk=false;
public void actionPerformed(ActionEvent e)
{
if ("OK".equals(e.getActionCommand()))
isOk=true;
setVisible(false);
}
}
解决方案
您正在将操作命令设置为:
okBut.setActionCommand("ok");
但正在检查:
if ("OK".equals(e.getActionCommand()))
这不匹配,因为情况不同。
推荐阅读
- c# - 值未绑定到选择菜单中的选定选项
- pdf - 使用 Latex 嵌入 pdf 的视频无法正确播放
- web-worker - 无法从网络工作者读取/写入文件
- javascript - JQuery 链接 onClick 事件发送图像 - 如何获取链接对象?
- android - Kotlin 中的 edittext.setText("str") 和 edittext.text.text="str" 有什么区别?
- c# - 如何使用 Mediator 配置 MassTransit 以发布消息?
- android - Android Gradle“Api”不适用于外部库
- javascript - GRT 修复了响应式菜单 - jQuery 插件 - 关闭菜单的问题(点击链接后)
- google-apps-script - 获取表索引
- javascript - ReactJS 在生产中没有缩小错误