gpt4 book ai didi

java - JPanel 未添加到 MenuSelected 中

转载 作者:行者123 更新时间:2023-12-01 14:27:26 25 4
gpt4 key购买 nike

我的问题是,当选择“登录”菜单时,此代码应该在中心创建一个新按钮。当我选择菜单时,它打印“aaa”但不添加按钮。我该如何修复它?谢谢。

public class NewJApplet extends JApplet {
static JPanel panel;
static Container content;
static JMenuBar menubar;

/**
* Initialization method that will be called after the applet is loaded into
* the browser.
*/
public void init() {
//panel = new JPanel();
//BorderLayout borderlayout = new BorderLayout();
//panel.setLayout(borderlayout);
panel = new JPanel();
BorderLayout borderlayout = new BorderLayout();
panel.setLayout(borderlayout);


menubar = new JMenuBar();
JMenu login = new JMenu("Login");
menubar.add(login);
login.addMenuListener( new MenuListener(){
public void menuSelected(MenuEvent e) {
System.out.println("aaa");
//menubar.setVisible(false);
panel.add(new JButton("add"), BorderLayout.CENTER);
}
public void menuCanceled(MenuEvent e) {

}
public void menuDeselected(MenuEvent e) {

}
});



JMenu arizabildirimformu = new JMenu("Arıza Bildirim Formu");
menubar.add(arizabildirimformu);

setJMenuBar(menubar);


//panel.add(new JButton("add"), BorderLayout.CENTER);
//panel.add(menubar, BorderLayout.NORTH);
// TODO start asynchronous download of heavy resources
content = getContentPane();
content.setLayout(new GridBagLayout());
content.add(panel);

}

}

最佳答案

当您将组件添加到可见 GUI 时,代码应该是:

panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed

我建议您也看看How to Use Menus 。您确实应该使用 JMenuItems 和 ActionListeners。

关于java - JPanel 未添加到 MenuSelected 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17096667/

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