如何使用netbeans ide在中间设置菜单栏位置

时间:2014-01-03 10:02:57

标签: java swing

public class frame extends JFrame {

/**
 * Creates new form frame8
 */
public frame8() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenu2 = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jMenu1.setText("File");
    jMenuBar1.add(jMenu1);

    jMenu2.setText("Edit");
    jMenuBar1.add(jMenu2);

    setJMenuBar(jMenuBar1);

    pack();
}// </editor-fold>                        

/**
 * @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(frame8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(frame8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(frame8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(frame8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new frame8().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
// End of variables declaration                   
}

2 个答案:

答案 0 :(得分:0)

initComponents()中添加以下代码以设置菜单栏位置。

setLayout(new BorderLayout());
add(jMenuBar1, BorderLayout.NORTH); // set position

答案 1 :(得分:0)

确实并非完全无足轻重,因为NetBeans GUI编辑器有自己的(正确的)意见,菜单栏应该位于顶部。

private static class JMenuBarWithTitle extends JMenuBar {
    private final JLabel titleLabel;
    private final static int TITLE_HEIGHT = 30;

    JMenuBarWithTitle(String title) {
        setMargin(new java.awt.Insets(TITLE_HEIGHT, 0, 0, 0));
        titleLabel = new JLabel(title);
        titleLabel.setOpaque(true);
        titleLabel.setForeground(SystemColor.activeCaptionText);
        titleLabel.setBackground(SystemColor.activeCaption);
        titleLabel.setFont(getFont().deriveFont(Font.BOLD, 14.0F));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        titleLabel.setBounds(0, 0, getWidth(), TITLE_HEIGHT);
        titleLabel.paint(g);
    }
}

然后添加JMenuBar并选择自定义创建代码new JMenuBarWithTitle("Hello");