【问题标题】:Working with JApplet with Menus使用带有菜单的 JApplet
【发布时间】:2011-09-26 13:19:07
【问题描述】:

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

这是我的第一个代码:

    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){

}
}

当我尝试运行它时,没有出现“音乐”菜单。它变为播放(单选按钮)。按钮组有帮助吗?当我尝试使用按钮组时,没有任何反应。

【问题讨论】:

  • 与另一个 Java 论坛上的答案相同
  • @mKorbel 这样吗??:: group.add(play);
  • 没有人能说这是过时的教程download.oracle.com/javase/tutorial/uiswing/components/… 和第一个。我在周围找到的示例java2s.com/Tutorial/Java/0240__Swing/…
  • 我已经阅读了该教程。 :( 仍然可以得到它。当我尝试使用 group.add(play);
  • id 已经阅读了该教程。仍然无法得到它:(当我尝试使用 group.add(play); 时,它真的不起作用。我的音乐菜单不会出现。它与我的播放收音机子菜单重叠:(

标签: java swing menu japplet


【解决方案1】:

像这样?

/* <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){

    }
}

代码中的错误基本上是:

  • 如果音乐有孩子,它必须是JMenu,而不是JMenuItem
  • ButtonGroup 是一个逻辑组(例如,从一组按钮中制作单选按钮),它不是一个容器。所以除了将按钮添加到组之外,还需要将它们添加到音乐JMenu

【讨论】:

  • 最好删除我的帖子+1
  • @mKorbel 你的代码更好,声明了serialVersionUID@Override 符号。 :)
  • @Andrew Thompson 正如您所提到的,您在 TextPad 中管理 Java 代码,但 IDE 管理我如何、在哪里、为什么...,我只是将 mouse_click 推到黄色,意图清理所有可能的警告,(有时)真正的 IDE 管理我们 :-)
  • @mKorbel 现在我已经升级到 64 位操作系统并且已经从(最近 2 到)3.2,然后到 4 Gig RAM(很快到 8),我一直在考虑给 TextPad轻弹并再次运行其中一个正确的 IDE。使用 64 位操作系统和一堆 RAM,看看我是否不那么讨厌它。 :)
【解决方案2】:

您的源代码中有语法错误。尝试注释掉失败并重新编译的行。这应该会在您的界面 (GUI) 中为您提供更多信息。

【讨论】:

    猜你喜欢
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2014-02-22
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多