【问题标题】:how set main menu bar such that in its view in middle in java swing如何设置主菜单栏,使其在 java swing 中间的视图中
【发布时间】:2014-01-03 06:14:42
【问题描述】:

http://postimg.org/image/fi1bwpfoz/ 如何在 java swing 中显示没有内部框架的菜单栏。我使用内部框架来显示菜单栏。什么应该是替代解决方案。

这是我的代码 私人无效 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                   

}

【问题讨论】:

  • 但它应该在框架顶部移动。是否有任何替代方案可以像快照视图一样。
  • 你在说什么?您的意思是要在不使用内部框架的情况下在顶部添加空间?
  • “什么应该是替代解决方案” - 您可以从阅读available tutorials 开始。别忘了,JMenuBar 只是另一个组件...
  • 如何将菜单栏定位到框架的中间。如果不使用内部框架,它将在框架顶部移动。请帮忙
  • 如何使用 netbeans 设计器,netbeans 中有水平和垂直属性,没有位置。南北在那

标签: java swing jmenubar


【解决方案1】:

无论窗口大小如何调整,使用 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);

【讨论】:

  • 为什么不setJMenuBar()
  • 我希望我的标题和标签位于菜单栏上方。这就是为什么在内部框架中使用菜单栏的原因。据我所知,菜单栏应该行为组件将是框架的顶部。任何想法如何在 Swing 中设置没有内部框架的类似布局
  • 然后尝试为菜单栏使用另一个 jpanel。将其布局设置为borderlayout并将菜单栏添加到该jpanel中。
  • setJMenuBar() 应该也可以,没想到哈哈
  • 我们无法在面板中添加菜单栏
猜你喜欢
  • 2013-09-21
  • 2016-10-10
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-23
  • 1970-01-01
  • 2020-12-04
相关资源
最近更新 更多