JMenu doesn't appear on top
Hello!I have created a small app (all code below) that should use a JMenuBar with JMenu and items. But the JMenu shows behind other components below the MenuBar, very strange.
So if any one could test the code below and se if you can figure out how to get the JMenu to stay on top, above all components in the app.
Best regards
Fredrik
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Main extends JFrame
{
GuiManager guiManager = new GuiManager();
GuiMenuBar guiMenuBar = new GuiMenuBar();
public Main()
{
super("JMenu Tester");
setSize(320, 355);
getContentPane().add(guiManager);
setJMenuBar(guiMenuBar);
show();
}
public static void main(String[] args)
{
new Main();
}
}
class GuiManager extends JPanel
{
Panel2D panel= new Panel2D("Panel");
public GuiManager()
{
setBackground(Color.black);
setLayout(null);
panel.setBounds(5, 5, 300, 300);
add(panel);
}
}
class GuiMenuBar extends JMenuBar implements ActionListener
{
JMenu file = new JMenu("File");
JMenuItem _new = new JMenuItem("New");
JMenuItem save = new JMenuItem("Save");
JMenuItem exit = new JMenuItem("Exit");
public GuiMenuBar()
{
exit.addActionListener(this);
file.add(_new);
file.add(save);
file.add(exit);
add(file);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == exit)
{
System.exit(1);
}
}
}
class Panel2D extends JPanel
{
Canvas2D canvas2d = new Canvas2D();
public Panel2D(String borderTitle)
{
setBackground(Color.red);
Border border = BorderFactory.createLineBorder(Color.black);
TitledBorder titledBorder = BorderFactory.createTitledBorder(border, borderTitle);
setBorder(titledBorder);
setLayout(null);
canvas2d.setBounds(10, 15, 280, 275);
add(canvas2d);
}
}
class Canvas2D extends Canvas
{
public Canvas2D()
{
setBackground(Color.yellow);
}
}
