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

时间:2016-08-21 11:49:09

标签: java swing jmenubar

尽管使用JMenubarmyFrame.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:
}      
}

1 个答案:

答案 0 :(得分:2)

您正在创建两个 JMenuBars,而不是一个,您填写了菜单,另一个不是 - 并且猜测您要添加哪个JMenuBars你的JFrame?

请注意,您的类会扩展JMenuBar - 即JMenuBar,并且是您正在添加到JFrame中的那个。

另一个JMenuBar是同一个类中的一个名为generationMenu的字段,它是您添加所有菜单但未添加到JFrame的字段。

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

this.setJMenuBar(createdMenuBar);

为:

this.setJMenuBar(createdMenuBar.getMenuBar());