为什么Plot2DPanel没有对其MouseListener的点击事件做出反应?

时间:2016-05-07 20:31:49

标签: java swing jmathplot

我最近学到了how to switch between Jpanels in CardLayout。 代码工作正常,但是当我尝试将jPanel中的一个更改为Plot2DPanel时,我在运行时会遇到奇怪的行为。 Plot2DPanel没有获取鼠标点击事件。我做错了什么? (我认为这不是jmathplot中的错误。)

以下是代码:

public class Window {

    private JFrame frame;
    private JPanel cards;
    private JPanel panelOne;
    private Plot2DPanel panelTwo;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window window = new Window();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Window() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 790, 483);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        cards = new JPanel();
        cards.setLayout(new CardLayout());

        panelOne = new JPanel();
        panelOne.setBackground(Color.BLACK);
        panelOne.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e){
                java.awt.Toolkit.getDefaultToolkit().beep(); //debug beep
                CardLayout cl = (CardLayout) cards.getLayout();
                cl.next(cards);
            }
        });

        panelTwo = new Plot2DPanel();
        panelTwo.setBackground(Color.WHITE);
        panelTwo.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e){
                java.awt.Toolkit.getDefaultToolkit().beep(); //debug beep
                CardLayout cl = (CardLayout) cards.getLayout();
                cl.next(cards);
            }
        });

        cards.add(panelOne, "panel1");
        cards.add(panelTwo, "panel2");

        frame.getContentPane().add(cards);
    }

}

0 个答案:

没有答案