JAVA窗口事件问题。。。。。。。。

import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

import javax.swing.JFrame;

public class Demo extends JFrame {

public Demo() {
    addWindowFocusListener(new MyWindowStateListener());
    setSize(200, 200);// 设置大小
    setDefaultCloseOperation(EXIT_ON_CLOSE);// 关闭窗口退出程序
    setVisible(true);// 窗体可以显示
}

private class MyWindowStateListener implements WindowStateListener {
    public void windowStateChanged(WindowEvent e) {
        int oldState = e.getOldState();// 获得窗体以前的状态
        int newState = e.getNewState();// 获得窗体现在的状态
        String from = "";// 标识窗体以前状态的中文字符串
        String to = "";// 标识窗体现在状态的中文字符串
        switch (oldState) {// 判断窗台以前的状态
        case JFrame.NORMAL:// 窗体处于正常化
            from = "正常化";
            break;
        case JFrame.MAXIMIZED_BOTH:// 窗体处于最大化
            from = "最大化";
            break;
        default:// 窗体处于最小化
            from = "最小化";
        }
        switch (newState) {// 判断窗台现在的状态
        case JFrame.NORMAL:// 窗体处于正常化
            to = "正常化";
            break;
        case JFrame.MAXIMIZED_BOTH:// 窗体处于最大化
            to = "最大化";
            break;
        default:// 窗体处于最小化
            to = "最小化";
        }
        System.out.println(from + "——>" + to);
    }
}

public static void main(String[] args) {
    new Demo();
}

}
这里 addWindowFocusListener报错,怎么办?

4个回答

f.addWindowListener(参数类型错误);MyListener继承了你自己定义的WindowAdapter,但是要求的参数是WindowListener,java.awt.event.WindowAdapter才继承了WindowListener。
https://zhidao.baidu.com/question/574848562.html

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KeyEventDemo extends JFrame{
private KeyboardPanel keyboardPanel = new KeyboardPanel();

    public KeyEventDemo(){
          add( keyboardPanel);

           keyboardPanel.setFocusable(true);

   }

    public static void main(String[] args){
          KeyEventDemo frame = new KeyEventDemo();
          frame.setTitle( "KeyEventDemo");
          frame.setSize(300,300);
          frame.setLocationRelativeTo( null);
          frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
          frame.setVisible( true);

   }

    static class KeyboardPanel extends JPanel{
           private int x = 100;
           private int y = 100;
           private char KeyChar = 'A' ;

           public KeyboardPanel(){
                 addKeyListener( new KeyAdapter(){
                        public void keyPressed(KeyEvent e){
                               switch(e.getKeyCode()){
                               case KeyEvent.VK_DOWN: y += 10;break;
                               case KeyEvent.VK_UP: y -= 10; break;
                               case keyEvent.VK_LEFT: x -= 10; break;
                               case KeyEvent.VK_RIGHT: x += 10; break;
                               default: keyChar = e.getKeyChar();
                              }

                              repaint();
                       }
                 });
          }

           protected void paintComponent(Graphics g){
                  super.paintComponent(g);

                 g.setFont( new Font("TimesRoman" ,Font.PLAIN,24));
                 g.drawString(String. valueOf(KeyChar), x, y);
          }
   }

}

少了
import java.awt.event.WindowFocusListener;

你addWindowFocusListener,传参却传入WindowStateListener 的实现类,你的Demo 表示这不是我的菜,我不喜欢。
自己按照需求改吧

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问