gpt4 book ai didi

java - JMenuItem 无法动态添加组件

转载 作者:行者123 更新时间:2023-12-02 00:17:45 25 4
gpt4 key购买 nike

我有一个带有 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();
}
});
}
}

结果是:

enter image description here enter image description here

关于java - JMenuItem 无法动态添加组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11604749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com