02. juli 2004 - 13:32Der 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.
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); }
Hos Computerworld it-jobbank er vi stolte af at fortsætte det gode partnerskab med folkene bag IT-DAY – efter vores mening Danmarks bedste karrieremesse for unge og erfarne it-kandidater.
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();
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.