我想实现KeyListiner,以便在按下Space键时,程序会生成Function类的新对象并绘制表示该函数的新图像。目前程序正在运行,但是按空格键不允许生成新图像。每个建议将不胜感激:)

主班:

public class Main  {

    public static void main(String[] args) {
        JFrame window = new JFrame(SOFTWARE_TITLE);
        MainView mainView = new MainView();
        Key key = new Key();
        Controller controller = new Controller(mainView);
        key.setController(controller);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(790, 580);
        window.setLocationRelativeTo(null);
        window.setResizable(false);
        window.add(mainView);
        window.setVisible(true);

    }
}


MainView类:

public class MainView extends JPanel {

    private Function function;

    public MainView(){
        super();
        setBackground(Color.GRAY);
        setLayout(null);
        setSize(200, 200);
        function = new Function();

        DrawBoard drawBoard = new DrawBoard(function);
        drawBoard.setSize(500, 500);
        drawBoard.setBackground(Color.lightGray);
        drawBoard.setLocation(250, 20);
        add(drawBoard);

        setVisible(true);

    }
}


DrawBoard类:

public class DrawBoard extends Canvas {

    short CUSTOM_WIDTH = 5 * 100;
    private Function function;

    public DrawBoard(Function function) {
        this.function = function;
    }

    public void paint(Graphics g) {

        double difference = function.getzMax() - function.getzMin();

        for (int indexX = 0; indexX < 100; indexX++) {
            for (int indexY = 0; indexY < 100; indexY++) {
                double value = function.getPoints()[indexX][indexY].getZ() - function.getzMin();
                double corrected = setFloatInRGBRange(difference, value);
                g.setColor(intToColor((int) corrected));
                g.fillRect(indexX * 5, CUSTOM_WIDTH - ((indexY+1) * 5), 5, 5);
            }
        }
    }

    public Color intToColor(int colNum){
        return new Color(colNum, colNum, colNum);
    }

    private int setFloatInRGBRange(double difference, double value){
        return (int) ((255 * value)/difference);
    }

}


关键类别:

public class Key implements KeyListener {

    Controller controller;

    public void setController(Controller controller) {
        this.controller = controller;
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyChar() == KeyEvent.VK_SPACE){
            controller.generateFormula();
        }
    }
}


控制器类:

public class Controller {

    MainView mainView;

    public Controller(MainView mainView) {
        this.mainView = mainView;
    }

    public void generateFormula() {
        this.mainView = new MainView();
    }
}

最佳答案

您发布的代码中还需要检查其他一些内容。但是,对于您主要关心的问题,我想您不能正确检查按键。

我将使用e.getKeyCode()而不是e.getKeyChar()来检查事件。

因此您的条件将是:e.getKeyCode() == KeyEvent.VK_SPACE

它不合主题,但是:


不需要在多个类中使用Function。它只能在DrawBoard中使用
不要像其他已经建议的那样混合使用Swing和AWT组件

09-26 19:25