0
我有一個框架JMenuBar
。當我按JMenuItems
其中之一時,我想添加一個JLabel
到框架,它不起作用。我不明白......它在同一班。JMenuItem無法動態添加組件
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);
}
}
是否打印 「您按下hedsup」 安慰使用JMenu,用一個CardLayout的例子嗎? – 2012-07-22 23:17:35
1)不要使用空佈局2)在添加或刪除組件後,不要忘了重新繪製()和重新驗證()你的容器。 – 2012-07-22 23:18:00
@Lablabla是的,它打印到控制檯我的消息 – que1326 2012-07-22 23:20:04