单击“ homeMenu”时,控制台上将显示“选择菜单”和“取消选择菜单”,而不会出现面板。单击“ homeMenu”时应如何使面板出现?
public class MyClass extends JApplet{
JMenuBar menuBar;
JMenu fileMenu, homeMenu;
public MyClass () {
setTheMenuBar();
}
void setTheMenuBar() {
menuBar = new JMenuBar();
setJMenuBar(menuBar);
homeMenu = new JMenu("Home");
menuBar.add(homeMenu);
homeMenu.addMenuListener(new MenuListener() {
@Override
public void menuSelected(MenuEvent e) {
JPanel p = new JPanel(new FlowLayout());
p.add(new JButton("yara"));
add(p, BorderLayout.NORTH);
System.out.println("Menu selected");
}
@Override
public void menuDeselected(MenuEvent e) {
System.out.println("Menu deselected");
}
@Override
public void menuCanceled(MenuEvent e) {
System.out.println("Menu canceled");
}
});
public static void main(String[] args) {
// TODO code application logic here
MyClass applet = new MyClass();
JFrame frame = new JFrame();
frame.add(applet);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
applet.start();
}
}