gpt4 book ai didi

java - 自定义 JMenuBar 添加到 JFrame 但不显示

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

尽管实例化了我的自定义 JMenubar,但使用 myFrame.setJMenuBar(customJMenuBar);myFrame.validate();myFrame .repaint(); 我的自定义菜单栏未显示。

我想跨多个框架显示相同的菜单栏组件,因此我创建了一个可以根据需要显示(并在将来修改)的自定义菜单栏。但是,当我调试时,我可以看到自定义菜单栏已添加到 JFrame 中,但它没有显示为标准或首选尺寸。

我的框架类:MainMenuScreen

public class MainMenuScreen extends javax.swing.JFrame {
/**
* Creates new form MainMenuScreen
*/
public MainMenuScreen() {
initComponents();
this.displayFileMenuBar();
}

/**
* Create new GenerationMenuBar
* Attach to MainMenuScreen
*/
private void displayFileMenuBar()
{
createdMenuBar = new GenerationMenuBar(this.designMenu);
this.setJMenuBar(createdMenuBar);
this.validate();
this.repaint();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainMenuScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainMenuScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainMenuScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainMenuScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MainMenuScreen().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton DesignExperimentButton;
private javax.swing.JPanel MainMenuPanel;
private javax.swing.JButton PresentExperimentButton;
private javax.swing.JMenuBar designMenu;
// End of variables declaration
}

我的自定义JMenuBar类:GenerationMenuBar

import java.awt.Dimension;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

/**
*
* @author Marion Grenfell-Essam
*/
public class GenerationMenuBar extends JMenuBar {

// GUI Attributes
private JMenuBar generationMenu;
private JMenu editDesignMenu;
private JMenu fileDesignMenu;
private JMenuItem newDesignMenuItem;
private JMenuItem openDesignMenuItem;
private JMenuItem saveAsMenuItem;
private JMenuItem saveDesignMenuItem;
private JMenuItem undoDesignMenuItem;

public GenerationMenuBar(JMenuBar designMenu){
initComponents(designMenu);
}

private void initComponents(JMenuBar sentMenuBar) {
this.setMenuBar(sentMenuBar);
this.getMenuBar().setPreferredSize(new Dimension(1024, 25));
fileDesignMenu = new JMenu();
newDesignMenuItem = new JMenuItem();
openDesignMenuItem = new JMenuItem();
saveDesignMenuItem = new JMenuItem();
saveAsMenuItem = new JMenuItem();
editDesignMenu = new JMenu();
undoDesignMenuItem = new JMenuItem();

fileDesignMenu.setText("File");

newDesignMenuItem.setText("New");
newDesignMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
newDesignMenuItemActionPerformed(evt);
}
});
fileDesignMenu.add(newDesignMenuItem);

openDesignMenuItem.setText("Open");
fileDesignMenu.add(openDesignMenuItem);

saveDesignMenuItem.setText("Save");
fileDesignMenu.add(saveDesignMenuItem);

saveAsMenuItem.setText("SaveAs");
fileDesignMenu.add(saveAsMenuItem);

this.getMenuBar().add(fileDesignMenu);

editDesignMenu.setText("Edit");

undoDesignMenuItem.setText("Undo");
editDesignMenu.add(undoDesignMenuItem);

this.getMenuBar().add(editDesignMenu);
}

// Getters
public JMenuBar getMenuBar()
{
return generationMenu;
}

// Setters
public void setMenuBar(JMenuBar sentMenuBar)
{
generationMenu = sentMenuBar;
}

// Event handlers
private void newDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void openDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void saveDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void undoDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
}

最佳答案

您正在创建两个 JMenuBar,而不是一个,一个用菜单填充,另一个则不填充 - 猜猜您要将哪一个添加到 JFrame 中?

请注意,您的类扩展了 JMenuBar —— 这是 JMenuBar 类,也是您要添加到 JFrame 中的类。

另一个 JMenuBar 是同一个类中的一个字段,称为 GenerationMenu,您要将所有菜单添加到该字段,但不将其添加到 JFrame。

解决方案:不要这样做。使用一个且仅一个 JMenuBar。我自己,我会让类扩展 JMenuBar,然后更改这一行:

this.setJMenuBar(createdMenuBar);

至:

this.setJMenuBar(createdMenuBar.getMenuBar());

关于java - 自定义 JMenuBar 添加到 JFrame 但不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39063849/

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