How do I create a window which looks like this开发者_运维技巧 in Java:
I want that window layout, instead of the standard Windows-borders, and I don't know how this is called.
Edit: look and feel doesn't work for me:
If you want your Look and Feel to draw the window decoration (that's what the "border" is called), then you need to call JFrame.setDefaultLookAndFeelDecorated(true)
before creating your JFrame
objects and JDialog.setDefaultLookAndFeelDecorated(true)
before creating your JDialog
objects.
that's called look and feel, you can find a detailed explanation here http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
You will need to first set the look and feel to use the cross platform look and feel (As someone commented before it's called metal). Then before you create the Frame you need to request that the borders are drawn by the look and feel.
try
{
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) { }
This will set the look and feel to the one you want. As the cross platform look and feel is metal in Sun's JRE.
// Get window decorations drawn by the look and feel.
JFrame.setDefaultLookAndFeelDecorated(true);
// Create the JFrame.
JFrame frame = new JFrame("A window");
And this will make the created JFrame have borders like you describe.
Add this to your main() method:
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
To set windows look and feel for swing write following code in main method.
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("windows".equalsIgnoreCase(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex) {
System.out.println(e);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
MainFrame mainFrame = new MainFrame();
mainFrame.setExtendedState(MAXIMIZED_BOTH);
mainFrame.setVisible(true);
InitDatabaseDialog initDatabaseDialog = new InitDatabaseDialog(mainFrame, true);
initDatabaseDialog.setVisible(true);
}
});
}
精彩评论