尽管实例化了我的自定义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:
}
}
最佳答案
您正在创建两个JMenuBars,而不是一个,一个是用菜单填充的菜单,另一个是不填充的菜单-并猜测要添加到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/