gpt4 book ai didi

java - NimbusLookAndFeel 无法解析为变量

转载 作者:行者123 更新时间:2023-11-29 06:37:10 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com