有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在我的对话框中按下按钮时,不会调用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);
            }  
}

共 (2) 个答案

  1. # 1 楼答案

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

    okBut.setActionCommand("ok"); 
    

    但我们正在检查:

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

    这不匹配,因为情况不同

  2. # 2 楼答案

    setActionCommand仅在ActionEvent中设置消息。您需要的是addActionListener。我已经更新了你的代码

    但是你应该知道的另一件事是没有必要这样做。而是使用JOptionPane。以JComponent作为消息参数的showConfirmDialog:它自动执行所有按钮处理和模式在线等待

        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"); 
             okBut.addActionListener(this); // added this line
             butPan.add(okBut, BorderLayout.SOUTH);    
        
             JButton CancelBut = new JButton("Cancel");
             CancelBut.setActionCommand("Cancel");
             CancelBut.addActionListener(this); 
             butPan.add(CancelBut, BorderLayout.SOUTH); //added this line    
             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);
            }  
    }