首页 > 解决方案 > 在我的对话框中按下按钮时未调用 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);
            }  
}

标签: java

解决方案


您正在将操作命令设置为:

okBut.setActionCommand("ok"); 

但正在检查:

if ("OK".equals(e.getActionCommand()))

这不匹配,因为情况不同。


推荐阅读