← Foro de Java (básico)

JMenuBar y JToolbar

Hilo en el foro de Java (básico) · 18 de Octubre del 2005 · 2 respuestas
me18 de Octubre del 2005
Pregunta
Hola.

Me gustaría que alguien me ayudase a saber como poner en una ventana una JMenuBar y en otra linea distinta la JToolbar. Yo he conseguido ponerlo todo en la misma linea pero no consigo ponerlas en una linea distinta.

Un saludo

Respuestas 2

miguelAngel18 de Octubre del 2005
Depende del gestor de diseño que estes empleando, de todas formas prueba esto:

JmenuBar barra;

// Se inicializa y se añaden los menus o item
// necesarios

JToolBar barraTareas;

// Se inicializa y se añaden componentes

Si estas dentro de un JFrame puede utilizar

setJMenuBar(barra);

y luego añades la barra tareas

espero que te sirva

saludos
mungassey18 de Octubre del 2005
a ver si te sirve eso:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JMenuToolBarDemo extends JFrame {
private JPanel ivjJFrameContentPane = null;
private JMenuBar ivjJMenuToolBarDemoJMenuBar = null;
IvjEventHandler ivjEventHandler = new IvjEventHandler();

// sub clase que se encarga de monitorear los eventos del menu, toolbar y ventana
class IvjEventHandler implements ActionListener, ComponentListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == JMenuToolBarDemo.this.getmnuItemSalir())
connEtoC2();
if (e.getSource() == JMenuToolBarDemo.this.gettoolBarBoton())
connEtoC3();
};
public void componentHidden(ComponentEvent e) {};
public void componentMoved(ComponentEvent e) {};
public void componentResized(ComponentEvent e) {
if (e.getSource() == JMenuToolBarDemo.this)
connEtoC1(e);
};
public void componentShown(ComponentEvent e) {};
};

// componentes java swing
private JMenuItem ivjmnuItemSalir = null;
private JMenu ivjmnuMenuBar = null;
private JToolBar ivjtoolBar = null;
private JButton ivjtoolBarBoton = null;

// constructor
public JMenuToolBarDemo() {
super();
initialize();
}

// evento conectado a la ventana
private void connEtoC1(ComponentEvent arg1) {
try {
this.jMenuToolBarDemo_ComponentResized(arg1);
} catch (Throwable ivjExc) {
handleException(ivjExc);
}
}

// evento conectado al menu
private void connEtoC2() {
try {
this.jMenuItem1_ActionEvents();
} catch (Throwable ivjExc) {
handleException(ivjExc);
}
}

// evento conectado al boton
private void connEtoC3() {
try {
this.jToolBarButton1_ActionEvents();
} catch (Throwable ivjExc) {
handleException(ivjExc);
}
}

// construccion del gui
private JPanel getJFrameContentPane() {
if (ivjJFrameContentPane == null) {
try {
ivjJFrameContentPane = new JPanel();
ivjJFrameContentPane.setName("JFrameContentPane");
ivjJFrameContentPane.setLayout(null);
ivjJFrameContentPane.setBackground(Color.white);
getJFrameContentPane().add(gettoolBar(), gettoolBar().getName());
} catch (Throwable ivjExc) {
handleException(ivjExc);
}
}

return ivjJFrameContentPane;
}

// construccion del JToolBar
private JMenuBar getJMenuToolBarDemoJMenuBar() {
if (ivjJMenuToolBarDemoJMenuBar == null) {
try {
ivjJMenuToolBarDemoJMenuBar = new JMenuBar();
ivjJMenuToolBarDemoJMenuBar.setName("JMenuToolBarDemoJMenuBar");
ivjJMenuToolBarDemoJMenuBar.add(getmnuMenuBar());
} catch (Throwable ivjExc) {
handleException(ivjExc);
}
}

return ivjJMenuToolBarDemoJMenuBar;
}

// construccion de un JMenuItem (salir)
private JMenuItem getmnuItemSalir() {
if (ivjmnuItemSalir == null) {
try {
ivjmnuItemSalir = new JMenuItem();
ivjmnuItemSalir.setName("mnuItemSalir");
ivjmnuItemSalir.setIcon(null);
ivjmnuItemSalir.setMnemonic('S');
ivjmnuItemSalir.setText("Salir");
} catch (Throwable ivjExc) {
handleException(ivjExc);
}
}

return ivjmnuItemSalir;
}

// construccion de un JMenu
private JMenu getmnuMenuBar() {
if (ivjmnuMenuBar == null) {
try {
ivjmnuMenuBar = new JMenu();
ivjmnuMenuBar.setName("mnuMenuBar");
ivjmnuMenuBar.setMnemonic('A');
ivjmnuMenuBar.setText("Archivo");
ivjmnuMenuBar.add(getmnuItemSalir());
} catch (Throwable ivjExc) {
handleException(ivjExc);
}
}

return ivjmnuMenuBar;
}

// construccion de un JToolBar
private JToolBar gettoolBar() {
if (ivjtoolBar == null) {
try {
ivjtoolBar = new JToolBar();
ivjtoolBar.setName("toolBar");
ivjtoolBar.setFloatable(false);
ivjtoolBar.setBounds(0, 0, 426, 45);
gettoolBar().add(gettoolBarBoton(), gettoolBarBoton().getName());
} catch (Throwable ivjExc) {
handleException(ivjExc);
}
}

return ivjtoolBar;
}

private JButton gettoolBarBoton() {
if (ivjtoolBarBoton == null) {
try {
ivjtoolBarBoton = new JButton();
ivjtoolBarBoton.setName("toolBarBoton");
ivjtoolBarBoton.setText("");
ivjtoolBarBoton.setToolTip("Salir de la aplicacion");
ivjtoolBarBoton.setMaximumSize(new Dimension(42, 42));
ivjtoolBarBoton.setFocusPainted(false);
ivjtoolBarBoton.setHorizontalTextPosition(SwingConstants.CENTER);
ivjtoolBarBoton.setVerticalTextPosition(SwingConstants.BOTTOM);
ivjtoolBarBoton.setIcon(new ImageIcon(getClass().getResource("/img/door02.gif")));
ivjtoolBarBoton.setPreferredSize(new Dimension(42, 42));
ivjtoolBarBoton.setMargin(new Insets(0, 0, 0, 0));
ivjtoolBarBoton.setMinimumSize(new Dimension(42, 42));
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
return ivjtoolBarBoton;
}

private void handleException(Throwable exception) {
System.out.println("--------- Exception no considerada ---------");
exception.printStackTrace(System.out);
}

private void initConnections() throws java.lang.Exception {
this.addComponentListener(ivjEventHandler);
getmnuItemSalir().addActionListener(ivjEventHandler);
gettoolBarBoton().addActionListener(ivjEventHandler);
}

private void initialize() {
try {
setName("JMenuToolBarDemo");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(426, 240);
setJMenuBar(getJMenuToolBarDemoJMenuBar());
setContentPane(getJFrameContentPane());
initConnections();
} catch (Throwable ivjExc) {
handleException(ivjExc);
}
}

public void jMenuItem1_ActionEvents() {
this.dispose();
System.exit(0);
}

public void jMenuToolBarDemo_ComponentResized(ComponentEvent componentEvent) {
gettoolBar().setBounds(0, 0, this.getWidth(), 45);
}

public void jToolBarButton1_ActionEvents() {
this.dispose();
System.exit(0);
}

public static void main(java.lang.String[] args) {
try {
JMenuToolBarDemo aJMenuToolBarDemo;
aJMenuToolBarDemo = new JMenuToolBarDemo();

aJMenuToolBarDemo.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
};
});

aJMenuToolBarDemo.show();
Insets insets = aJMenuToolBarDemo.getInsets();
aJMenuToolBarDemo.setSize(aJMenuToolBarDemo.getWidth() + insets.left + insets.right, aJMenuToolBarDemo.getHeight() + insets.top + insets.bottom);
aJMenuToolBarDemo.setVisible(true);
} catch (Throwable exception) {
System.err.println("Exception ocurrido en main() de javax.swing.JFrame ...");
exception.printStackTrace(System.out);
System.exit(0);
}
}
}
¿Buscas más hilos como este? Mira el foro de Java (básico) o usa el buscador.