首先,我为我的英语道歉,我用谷歌翻译的这篇文章的大部分内容。 至于问题,尝试使用JMenu的CardLayout并尝试显示不同的视图,我跳转错误。 我在谷歌搜索并查看同一页面,我发现了类似的东西,但是应用问题仍然存在。 以下是我所做的代码:
public class Principal extends JFrame implements ActionListener {
//variables menu
private final CardLayout cardLayout = new CardLayout();
private final JPanel panelSuperior = new JPanel(), panelInferior = new JPanel(), panelPrincipal = new JPanel(),
panelVentas = new JPanel();
private final JMenuBar barMenu = new JMenuBar();
private final JMenu menuPrincipal = new JMenu(), menuProductos = new JMenu();
private final JMenuItem menuVenta = new JMenuItem(), menuProdAgregar = new JMenuItem(), menuProdBuscar = new JMenuItem(),
menuProdActualizar = new JMenuItem(), menuFaltante = new JMenuItem(), menuSalir = new JMenuItem();
//variables de ventanas
private JLabel tituloVentana;
public Principal(){
initWinPanelSuperior();
initWinPrincipal();
initWinPanelVenta();
initWinPanelInferior();
initWindow();
}
private void initWinPanelSuperior(){
setJMenuBar(barMenu);
menuPrincipal.setText("Menu");
menuVenta.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
menuVenta.setText("Ventas");
menuVenta.addActionListener(this);
menuPrincipal.add(menuVenta);
menuProductos.setText("Productos");
menuProdAgregar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
menuProdAgregar.setText("Agregar");
menuProdAgregar.addActionListener(this);
menuProductos.add(menuProdAgregar);
menuProdActualizar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
menuProdActualizar.setText("Actualizar");
menuProdActualizar.addActionListener(this);
menuProductos.add(menuProdActualizar);
menuProdBuscar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
menuProdBuscar.setText("Buscar");
menuProdBuscar.addActionListener(this);
menuProductos.add(menuProdBuscar);
menuPrincipal.add(menuProductos);
menuFaltante.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F6, 0));
menuFaltante.setText("Mostrar faltante");
menuFaltante.addActionListener(this);
menuPrincipal.add(menuFaltante);
menuSalir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, 0));
menuSalir.setText("Salir");
menuSalir.addActionListener(this);
menuPrincipal.add(menuSalir);
barMenu.add(menuPrincipal);
barMenu.add(Box.createHorizontalGlue());
}
private void initWinPrincipal(){
JLabel titulo = new JLabel();
titulo.setText("Bienvenidos");
titulo.setBounds(0, 0, 300, 200);
titulo.setFont(new Font("SansSerif", 0, 42));
titulo.setVisible(true);
panelPrincipal.add(titulo);
}
private void initWinPanelVenta(){
tituloVentana = new JLabel();
tituloVentana.setBounds(200, 0, 180, 30);
tituloVentana.setFont(new java.awt.Font("SansSerif", 0, 38));
tituloVentana.setHorizontalTextPosition(JLabel.CENTER);
tituloVentana.setVerticalTextPosition(JLabel.CENTER);
tituloVentana.setText("Ventas");
tituloVentana.setVisible(true);
panelVentas.add(tituloVentana);
}
private void initWinPanelInferior(){
panelInferior.setLayout(cardLayout);
panelInferior.add(panelPrincipal, "principal");
panelInferior.add(panelVentas, "ventas");
}
private void initWindow(){
JPanel panel = (JPanel) this.getContentPane();
this.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
this.add(panelSuperior);
this.add(panelInferior);
this.pack();
this.getPreferredSize();
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setTitle("Sys");
this.show();
}
@Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == menuVenta) {
//problem here
CardLayout cl = (CardLayout) panelInferior.getLayout();
cl.show(panelVentas, "ventas");
}
if (e.getSource() == menuSalir) {
System.exit(0);
}
}
这里是错误的痕迹:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout
at java.awt.CardLayout.checkLayout(CardLayout.java:404)
at java.awt.CardLayout.show(CardLayout.java:526)
at Vista.Principal.actionPerformed(Principal.java:121)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:1)
CardLayout#show(Container, String)
将布局对象的实际容器作为第一个参数。第二个参数是您给卡组件的名称,在这种情况下它是ventas
,如下所示。
panelInferior.add(panelVentas, "ventas");
所以:
layout
的容器。Card
组件的名称。cl.show(panelVentas, "ventas");
实际应该是
cl.show(panelInferior, "ventas");