2011-09-30 114 views
3

我在一个项目中使用Nimbus外观和感觉。但是,尽管每个GUI JComponent都具有Nimbus的外观和感觉,但JFrame始终具有Windows外观和感觉。JFrame和Nimbus外观和感觉

JFrame如何具有Nimbus外观和感觉?

编辑:操作系统:Windows XP中

+2

参见[我怎么可以自定义JFrame的标题栏?](http://stackoverflow.com/questions/2781987/how-can-i-customize-the-title-bar-on-jframe ) – Jonas

+0

它说我不能?我可以吗? – MOD

回答

8

尝试使用这样的:

JFrame.setDefaultLookAndFeelDecorated(true); //before creating JFrames 

欲了解更多信息,请参阅How to Set the Look and Feel教程。


import javax.swing.*; 

class FrameLook { 

    public static void showFrame(String plaf) { 
     try { 
      UIManager.setLookAndFeel(plaf); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
     JFrame f = new JFrame(plaf); 
     f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     f.setSize(400,100); 
     f.setLocationByPlatform(true); 
     f.setDefaultLookAndFeelDecorated(true); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     showFrame(UIManager.getSystemLookAndFeelClassName()); 
     showFrame(UIManager.getCrossPlatformLookAndFeelClassName()); 
     showFrame("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
    } 
} 

Frame title bar Look'n'Feel

+1

不起作用:/ – MOD

+3

@Marek起初,我发表了一条评论,声称OP必须做错事,但我编辑到您的答案的来源表明否则。在这里,虽然x平面框架标题栏与Windows标题栏不同,但Nimbus标题栏是相同的。我怀疑这意味着Nimbus对标题栏没有特别的改变。其他操作系统的用户能否确认我的结果? –

+2

@Marku,很好的例子+1 – mKorbel

4

确认@安德鲁的怀疑,setDefaultLookAndFeelDecorated()说,如果支持, “新创建的JFrame旨意有当前LookAndFeel提供他们Window装饰品。”我改变了大小看整个标题。

FrameLook

import javax.swing.*; 

class FrameLook { 

    public static void showFrame(String plaf) { 
     try { 
      UIManager.setLookAndFeel(plaf); 
     } catch (Exception e) { 
      e.printStackTrace(System.out); 
     } 
     JFrame f = new JFrame(plaf); 
     f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     f.setSize(500, 100); 
     f.setLocationByPlatform(true); 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     showFrame(UIManager.getSystemLookAndFeelClassName()); 
     showFrame(UIManager.getCrossPlatformLookAndFeelClassName()); 
     showFrame("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
    } 
} 
+1

@Andrew Thompson:这似乎也取决于平台当前的窗口管理器。 – trashgod

2

并确认基于Windows经典UI为XP。 enter image description here

+0

请注意,Nimbus中窗格的背景色是不同的,并且是默认颜色,而不是Windows窗格的背景颜色。 –

1

由于Nimbus不支持窗口装饰,所以你不能直接这样做,这就是为什么你总是得到系统窗口,即使给出了答案。 试试这个非常简单的代码:

import javax.swing.LookAndFeel; 
import javax.swing.UIManager; 
import javax.swing.UIManager.LookAndFeelInfo; 

public class DoesNimbusSupportWindowDecorations { 

    @SuppressWarnings("unchecked") 
    public static void main(String... args) { 
     LookAndFeel nimbus = null; 
     for (LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels()) { 
      if (lafInfo.getName() == "Nimbus") { 
       try { 
        nimbus = ((Class<LookAndFeel>) Class.forName(
          lafInfo.getClassName())).newInstance(); 
       } catch (Exception e) { 
        System.err.println("Unexpected exception."); 
       } 
      } 
     } 

     if (nimbus != null) { 
      System.out.println("Nimbus supports window decorations...? " 
        + (nimbus.getSupportsWindowDecorations() ? "YES" : "NO")); 
     } else { 
      System.err.println("Your system does not support Nimbus, you can't" 
        + " run this test."); 
     } 
    } 

} 

或者干脆用正确导入您的代码中:

System.out.println(new NimbusLookAndFeel().getSupportsWindowDecorations()); 

什么是超出了我的理解就是为什么Sun决定这样的事情,因为确实存在内部框架装饰并有一个定制的装饰。我将研究是否可以通过扩展NimbusLookAndFeel或默认播放来使用这些装饰,因为Nimbus基于Synth,不确定最佳方式。

0

这就是我的做法。从我的eclipse项目复制粘贴。

import javax.swing.UIManager.LookAndFeelInfo; 
import java.awt.EventQueue; 
import java.awt.BorderLayout; 
import javax.swing.*; 
public class Frame1 { 
    private JFrame frame; 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 

        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
          if ("Nimbus".equals(info.getName())) { 
           UIManager.setLookAndFeel(info.getClassName()); 
           break; 
          } 
         } 
       Frame1 window = new Frame1(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    }