Working with JApplet with Menus - java

I'm having problems with my code. The sub-menu for the (Music) menu should be a radio button type.
Here's my first code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AMBAT_FLAB1 extends JApplet implements ActionListener{
JMenuBar mainBar = new JMenuBar();
JMenu menu1 = new JMenu("File");
JMenu menu2 = new JMenu("Format");
JMenu menu3 = new JMenu("Background");
//for file
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem reset = new JMenuItem("Reset");
//for format
JMenuItem setFont = new JMenuItem("Set Font");
JMenuItem setColor = new JMenuItem("Set Color");
//for background
JMenuItem image = new JMenuItem("Images");
JMenuItem music = new JMenuItem("Music");
//submenu of music
JRadioButtonMenuItem play = new JRadioButtonMenuItem("Play");
JRadioButtonMenuItem loop = new JRadioButtonMenuItem("Loop");
JRadioButtonMenuItem stop = new JRadioButtonMenuItem("Stop");
ButtonGroup group = new ButtonGroup();
//file chooser
//JFileChooser fileChooser = new JFileChooser();
//fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//text area
JTextArea myArea = new JTextArea(50, 50);
JScrollPane scrollingArea = new JScrollPane(myArea);
Container con = getContentPane();
public void init(){
setJMenuBar(mainBar);
mainBar.add(menu1);
mainBar.add(menu2);
mainBar.add(menu3);
menu1.add(open);
menu1.add(save);
menu1.add(reset);
menu2.add(setFont);
menu2.add(setColor);
menu3.add(image);
menu3.add(music);
music.group.add(play);
//group.add(loop);
//music.add(stop);
open.addActionListener(this);
save.addActionListener(this);
reset.addActionListener(this);
setFont.addActionListener(this);
setColor.addActionListener(this);
image.addActionListener(this);
music.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
}
}
When I try to run it, the Music menu doesn't appear. It changes to the Play (radio button). Does the button group help? When i tried to use the button group nothing happens.

Like this?
/* <applet code='AMBAT_FLAB1' width=220 height=100></applet> */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AMBAT_FLAB1 extends JApplet implements ActionListener{
JMenuBar mainBar = new JMenuBar();
JMenu menu1 = new JMenu("File");
JMenu menu2 = new JMenu("Format");
JMenu menu3 = new JMenu("Background");
//for file
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem reset = new JMenuItem("Reset");
//for format
JMenuItem setFont = new JMenuItem("Set Font");
JMenuItem setColor = new JMenuItem("Set Color");
//for background
JMenuItem image = new JMenuItem("Images");
JMenu music = new JMenu("Music");
//submenu of music
JRadioButtonMenuItem play = new JRadioButtonMenuItem("Play");
JRadioButtonMenuItem loop = new JRadioButtonMenuItem("Loop");
JRadioButtonMenuItem stop = new JRadioButtonMenuItem("Stop");
ButtonGroup group = new ButtonGroup();
//file chooser
//JFileChooser fileChooser = new JFileChooser();
//fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//text area
JTextArea myArea = new JTextArea(50, 50);
JScrollPane scrollingArea = new JScrollPane(myArea);
Container con = getContentPane();
public void init(){
setJMenuBar(mainBar);
mainBar.add(menu1);
mainBar.add(menu2);
mainBar.add(menu3);
menu1.add(open);
menu1.add(save);
menu1.add(reset);
menu2.add(setFont);
menu2.add(setColor);
menu3.add(image);
menu3.add(music);
group.add(play);
group.add(loop);
group.add(stop);
music.add(play);
music.add(loop);
music.add(stop);
//music.add(stop);
open.addActionListener(this);
save.addActionListener(this);
reset.addActionListener(this);
setFont.addActionListener(this);
setColor.addActionListener(this);
image.addActionListener(this);
music.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
}
}
The mistakes in the code were basically:
If Music had children, it had to be a JMenu, not a JMenuItem
A ButtonGroup is a logical group (e.g. to make radio buttons out of a group of buttons), it is not a container. So besides adding the buttons to the group, it is necessary to add them to the Music JMenu.

You have a syntax error in your source code. Try commenting out the line that is failing and recompiling. That should give you a bit more information in your interface (GUI).

Related

How to Keep JMenuItem visible after press?

Just a simple GUI
Shows a JMenu >> JMenuItem
One of those menu items shows a JPopupMenu
My issue is that the JMenuItems' are setVisible(false) on press, by default
I've tried it with MouseAdapter too.
public class SubMenu extends JFrame
{
JMenuBar menubar = new JMenuBar();
JMenu File = new JMenu("File");
JMenuItem New = new JMenuItem("New");
JMenuItem Open = new JMenuItem("Open");
JMenuItem Save = new JMenuItem("Save");
JMenuItem Import = new JMenuItem("Import");
JPopupMenu Import_Popup = new JPopupMenu();
JMenuItem Import_Pop_1 = new JMenuItem("Import newsfeed list...");
JMenuItem Import_Pop_2 = new JMenuItem("Import bookmarks...");
JMenuItem Import_Pop_3 = new JMenuItem("Import mail...");
JMenuItem Exit = new JMenuItem("Exit");
SubMenu()
{
setTitle("SubMenu");
setSize(500, 500);
setLocation(200, 200);
setVisible(true);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
menubar.setBounds(0, 0, 2000, 30);
add(menubar);
menubar.add(File);
File.add(New);
File.add(Open);
File.add(Save);
File.add(new JSeparator());
File.add(Import);
File.add(new JSeparator());
File.add(Exit);
add(Import_Popup);
Import_Popup.add(Import_Pop_1);
Import_Popup.add(Import_Pop_2);
Import_Popup.add(Import_Pop_3);
// The issue is Right here.
Import.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
Import_Popup.show(Import, Import.getWidth(), 0);
}
});
}// Constructor
public static void main(String[] args)
{
new SubMenu();
}
}
Raises this exception on Press :
component must be showing on the screen to determine its location
Is there a way to keep JMenuItems visible after press?

How to add ScrollBar on Textarea or JFrame in Java Swing?

Hi everyone ,i`m beginner in programmation and i wanted to make a text editor interface by using java Eclipse and i get problem each time i add scrollbar , the text area disappear ... How can i fix my code to continue on it ?... i saw many tutorials but nothing work>
import java.awt.*;
import java.awt.color.*;
import javax.swing.*;
public class Fenetre extends JFrame {
JMenuBar Menubar = new JMenuBar();
JMenu MenuFichier = new JMenu("Fichier");
JMenuItem MenuFichier1 = new JMenuItem("Nouveau");
JMenuItem MenuFichier2 = new JMenuItem("Ouvrir");
JMenuItem MenuFichier3 = new JMenuItem("Enregister");
JMenuItem MenuFichier4 = new JMenuItem("Enregister sous");
JMenuItem MenuFichier5 = new JMenuItem("Quitter");
JMenu MenuEdition = new JMenu("Edition");
JMenuItem MenuEdition1 = new JMenuItem("Couper");
JMenuItem MenuEdition2 = new JMenuItem("Copier");
JMenuItem MenuEdition3 = new JMenuItem("Coller");
JMenu MenuAide = new JMenu("Aide");
JMenuItem MenuAide1 = new JMenuItem("A propos");
JPanel p=new JPanel();
JScrollPane sp = new JScrollPane ();
JTextArea jt = new JTextArea (90,90);
public Fenetre() {
add(p);
setTitle("Text Editor");
setBounds(100,100, 500, 500);
//setExtendedState(JFrame.MAXIMIZED_BOTH);
sp.add(jt);
jt.setText(null);
jt.setLineWrap(true);
jt.setWrapStyleWord(true);
jt.setVisible(true);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
p.add(sp);
sp.setVisible(true);
Menubar.add(MenuFichier);
Menubar.add(MenuEdition);
Menubar.add(MenuAide);
MenuFichier.add(MenuFichier1);
MenuFichier.add(MenuFichier2);
MenuFichier.addSeparator();
MenuFichier.add(MenuFichier3);
MenuFichier.add(MenuFichier4);
MenuFichier.addSeparator();
MenuFichier.add(MenuFichier5);
MenuEdition.add(MenuEdition1);
MenuEdition.add(MenuEdition2);
MenuAide.add(MenuAide1);
Menubar.setBackground(Color.PINK);
getContentPane().add(sp);
setJMenuBar(Menubar);
setVisible(true);
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
and this is the main of the code
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
public class CreationEditor {
public static void main(String[] args) {
JFrame f = new Fenetre();
}
}

How do I divide windows in using Swing?

I am writing a program in Java and I'm learning Swing as I go, but I seem to be at a roadblock. I have attached a sketch to explain what I'm trying to do, and hopefully you can help me understand what to do.
+===============================================================================+
|File Edit View Help |
+================================================================================
| | |
| | |
|Content 1 | Content 2 |
| | |
| | |
+===============================================================================+
I am using JFrames and JPanels to construct this program, but I cannot understand how to make the two frames for the content. Also, I want the left column to always be 150px wide, and the entire height of the frame, while the right should always fill the remaining width of the window.
package mycookbook;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
public class MyCookBook extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new MyCookBook();
}
public MyCookBook() {
super("My Cook Book vers. 0.0.0.1");
setSize(1920, 1030);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setMenu();
setWindows();
}
public void setMenu() {
//Setting up the menubar.
JMenuBar menubar = new JMenuBar();
//Adding the menu bar.
setJMenuBar(menubar);
//Setting up the file menu.
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenu nwFile = new JMenu("New >>");
JMenuItem cbNwFile = new JMenuItem("Cookbook");
JMenuItem chNwFile = new JMenuItem("Chapter");
JMenuItem rcNwFile = new JMenuItem("Recipe");
JMenu opFile = new JMenu("Open >>");
JMenuItem cbOpFile = new JMenuItem("Cookbook");
JMenuItem chOpFile = new JMenuItem("Chapter");
JMenuItem rcOpFile = new JMenuItem("Recipe");
JMenuItem svFile = new JMenuItem("Save");
JMenuItem ipFile = new JMenuItem("Import");
JMenuItem epFile = new JMenuItem("Export");
JMenuItem pnFile = new JMenuItem("Print");
JMenuItem upFile = new JMenuItem("Update");
JMenuItem prFile = new JMenuItem("Properties");
JMenuItem exFile = new JMenuItem("Exit");
//Adding all the file menu and its contents to the menubar.
menubar.add(fileMenu);
fileMenu.add(nwFile);
nwFile.add(cbNwFile);
nwFile.add(chNwFile);
nwFile.add(rcNwFile);
fileMenu.add(opFile);
opFile.add(cbOpFile);
opFile.add(chOpFile);
opFile.add(rcOpFile);
fileMenu.addSeparator();
fileMenu.add(svFile);
fileMenu.add(ipFile);
fileMenu.add(epFile);
fileMenu.add(pnFile);
fileMenu.addSeparator();
fileMenu.add(upFile);
fileMenu.add(prFile);
fileMenu.addSeparator();
fileMenu.add(exFile);
exFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//Setting up the edit menu.
JMenu editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
JMenuItem cbEdit = new JMenuItem("Cookbook");
JMenuItem chEdit = new JMenuItem("Chapter");
JMenuItem rcEdit = new JMenuItem("Recipe");
menubar.add(editMenu);
editMenu.add(cbEdit);
editMenu.add(chEdit);
editMenu.add(rcEdit);
//Setting up the view menu.
JMenu viewMenu = new JMenu("View");
viewMenu.setMnemonic(KeyEvent.VK_V);
JMenu mdView = new JMenu("Mode >>");
JMenuItem pnView = new JMenuItem("Panel View");
JMenuItem pgView = new JMenuItem("Page View");
JMenuItem lsView = new JMenuItem("List View");
JMenu rsView = new JMenu("Resolution >>");
JMenuItem smRes = new JMenuItem("1024x718");
JMenuItem mdRes = new JMenuItem("1440x910");
JMenuItem lgRes = new JMenuItem("1920x1030");
menubar.add(viewMenu);
viewMenu.add(mdView);
viewMenu.add(rsView);
mdView.add(pnView);
mdView.add(pgView);
mdView.add(lsView);
rsView.add(smRes);
rsView.add(mdRes);
rsView.add(lgRes);
smRes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setSize(1024,718);
setLocationRelativeTo(null);
}
});
mdRes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setSize(1440,910);
setLocationRelativeTo(null);
}
});
lgRes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setSize(1920,1030);
setLocationRelativeTo(null);
}
});
exFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//Setting up the help menu.
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic(KeyEvent.VK_H);
JMenuItem hpHelp = new JMenuItem("My Cookbook Help");
JMenuItem abHelp = new JMenuItem("About My Cookbook");
menubar.add(helpMenu);
helpMenu.add(hpHelp);
helpMenu.add(abHelp);
revalidate();
}
public void setWindows() {
JScrollPane bookPane = new JScrollPane();
JScrollPane recPane = new JScrollPane();
JSplitPane content = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,bookPane, recPane);
Dimension bpDim = new Dimension(150, 400);
Dimension rpDim = new Dimension(650, 400);
bookPane.setMinimumSize(bpDim);
recPane.setMinimumSize(rpDim);
add(content);
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
Read the section from the Swing tutorial on How to Use Border Layout.
One component goes to BorderLayout.LINE_START the other to BorderLayout.CENTER.
Also, I want the left column to always be 150px wide
The width is determined by the preferred size of the component added to that area of the BorderLayout.
You can simply solve this problem by using a different layout for your GUI. I recommend you check out the GridBagLayout as this layout is highly customizible.
We can modify our GridBagLayout by supplying it with GridBagConstraints once you've learned how to modify your GridBagLayout using GridBagConstraints your problem will be extremely simple to solve.
You asked how to divide windows, simple just make three JPanels and put two of thoseJPanels in the main JPanel that will be displayed at the highest level.

how to call a method from another class Java Swing?

I have the following SwingMenu class.
package base;
import javax.swing.*;
public class SwingMenu {
public static void main(String[] args) {
SwingMenu s = new SwingMenu();
}
public SwingMenu() {
JFrame frame = new JFrame(
"Creating a JMenuBar, JMenu, JMenuItem and seprator Component");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
JMenu filemenu = new JMenu("File");
filemenu.add(new JSeparator());
JMenu editmenu = new JMenu("Edit");
editmenu.add(new JSeparator());
JMenuItem fileItem1 = new JMenuItem("New");
JMenuItem fileItem2 = new JMenuItem("Open");
JMenuItem fileItem3 = new JMenuItem("Close");
fileItem3.add(new JSeparator());
JMenuItem fileItem4 = new JMenuItem("Save");
JMenuItem editItem1 = new JMenuItem("Cut");
JMenuItem editItem2 = new JMenuItem("Copy");
editItem2.add(new JSeparator());
JMenuItem editItem3 = new JMenuItem("Paste");
JMenuItem editItem4 = new JMenuItem("Insert");
filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
editmenu.add(editItem1);
editmenu.add(editItem2);
editmenu.add(editItem3);
editmenu.add(editItem4);
menubar.add(filemenu);
menubar.add(editmenu);
frame.setJMenuBar(menubar);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
And I want to display the Menu by calling it from the this main class.
package base;
import javax.swing.*;
import java.awt.*;
import base.SwingMenu;
public class StickyNotes {
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Java Sticky Notes");
frame.setPreferredSize(new Dimension(5000, 5000));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
// Add Label
JLabel label = new JLabel("Type Below");
frame.getContentPane().add(label);
// Add Main Menu
SwingMenu mainBar = new SwingMenu();
//frame.setJMenuBar(mainBar);
//frame.getContentPane().add(mainBar);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public Container createContentPane() {
// Create the content-pane-to-be.
JPanel jplContentPane = new JPanel(new BorderLayout());
jplContentPane.setLayout(new BorderLayout());
jplContentPane.setOpaque(true);
return jplContentPane;
}
public static void main(String[] args) {
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I just cannot figure it out all morning :) How do I get the Menu Bar to show up in Java Swing?
// Add Main Menu
SwingMenu mainBar = new SwingMenu();
Check this modified code example :
import javax.swing.*;
import java.awt.*;
public class StickyNotes {
private JMenuBar getMenuBar()
{
JMenuBar menubar = new JMenuBar();
JMenu filemenu = new JMenu("File");
filemenu.add(new JSeparator());
JMenu editmenu = new JMenu("Edit");
editmenu.add(new JSeparator());
JMenuItem fileItem1 = new JMenuItem("New");
JMenuItem fileItem2 = new JMenuItem("Open");
JMenuItem fileItem3 = new JMenuItem("Close");
fileItem3.add(new JSeparator());
JMenuItem fileItem4 = new JMenuItem("Save");
JMenuItem editItem1 = new JMenuItem("Cut");
JMenuItem editItem2 = new JMenuItem("Copy");
editItem2.add(new JSeparator());
JMenuItem editItem3 = new JMenuItem("Paste");
JMenuItem editItem4 = new JMenuItem("Insert");
filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
editmenu.add(editItem1);
editmenu.add(editItem2);
editmenu.add(editItem3);
editmenu.add(editItem4);
menubar.add(filemenu);
menubar.add(editmenu);
return menubar;
}
private void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Java Sticky Notes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
// Add Label
JLabel label = new JLabel("Type Below");
frame.getContentPane().add(label);
// Add Main Menu
frame.setJMenuBar(getMenuBar());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public Container createContentPane() {
// Create the content-pane-to-be.
JPanel jplContentPane = new JPanel(new BorderLayout());
jplContentPane.setLayout(new BorderLayout());
jplContentPane.setOpaque(true);
return jplContentPane;
}
public static void main(String[] args) {
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new StickyNotes().createAndShowGUI();
}
});
}
}
Or you can modify your code a bit like this, if you really wanted to keep the JMenuBar set up in a different Class, where you can simply make an Object of the SwingMenu Class and call the method getMenuBar() by making an Object of this Class :
import javax.swing.*;
import java.awt.*;
public class StickyNotes {
private void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Java Sticky Notes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
// Add Label
JLabel label = new JLabel("Type Below");
frame.getContentPane().add(label);
// Add Main Menu
SwingMenu swingMenu = new SwingMenu();
frame.setJMenuBar(swingMenu.getMenuBar());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public Container createContentPane() {
// Create the content-pane-to-be.
JPanel jplContentPane = new JPanel(new BorderLayout());
jplContentPane.setLayout(new BorderLayout());
jplContentPane.setOpaque(true);
return jplContentPane;
}
public static void main(String[] args) {
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new StickyNotes().createAndShowGUI();
}
});
}
}
class SwingMenu {
public JMenuBar getMenuBar()
{
JMenuBar menubar = new JMenuBar();
JMenu filemenu = new JMenu("File");
filemenu.add(new JSeparator());
JMenu editmenu = new JMenu("Edit");
editmenu.add(new JSeparator());
JMenuItem fileItem1 = new JMenuItem("New");
JMenuItem fileItem2 = new JMenuItem("Open");
JMenuItem fileItem3 = new JMenuItem("Close");
fileItem3.add(new JSeparator());
JMenuItem fileItem4 = new JMenuItem("Save");
JMenuItem editItem1 = new JMenuItem("Cut");
JMenuItem editItem2 = new JMenuItem("Copy");
editItem2.add(new JSeparator());
JMenuItem editItem3 = new JMenuItem("Paste");
JMenuItem editItem4 = new JMenuItem("Insert");
filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
editmenu.add(editItem1);
editmenu.add(editItem2);
editmenu.add(editItem3);
editmenu.add(editItem4);
menubar.add(filemenu);
menubar.add(editmenu);
return menubar;
}
}
You are creating 2 different JFrames. After creating the JFrame:
JFrame frame = new JFrame("Java Sticky Notes");
Create the menu bar and assign it to the JFrame:
JMenuBar menubar = new JMenuBar();
// ...
frame.setJMenuBar(menubar);
No need for:
SwingMenu mainBar = new SwingMenu();
Try this out:
This basically makes SwingMenu a manu bar which will help encapsulating the build of the menu bar.
public class SwingMenu extends JMenuBar{
public SwingMenu() {
JMenu filemenu = new JMenu("File");
JMenu editmenu = new JMenu("Edit");
// Build your file menu and edit menu here...
add(filemenu);
add(editmenu);
}
}
Now, in your createAndShowGUI() just create a form and add the newly created menu bar to it.
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Java Sticky Notes");
// other stuff...
// Add Main Menu
SwingMenu mainBar = new SwingMenu();
frame.setJMenuBar(mainBar);
// Display the window.
frame.pack();
frame.setVisible(true);
}

Creating menus for JApplet

I am making a simple game where numbers fall from top to bottom
and I have to type the number. (number is erased as I type the number)
This is coordinated with a Zen.java which is a JApplet file.
I am trying to make a menu for this game with a typical menu bar.
So far, I've tried this..
public class MenuApplet extends JApplet{
public void init(){
JMenuBar menubar = new JMenuBar();
JMenu menuOption = new JMenu("Option");
JMenuItem NewGame = new JMenuItem("New Game");
menuOption.add(NewGame);
JMenuItem exitGame = new JMenuItem("Exit Game");
menuOption.add(exitGame);
JMenu menuLevel = new JMenu("Level");
JMenuItem levelOne = new JMenuItem("Level One");
JMenuItem levelTwo = new JMenuItem("Level Two");
JMenuItem levelThree = new JMenuItem("Level Three");
}
}
Right before my main method. However, the menu bar doesn't even show up.
I would appreciate a couple of advices.
/* <applet code='MenuApplet' width=200 height=100></applet> */
import javax.swing.*;
public class MenuApplet extends JApplet{
public void init(){
JMenuBar menubar = new JMenuBar();
JMenu menuOption = new JMenu("Option");
JMenuItem NewGame = new JMenuItem("New Game");
menuOption.add(NewGame);
JMenuItem exitGame = new JMenuItem("Exit Game");
menuOption.add(exitGame);
JMenu menuLevel = new JMenu("Level");
JMenuItem levelOne = new JMenuItem("Level One");
JMenuItem levelTwo = new JMenuItem("Level Two");
JMenuItem levelThree = new JMenuItem("Level Three");
// the menu items, menus and menu bar all need
// to be ADDED to something!
menubar.add(menuOption);
menuOption.add(NewGame);
menuOption.add(exitGame);
menubar.add(menuLevel);
menuLevel.add(levelOne);
menuLevel.add(levelTwo);
menuLevel.add(levelThree);
setJMenuBar(menubar);
}
}
You have to add JMenu into JMenuBar and finally use setJMenuBar to set menubar object.
menubar.add(menuOption);
menubar.add(exitGame);
setJMenuBar(menubar);

Categories

Resources