- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 JMenuBar
的框架。当我按下 JMenuItems
之一时,我想向框架添加一个 JLabel
,但它不起作用。我不明白……这是同一个类(class)的。
public class GuiMain {
private JLabel logo_headsup;
JFrame frame1 = new JFrame();
JMenuBar menubar;
// other stuff
public GuiMain() {
frame1.setLayout(null);
logo_headsup = new JLabel(new ImageIcon(getClass().getResource(
"logo_headsup.png")));
logo_headsup.setBounds(headImage.getWidth() + 2, 120, 271, 46);
menubar = new JMenuBar();
menubar.setOpaque(false);
menubar.setBorderPainted(false);
menubar.setBounds(0, 0, 750, 30);
JMenu view = new JMenu("View");
view.setMnemonic('V');
view.setFont(new Font("Verdana", Font.BOLD, 10));
Color myColor = new Color(192, 0, 0);
view.setForeground(myColor);
JMenuItem hu = new JMenuItem("Heads-up");
JMenuItem sh = new JMenuItem("Short-handed");
JMenuItem fr = new JMenuItem("Full-ring");
hu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You pressed headsup"); // testing & it works
frame1.remove(logo_fullring);
frame1.remove(logo_shorthanded);
frame1.add(logo_headsup).repaint(); // now it`s working
}
});
view.add(hu);
view.add(sh);
view.add(fr);
menubar.add(view);
}
}
最佳答案
我强烈支持 Andrew Thompson 使用 CardLayout 的建议,因为这可以简化一切。将 JMenu 与 CardLayout 结合使用的示例:
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Swapper {
private static final String BLUEPRINT = "http://duke.kenai.com/IPG/Duke_Blueprint.gif";
private static final String COOKIES = "http://duke.kenai.com/IPG/Duke_Cookies.gif";
private static final String INT_FLOAT_CHAR = "http://duke.kenai.com/IPG/Duke_Int_Float_Char.gif";
private static final String THREAD = "http://duke.kenai.com/IPG/Duke_Thread.gif";
private JPanel mainPanel = new JPanel();
private JMenu menu = new JMenu("Menu");
private JMenuBar menuBar = new JMenuBar();
private CardLayout cardlayout = new CardLayout();
public Swapper() {
mainPanel.setLayout(cardlayout);
mainPanel.setBackground(Color.white);
addCard("Blueprint", BLUEPRINT);
addCard("Cookies", COOKIES);
addCard("Int Float Char", INT_FLOAT_CHAR);
addCard("Thread", THREAD);
menuBar.add(menu);
}
private void addCard(String name, String imagePath) {
BufferedImage img;
try {
URL imgUrl = new URL(imagePath);
img = ImageIO.read(imgUrl);
ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel(icon);
mainPanel.add(label, name);
menu.add(new JMenuItem(new MenuAction(name)));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public JComponent getMainPanel() {
return mainPanel;
}
public JMenuBar getMenuBar() {
return menuBar;
}
private class MenuAction extends AbstractAction {
public MenuAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent arg0) {
String name = getValue(NAME).toString();
cardlayout.show(mainPanel, name);
}
}
private static void createAndShowGui() {
Swapper swapper = new Swapper();
JFrame frame = new JFrame("Swapper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(swapper.getMainPanel());
frame.setJMenuBar(swapper.getMenuBar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
结果是:
关于java - JMenuItem 无法动态添加组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11604749/
我有这个代码: 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
我是一名优秀的程序员,十分优秀!