Working with JApplet with Menus
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){
}
}
开发者_如何学Go
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 aJMenuItem
- 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 MusicJMenu
.
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).
精彩评论