java - NimbusLookAndFeel 无法解析为变量

标签 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();
    }
}

最佳答案

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

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

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 .

Version Note: Do not set the Nimbus look and feel explicitly by invoking the UIManager.setLookAndFeel method because not all versions or implementations of Java SE 6 support Nimbus. Additionally, the location of the Nimbus package changed between the JDK 6 Update 10 and JDK 7 releases. Iterating through all installed look and feel implementations is a more robust approach because if Nimbus is not available, the default look and feel is used. For the JDK 6 Update 10 release, the Nimbus package is located at com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel.

关于java - NimbusLookAndFeel 无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18816952/

相关文章:

java - 当尝试将 1 到 n 之间的所有数字加在一起时,代码不会返回总和

java - Spring 数据 Jpa : Multiple ContainingIgnoreCase

java - 使用接口(interface)实现将线程结果返回给Fragment

java - Android - 如何在 Google map 上为 Google Logo 设置底部填充

java - 有没有办法让 java.awt.Choice 进一步下拉?

用于绘制图像的 Java Graphics2D 方法,其中使用像素 alpha 值,但颜色值替换为给定颜色

java - 如何从 jinternalframe 调用 jdialog

java - 如何在包含 HTML 的 JLabel 中获取省略号?

java - 在屏幕上移动对象

Java JPG转PNG