【问题标题】:problem with calling the method after pressing the key按键后调用方法的问题
【发布时间】:2020-03-25 19:50:36
【问题描述】:

我想实现 KeyListiner 以便在按下空格键时程序会生成 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);

    }
}

画板类:

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();
    }
}

【问题讨论】:

  • 1.在 JPanel 的paintComponent 方法中绘制。 2. 不要混合使用 Swing 和 AWT 组件(换句话说,摆脱 Canvas 并使用 JPanel)。 3.不要在控制器中创建新的组件,而是在绘图组件中绘制新的非组件的东西。
  • 不知道为什么要使用Canvas 来完成如此简单的事情,看起来很像矫枉过正,几乎没有好处(可能还有一些缺点)。您确实应该使用Key bindings API 而不是KeyListener,但是,作为一般规则,您需要实际注册事件

标签: java swing canvas keylistener


【解决方案1】:

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

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

所以你的条件是:e.getKeyCode() == KeyEvent.VK_SPACE

题外话,但是:

  1. 您没有必要在多个类中使用Function。它只能用于DrawBoard
  2. 不要像其他已经建议的那样混合 Swing 和 AWT 组件

【讨论】:

    猜你喜欢
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多