0

我最近学会了如何在JpanelsCardLayout之间切换。该代码工作正常,但是当我尝试将其中一个更改为时jPanelPlot2DPanel我在运行时会出现奇怪的行为。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);
    }

}
4

0 回答 0