동일한 메뉴 막대 구성 요소를 여러 프레임에 걸쳐 표시하고자하므로 필요에 따라 표시하고 (나중에 수정할 수있는) 사용자 정의 메뉴 모음을 만들었습니다. 그러나 디버그 할 때 사용자 지정 메뉴 모음이 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
클래스 : 당신은 당신이 메뉴를 채우기 이 JMenuBars이 아니라, 하나를 만드는 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:
}
}
합니다. 질문에 게시 된 코드는 실행 가능하지 않으므로 코드를 복사하여 내 Eclipse에 붙여 넣을 수 없으며 잘못된 것을 확인할 수 있습니다. –
'public class GenerationMenuBar extends JMenuBar {'그 코드에서'JMenuBar'를 확장해야 할 이유가 있다면 나는 그것을 놓쳤습니다. 메뉴 막대의 표준 인스턴스를 사용하십시오. 보다 일반적으로 더 나은 도움을 받으려면 [MCVE] 또는 [Short, Self Contained, Correct Example] (http://www.sscce.org/)를 게시하십시오. –
* "여러 프레임에 같은 JMenuBar 구성 요소를 표시하고 싶습니다."* 1) 정적 팩토리 메서드를 사용하면되지만 .. 2) [여러 JFrames 사용, 좋음/나쁨 연습]을 참조하십시오 (http : // stackoverflow .com/q/9554636/418556) –