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

发布于 2024-10-18 23:55:48 字数 233 浏览 7 评论 0 原文

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

frame

我想要该窗口布局,而不是标准的 Windows-边界,我不知道这怎么称呼。

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

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

frame

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:

not working for me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小嗲 2024-10-25 23:55:48

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

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.

殊姿 2024-10-25 23:55:48

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

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

够钟 2024-10-25 23:55:48

您需要首先设置外观和感觉才能使用跨平台的外观和感觉(正如有人在称为金属之前评论的那样)。然后,在创建框架之前,您需要请求根据外观绘制边框。

try
{
    UIManager.setLookAndFeel(
        UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) { }

这会将外观和感觉设置为您想要的。因为 Sun 的 JRE 的跨平台外观和感觉是金属的。

// Get window decorations drawn by the look and feel.
JFrame.setDefaultLookAndFeelDecorated(true);

// Create the JFrame.
JFrame frame = new JFrame("A window");

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

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.

北方。的韩爷 2024-10-25 23:55:48

将其添加到您的 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);
        }

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);
        }
仙女山的月亮 2024-10-25 23:55:48

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

公共静态无效主(字符串参数[]){
尝试 {

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

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); 
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文