- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望将相同的 JMenuItem 添加到多个 JMenu,但它只显示在最后一个 JMenu 中。这是我编写的代码。我希望为所有 JMenu 状态显示三个 JMenu 项。使用此代码,前两个状态没有 JMenuItems,所有三个状态都只有最后一个。
import javax.swing.*;
import java.awt.event.*;
public class Menu extends JFrame{
public Menu()
{
super("Funky Menu");
JMenu [] states = new JMenu [3];
JMenuItem [] items = new JMenuItem [3];
//Initializing the items
items[0] = new JMenuItem("Industries");
items[0].setMnemonic('I');
items[1] = new JMenuItem("Hill Stations");
items[1].setMnemonic('H');
items[2] = new JMenuItem("Top Institutions");
items[2].setMnemonic('T');
//Initializing the states
//I've set the adjacent keys as the Mnemonics for easy user interaction
//though it is less intuitive, it can vary on the user preference.
states[0] = new JMenu("Tamil Nadu"); states[0].setMnemonic('Q');
states[1] = new JMenu("West Bengal"); states[1].setMnemonic('W');
states[2] = new JMenu("Haryana"); states[2].setMnemonic('E');
//Adding all the items to each of the states
for(int i=0; i<3; ++i)
{
for(int j=0; j<3; ++j)
{
states[i].add(items[j]);
}
}
//adding action listener to menu items
for(int j=0; j<3; ++j)
{
items[j].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
//The next few lines could be clubbed together in one but for
//clarity sake I write them seperately
JMenuItem currentItem = (JMenuItem) evt.getSource();
String textToDisplay = currentItem.getText();
System.out.println(textToDisplay + " : located in ...");
//one liner : System.out.println(((JMenuItem) evt.getSource()).getText() + " : located in ...");
}
});
}
//finally to fix up the MenuBar
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
for(int i=0; i<3; ++i)
{
bar.add(states[i]);
}
getContentPane();
//TODO Create a JLabel add it to the contents
//Instead of writing to the console, update the frames text
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args)
{
Menu app = new Menu();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
最佳答案
我没有检查你的代码,但一个组件只能有一个父组件。您需要创建单独的菜单项(可能使用相同的 Action 对象)。
关于java - 将 JMenuItem 添加到多个菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10195323/
我有这个代码: JMenuItem itemA; JMenuItem itemB; JMenuItem itemC; JMenuItem[] items = {itemA, itemB, itemC}
如何防止 JMenuItem 在单击 JMenuItem 时关闭菜单? JMenuItem 已启用。 这就是场景,我有 3 个 JMenuItems: JMenuItem: A, B, C; C 显示
我有一个 JPopupMenu 并在其中插入一些 JMenuItems。我需要的是一个看起来与普通母鹿相同但不可单击的 JMenuItem。 我尝试过: JPopupMenu popmen = new
我有一个 JMenu,其中填充有来自具有监听器的数据库中的 JMenuItems,其中之一是从数据库中删除条目(如果选择)。发生这种情况时,JMenuItem 应该从菜单中消失。这是一个简短的例子 f
我正在尝试在 JMenuBar 上创建一个帮助按钮。目前我可以使用这个使其向右对齐 helpItem.setComponentOrientation(ComponentOrientation.RIGH
我正在尝试通过单击文件菜单栏中的菜单项来加载特定文件 (C://myfile.txt),我需要帮助来完成代码的操作部分 JMenuBar menuBar = new JMenuBar(); JMenu
是否可以创建一个包含复选框列表的自定义 JMenuItem?示例是 Excel 过滤器弹出菜单: 到目前为止,我所做的是这样的: JPopupMenu headerPopup = new JPopup
此代码将用户最喜欢的歌曲从 ArrayMap 添加到 JmenuItem public void actionPerformed(ActionEvent evt) { Stri
如何将 JMenuItem (newItem) 添加到 JMenu (menuUsers)?是否有合适的 ActionListener 用于此目的?有一部分代码执行将菜单项添加到菜单的操作。它在引发某
我的 inv 终于可以工作了! :D 但是,你知道...现在...这很愚蠢。我希望它在我右键单击时显示“使用项目 1”或其他内容,所以我这样做: if (actItemx == "It
我有一个带有 JMenuBar 的框架。当我按下 JMenuItems 之一时,我想向框架添加一个 JLabel ,但它不起作用。我不明白……这是同一个类(class)的。 public class
JMenuItem 具有以下构造函数:(来源: GrepCode ) public JMenuItem(Action a) { this(); setAction(a); } 但是,当
我创建了一个带有颜色的 3x3 内存游戏。程序运行并显示颜色,但 JMenuBar 无法正常工作。新棋盘、游戏和结束游戏按钮不起作用。我的 Actionlistener 有问题吗? 这就是代码应该如何
我将菜单添加到工具栏,如下所示: JMenuBar menu = new JMenuBar(); JMenu actions = new JMenu("Aktionen");
我想为颜色列表添加每个菜单项行,但有类似的错误 "at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)" 在我的
所以我必须在 Swing 中为我的 Java 类创建一个简单的 GUI,我偶然发现了这个小的装饰性问题。 我有以下代码: JMenuItem mntmQuit = new JMenuItem(
我试图添加一些菜单,想知道如何在第二个 menuItem 之后添加边框 就这样 Image Here 这是我的代码 jmb = new JMenuBar(); jmb.setPreferredSize
我想为 JMenuItem 设置快捷键。 我现在是这样设置的 openFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionE
我自动生成一组 JMenuItem 并将它们添加到每个右键单击事件的 JPopUpMenu 中。问题是当我在所选菜单上按回车键时,我为菜单项设置的操作没有运行。我可以使用向上/向下键在菜单上导航,但是
我有一个带有标准项和快捷方式的 JMenuBar。但是我注意到快捷方式描述是左对齐的,看起来很难看。有办法右对齐吗? PS:“Umschalt”表示转变。有没有办法强制它说 shift 而不是 Ums
我是一名优秀的程序员,十分优秀!