0

我收到错误消息:

JOptionPane 类型中的方法 showInternalConfirmDialog(Component, Object, String, int, int) 不适用于参数(new KeyListener(){}, String, String, int, int)

        public void keyTyped(KeyEvent e) {

        }

        public void keyReleased(KeyEvent e) {
            System.out.println("Key Released F1");
            if(e.getKeyCode()==KeyEvent.VK_F1) {



    /*here i get the error*/    int choice = JOptionPane.showInternalConfirmDialog(this, "Would you like to play the movie? " 
                        ,"Message", JOptionPane.WARNING_MESSAGE
                        , JOptionPane.OK_CANCEL_OPTION);

知道为什么我会收到此错误吗?

4

1 回答 1

0

假设您扩展了 JFrame,JFrame 不会扩展 JComponent。我认为这就是它不起作用的原因。作为一种解决方法,我建议使用 JPanel,此代码已编译并有效。

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class CodeTester extends JFrame implements KeyListener
{
    JPanel panel = new JPanel();

    public static void main(String[] args)
    {
        JFrame frame = new CodeTester();
        frame.setSize(400, 400);
        frame.setVisible(true);    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public CodeTester()
    {
        addKeyListener(this);
        getContentPane().add(panel);        
    }

    public void keyTyped(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {}

    public void keyReleased(KeyEvent e) 
    {
        int choice = JOptionPane.showInternalConfirmDialog(panel, 
                    "Would you like to play the movie? " 
                       ,"Message", JOptionPane.WARNING_MESSAGE
                    , JOptionPane.OK_CANCEL_OPTION);                

    }
}

希望这可以帮助。

编辑我刚刚注意到我在没有实现“F1”按钮条件的情况下编码,我只是为了简化而摆脱了。

于 2012-12-18T16:18:40.400 回答