Java KeyListener不起作用



我用JFreeChart尝试了简单的代码,当我按下"缩放"按钮时,我想用键盘上的"escape"取消缩放,但KeyListener不起作用。问题出在哪里?我还尝试将KeyListener添加到chartPanelpanel中,但也不起作用。

package test1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.data.xy.*;
public class Test1 {
    public static final String Title = "JGrapher";
    static JFrame frame;
    static double minX = -0.1, maxX = 1.1, minY, maxY;
    static XYSeriesCollection dataset;
    static ChartPanel chartPanel;
    static XYPlot xyplot; 
    private static void display(){  
        XYSeries serie = new XYSeries("1");
        serie.add(1, 1);
        serie.add(8, 8);
        dataset = new XYSeriesCollection();
        dataset.addSeries(serie);
        NumberAxis domain = new NumberAxis("u03C1");
        NumberAxis range = new NumberAxis("g(u03C1)");
        XYSplineRenderer r = new XYSplineRenderer(20);
        xyplot = new XYPlot(dataset, domain, range, r);
        final JFreeChart chart = new JFreeChart(xyplot);
        ValueAxis xAxis = xyplot.getDomainAxis();
        xAxis.setRange(-10, 10);
        ValueAxis yAxis = xyplot.getRangeAxis();
        yAxis.setRange(-10, 10);
        chartPanel = new ChartPanel(chart){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        chartPanel.setDomainZoomable(false);
        chartPanel.setRangeZoomable(false);
        frame = new JFrame(Title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(chartPanel);
        frame.addKeyListener(new KeyListener() { // KEYLISTENER NOW WORKS :(
            @Override
            public void keyTyped(KeyEvent ke) {}
            @Override
            public void keyPressed(KeyEvent ke) {
                System.out.println("You pressed " + ke.getKeyChar());
            }
            @Override
            public void keyReleased(KeyEvent ke) {}
        });
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER)); //buttons
        final JButton zoomFree = new JButton("Zoom");
        panel.add(zoomFree);
        zoomFree.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                chartPanel.setDomainZoomable(true);
                chartPanel.setRangeZoomable(true);
                zoomFree.setEnabled(false);
            }
        });
        frame.add(panel, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                display();
            }
        });
    }
}

是的,我尝试了简单的系统输出来检测keylistener 的工作情况

我想用键盘上的"escape"取消缩放,

然后使用密钥绑定,而不是KeyListener。Swing设计用于键绑定。

有关详细信息,请阅读Swing教程中关于如何使用密钥绑定的部分。

相关内容

  • 没有找到相关文章

最新更新