Avatar billede fredand Forsker
02. juli 2004 - 13:32 Der er 2 kommentarer og
1 løsning

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);
    }
}
Avatar billede _carsten Nybegynder
02. juli 2004 - 18:14 #1
Yes it is, and again - no it's not!

The problem is your class: Canvas2D

Class Canvas2D extends java.awt.Canvas, which is an AWT Component, and normally there are no problems in mixing AWT and Swing, but this is a situation where things go wrong.

Swing component's don't have the native interface that makes them capable to be visible on screen, they have to borough that capability from there closest AWT component.
The opposite goes for AWT component, they carry all the native stuff they need, to make them selfe visible on screen, and they will always be on top, that's whay AWT components are called HEAVYWEIGHT components and Swing = LIGHTWEIGHT components

You can mix AWT and Swing, but keep in mind how it works.
By the way, you don't need a Canvas for drawing, use a JPanel instead.

Here is an example, the AWT button is added below the Swing button, but when you run this example, you will see the AWT button is on top of the Swing button.

public class Exp extends javax.swing.JFrame {

    public Exp() {
        initComponents();
        setSize(350,250);
    }

    private void initComponents() {
        button1 = new java.awt.Button();
        jButton1 = new javax.swing.JButton();

        getContentPane().setLayout(null);

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        button1.setLabel("AWT Button");
        getContentPane().add(button1);
        button1.setBounds(220, 80, 80, 70);

        jButton1.setText("Swing JButton");
        getContentPane().add(jButton1);
        jButton1.setBounds(50, 110, 260, 23);

        pack();
    }

    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }

    public static void main(String args[]) {
        new Exp().show();
    }

    private java.awt.Button button1;
    private javax.swing.JButton jButton1; 
}
Avatar billede _carsten Nybegynder
02. juli 2004 - 18:18 #2
This will make all the difference

class Canvas2D extends JPanel
{
    public Canvas2D()
    {
        setBackground(Color.yellow);
    }
}
Avatar billede fredand Forsker
03. juli 2004 - 10:42 #3
Thanks alot mate!!!

Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester