NimbusLookAndFeel无法解析为变量

时间:2013-09-15 20:05:14

标签: java swing awt nimbus

我正在尝试使用swing学习基本的GUI。 当我尝试激活/设置nimbus时,显示以下错误“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel无法解析为变量”。 该错误显示在setLookAndFeel()方法的com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel行中。 我正在使用java build 1.7.0

import java.awt.FlowLayout;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.*;

public class swing1 extends JFrame {
    public swing1(){
        super("Title: Swing Project 1");
        //setLookAndFeel();
        setSize(225,80);
        setLookAndFeel();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flo = new FlowLayout();
        JButton adds = new JButton ("Add");
        JButton minus = new JButton("Substract");
        JButton mult = new JButton ("Multiply");
        add(adds);
        add(minus);
        add(mult);
        setVisible(true);                   
    }

    private void setLookAndFeel() {
        // TODO Auto-generated method stub
        try {
            UIManager.setLookAndFeel(“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”);
        }
        catch (Exception exc) {
            //ignore
        }       
    }

    public static void main (String args   []){
        swing1 startSwing = new swing1();
    }
}

4 个答案:

答案 0 :(得分:4)

您使用"定义的文字字符串不是

也可以使用此代码设置外观。

import javax.swing.UIManager.*;

try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (Exception e) {
    // If Nimbus is not available, you can set the GUI to another look and feel.
}

来自官方Nimbus Look and Feel

  

版本注意:请勿明确设置Nimbus外观   调用UIManager.setLookAndFeel方法,因为并非所有版本   或Java SE 6的实现支持Nimbus。另外,   Nimbus软件包的位置在JDK 6 Update 10和   JDK 7发布。迭代所有已安装的外观   实现是一种更强大的方法,因为如果不是Nimbus   可用,使用默认外观。对于JDK 6 Update 10   发布时,Nimbus包位于   com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel。

答案 1 :(得分:2)

使用常规报价

"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"

而不是

“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”

答案 2 :(得分:1)

如果您阅读The Java Tutorials处的文档,您会看到在Java 6和Java 7的发行版之间,Nimbus Look-and-Feel包的位置发生了变化。为Nimbus设置外观的推荐方法是:

import javax.swing.UIManager.*;

try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (Exception e) {
    // If Nimbus is not available, you can set the GUI to another look and feel.
}

答案 3 :(得分:0)

这是我设置Nimbus

的方法
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();
                }
            }
        });
    }