如何在JavaSwing中设置主菜单栏,使其视图位于中间
如何在JavaSwing中显示没有内部框架的菜单栏。我已经使用了内部框架来显示菜单栏。什么应该是另一个选项呢如何在JavaSwing中设置主菜单栏,使其视图位于中间,java,swing,jmenubar,Java,Swing,Jmenubar,如何在JavaSwing中显示没有内部框架的菜单栏。我已经使用了内部框架来显示菜单栏。什么应该是另一个选项呢 /** * @param args the command line arguments */ public static void main(String args[]) { try { //String host = "jdbc:derby://localhost:1527/Employees"; String host = "jdbc
/**
* @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
这是我的密码
私有无效jLabel3FocusGainedjava.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将菜单栏设置在顶部都不会出错。希望这有帮助
/**
* @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
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);
但它应该在帧的顶部移动。是否有类似于快照视图的替代方案。你们在说什么?你的意思是你想在不使用内部框架的情况下给顶部增加空间吗?什么应该是一个替代的选项呢?你可以从阅读开始。别忘了,JMenuBar只是另一个组件……如何将菜单栏定位到画面中间。若不使用内部框架,它将在框架顶部移动。请帮助我如何使用netbeans designer netbeans中有水平和垂直属性,没有位置。为什么不设置菜单栏?我希望我的标题和标签位于菜单栏上方。这就是为什么我在内部框架中使用菜单栏。正如我所知道的,菜单栏应该是框架顶部的组件。如果想知道如何在Swing中设置没有内部框架的类似布局,请尝试为菜单栏使用另一个jpanel。将其布局设置为borderlayout,并将菜单栏添加到jpanel.setJMenuBar中。setJMenuBar也应该可以工作,没有想到hahawe无法在面板中添加菜单栏