簡體   English   中英

如何設置主菜單欄,使其在Java swing中間的視圖中

[英]how set main menu bar such that in its view in middle in java swing

在此處輸入圖片說明 http://postimg.org/image/fi1bwpfoz/如何顯示菜單欄,在Java swing中沒有內部框架。我使用內部框架來顯示菜單欄。這應該是替代方案。

在此處輸入圖片說明

這是我的代碼private void jLabel3FocusGained(java.awt.event.FocusEvent evt){
// TODO在此處添加您的處理代碼:}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) 
{
    try {

        //String host = "jdbc:derby://localhost:1527/Employees";
        String host = "jdbc:mysql://localhost:3306/mysql";
        String uName = "root";
        String uPass = "paras123";

       //Class.forName("com.mysql.jdbc.Driver").newInstance();
        java.sql.Driver d=new com.mysql.jdbc.Driver();

        Connection con = DriverManager.getConnection(host, uName, uPass);

        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * from employee");

        while (rs.next()) {

            int col = rs.getInt("id");
            String first_name = rs.getString("firstname");
            String last_name = rs.getString("lastname");
            String job = rs.getString("job_title");
            //String p=col+""+first_name+""+last_name+""+job;
            System.out.println(col + " " + first_name + " " + last_name + " " + job);

        }

    } catch (SQLException err) {
        //err.printStackTrace();
        System.out.println( err.getMessage( ) );
    }





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

// Variables declaration - do not modify                     
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu10;
private javax.swing.JMenu jMenu11;
private javax.swing.JMenu jMenu12;
private javax.swing.JMenu jMenu13;
private javax.swing.JMenu jMenu14;
private javax.swing.JMenu jMenu15;
private javax.swing.JMenu jMenu16;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu5;
private javax.swing.JMenu jMenu6;
private javax.swing.JMenu jMenu7;
private javax.swing.JMenu jMenu8;
private javax.swing.JMenu jMenu9;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuItem jMenuItem8;
private javax.swing.JPanel jPanel1;
private javax.swing.JPopupMenu.Separator jSeparator1;
// End of variables declaration                   

}

您可以使用BorderLayout將菜單欄設置在頂部,而不必調整窗口大小和所有大小。 希望這可以幫助。

JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Test");
JMenuItem item = new JMenuItem("test2");
menu.add(item);
menuBar.add(menu);
frame.add(menuBar, BorderLayout.NORTH);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM