Java - 外观和感觉问题

时间:2012-04-20 13:28:50

标签: java swing look-and-feel

我遇到了java外观问题。

我在主方法中使用以下代码将其设置为Nimbus皮肤:

public class Main
{
    public static void main(String[] args)
    {
        try
        {
            boolean found = false;
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
            {               
                if ("Nimbus".equals(info.getName()))
                {
                    UIManager.setLookAndFeel(info.getClassName());
                    found = true;
                    break;
                }
            }

            if (!found) UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            UIManager.put("AuditoryCues.playList", UIManager.get("AuditoryCues.allAuditoryCues"));
        }
        catch (Exception e) {}

        Manager mngr = new Manager();
        mngr.setSize(1000, 600);
        Utils.centerWindow(mngr);
        mngr.setVisible(true);
    }
}

它给了我这种窗户:

enter image description here

正如您所看到的,JInternalFrames已正确蒙皮,但主窗口没有!

如何将主题应用于此窗口?

感谢。


Manager是一个简单的JFrame,其代码如下:

public class Manager extends JFrame
{
    public Manager()
    {
        initComponents();
    }

    private void initComponents()
    {
        setDefaultCloseOperation(3);
        setTitle("My window");
        setIconImage(ImageLoader.getIcon().getImage());

        // My components go here

        pack();
    }
}

3 个答案:

答案 0 :(得分:3)

答案 1 :(得分:0)

好吧,我不知道在没有好SSCCE的情况下该怎么做,但是如果你想要一个总是对我有用的方法的例子你可以看一下Java-Helper github上。具体来说,请查看this line处的SwingHelper。我正在添加下面的代码,以符合堆栈溢出的一些基本规则;)祝你好运。

  /**
   * Sets the look and feel to the given type (like "Nimbus") Learn more here:
   * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
   *
   * @param lookAndFeel to set (like "Nimbus")
   */
  public static void setLookAndFeel(String lookAndFeel) {
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ((info.getName()).equals(lookAndFeel)) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(SwingHelper.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
  }

答案 2 :(得分:0)

只需在创建帧/对话框之前尝试启用这两个选项:

JDialog.setDefaultLookAndFeelDecorated ( true );
JFrame.setDefaultLookAndFeelDecorated ( true );

这将允许JFrame / JDialog从LaF而不是系统中获取它们的装饰。