How to make a window look like this in Java?

穿精又带淫゛_ 提交于 2020-01-02 03:37:44

问题


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:


回答1:


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.




回答2:


that's called look and feel, you can find a detailed explanation here http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html




回答3:


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.




回答4:


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); 
        }
    });
}



回答5:


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);
        }


来源:https://stackoverflow.com/questions/5075243/how-to-make-a-window-look-like-this-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!