Results 1 to 6 of 6
Thread: Applet in JFrame, JMenu problem
- 12-27-2009, 07:31 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 5
- Rep Power
- 0
Applet in JFrame, JMenu problem
I'm having a problem with the below code, being displayed properly on windows vista. However it works on windows xp for me.
Java Code:public GUI(Applet app) { super("Title"); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); JMenuBar jmb = new JMenuBar(); // -- File -- JMenu file = new JMenu("File"); JMenuItem open = new JMenuItem("Open..."); open.setActionCommand("file-open"); open.addActionListener(this); JMenuItem reload = new JMenuItem("Reload"); reload.setActionCommand("file-reload"); reload.addActionListener(this); JMenuItem exit = new JMenuItem("Exit"); exit.setActionCommand("file-exit"); exit.addActionListener(this); file.add(open); file.add(reload); file.add(new JSeparator()); file.add(exit); // -- jmb.add(file); getContentPane().add(jmb); setJMenuBar(jmb); getContentPane().add(app, BorderLayout.CENTER); setSize(519, 394); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setVisible(true); }
What happens is:
On vista, the JMenuItem's are displayed under the applet. Instead of on top of the applet.
Why is this?
Thanks,
Bill86
-
It seems funny that you're adding an Applet to to a JFrame, but perhaps your problem is more from mixing AWT and Swing components than from trying shove the applet square peg into the desktop round hole.
So why this design?
- 12-27-2009, 12:42 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 5
- Rep Power
- 0
-
You would be better off if the "different Java program" (here called the "GUI class") produced a JPanel instead of an Applet. This way, should you desire to display it as an applet, you would create a class that extends JApplet, have it hold an instance of your GUI class (the one that now produces a JPanel) and place the JPanel into the JApplet's contentPane. Likewise if you wanted to display it as a JFrame, create a class that displays a JFrame, have it hold an instance of the GUI class and then place the GUI's JPanel into the JFrame's contentPane.
- 12-28-2009, 03:41 AM #5
Member
- Join Date
- Dec 2009
- Posts
- 5
- Rep Power
- 0
Like this?
"GUI.class" that now extends JPanel, instead of JFrame.
Java Code:// Applet app = (Applet) Class.forName("abc", false, loader).newInstance(); public GUI(Applet app) { setLayout(new BorderLayout()); add(app, BorderLayout.CENTER); }
The below class extends JFrame / implements ActionListener.
It works just the same on xp, I'll test it on vista when I can.Java Code:// 'panel' = an instance of the "GUI.class". public GUIJ(JPanel panel) { super("Title"); JMenuBar jmb = new JMenuBar(); // -- File -- JMenu file = new JMenu("File"); JMenuItem open = new JMenuItem("Open..."); open.setActionCommand("file-open"); open.addActionListener(this); JMenuItem reload = new JMenuItem("Reload"); reload.setActionCommand("file-reload"); reload.addActionListener(this); JMenuItem exit = new JMenuItem("Exit"); exit.setActionCommand("file-exit"); exit.addActionListener(this); file.add(open); file.add(reload); file.add(new JSeparator()); file.add(exit); // -- End -- jmb.add(file); getContentPane().add(jmb); setJMenuBar(jmb); getContentPane().add(panel); setResizable(false); setSize(519, 394); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); }
Edit: Still doesn't work on vista. Am I doing something wrong still?Last edited by Bill86; 12-28-2009 at 04:13 AM.
-
Similar Threads
-
link between two JFrame using JMenu
By JavaEbony in forum NetBeansReplies: 8Last Post: 03-09-2011, 11:19 AM -
Embed java applet in JFrame
By Bill89 in forum New To JavaReplies: 0Last Post: 12-09-2009, 01:05 PM -
javax.media.Player and JMenu problem
By nowy in forum AWT / SwingReplies: 2Last Post: 11-10-2009, 11:49 AM -
Closing Popup JFrame in Applet
By Arsenic in forum Java AppletsReplies: 3Last Post: 04-10-2009, 08:37 PM -
JFrame vs Applet
By baltimore in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 03:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks