使用菜单使用JApplet [英] Working with JApplet with Menus

查看:91
本文介绍了使用菜单使用JApplet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题. (音乐)菜单的子菜单应为单选按钮类型.

I'm having problems with my code. The sub-menu for the (Music) menu should be a radio button type.

这是我的第一个代码:

    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.

推荐答案

喜欢吗?

/* <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:

  • 如果音乐中有孩子,则必须是JMenu,而不是JMenuItem
  • ButtonGroup是一个逻辑组(例如,使一组按钮中的单选按钮成为可能),它不是是一个容器.因此,除了将按钮添加到组之外,还必须将它们添加到音乐JMenu.
  • 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.

这篇关于使用菜单使用JApplet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆