我最近学到了how to switch between Jpanel
s 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);
}
}