如何在 Java 中使窗口看起来像这样?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5075243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:18:06  来源:igfitidea点击:

How to make a window look like this in Java?

javaswinglook-and-feel

提问by friedkiwi

How do I create a window which looks like this in Java:

如何在 Java 中创建一个如下所示的窗口:

frame

框架

I want that window layout, instead of the standard Windows-borders, and I don't know how this is called.

我想要那个窗口布局,而不是标准的 Windows 边框,我不知道它是如何调用的。

Edit: look and feel doesn't work for me:

编辑:外观和感觉对我不起作用:

not working for me

不对我来说有效

回答by Joachim Sauer

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)beforecreating your JFrameobjects and JDialog.setDefaultLookAndFeelDecorated(true)beforecreating your JDialogobjects.

如果您希望外观绘制窗口装饰(这就是所谓的“边框”),那么您需要创建对象之前和创建对象之前调用。JFrame.setDefaultLookAndFeelDecorated(true)JFrameJDialog.setDefaultLookAndFeelDecorated(true)JDialog

回答by Mansuro

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

这就是所谓的观感,你可以在这里找到详细的解释http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

回答by Varun Madiath

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.

您需要首先设置外观以使用跨平台外观(正如有人在将其称为金属之前所评论的那样)。然后在创建 Frame 之前,您需要请求边框是由外观和感觉绘制的。

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.

这会将外观和感觉设置为您想要的。由于跨平台的外观和感觉在 Sun 的 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.

这将使创建的 JFrame 具有您描述的边框。

回答by PKhode

To set windows look and feel for swing write following code in main method.

要为 Swing 设置窗口外观,请在 main 方法中编写以下代码。

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

回答by stdapsy

Add this to your main() method:

将此添加到您的 main() 方法中:

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