3

我正在尝试使用 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

4 回答 4

4
于 2013-09-15T20:07:13.590 回答
2

如果您阅读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.
}
于 2013-09-15T20:09:09.867 回答
2

使用常规引号

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

代替

“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
于 2013-09-15T20:07:08.573 回答
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();
                }
            }
        });
    }
于 2016-07-21T13:04:52.983 回答