BuildAndFeel在Build之后发生了变化

时间:2014-08-14 07:16:22

标签: java swing netbeans jar build

我将程序中的LookAndFeel更改为“windows”。 在Clean和Build之后,我运行.jar文件,但LookAndFeel已更改。 当我在Netbeans中编译程序时,我选择的“windows”LookAndFeel工作正常。

问题是什么?有没有图书馆错过了?

来自德国的问候, 帕特里克

public class main{
final private static String lookAndFeel = "Windows";
public static String getLookAndFeel(){
return lookAndFeel;}
[...]
}

在我的帧级:

public static void main(String args[]){
main ref = new main();
String lookAndFeel = ref.getLookAndFeel;
try{
for(javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()){
if(lookAndFeel.equals(info.getName())){
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}}}catch[...]

left the netbeans compiled version, right the cmd-jar version

3 个答案:

答案 0 :(得分:0)

您的代码是正确的。可能是你在另一边做错了。这是一个帮助你的例子。

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;

public class LookAndFeel {

    public static void main(String... args) {
        String lookAndFeel = "Windows";
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if (lookAndFeel.equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            System.out.println("ERROR : " + ex);
        }

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("TESTING");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(500, 300);
                f.setLayout(new java.awt.FlowLayout());
                f.add(new JButton("Click ME"));
                f.setVisible(true);
            }
        });
    }
}

我在Netbeans的PC上运行,也通过CMD运行它,工作正常 在Clean and Build之后,您将在项目的dist目录中获得.jar文件。

到达dist目录后写入以下命令,在cmd中

java -jar MyProject.jar

MyProject是我创建的项目的名称 以下是我在PC上获得的输出:

enter image description here

答案 1 :(得分:0)

当我尝试使用此代码直接设置LookAndFeel时:

javax.swing.UIManager.setLookAndFeel(com.sun.java.swing.plaf.windows.WindowsLookAndFeel);

用try-catch包围,netbeans给出语法错误并说:

cannot find symbol
symbol: class windows
location: package com.sun.java.swing.plaf

所以没有找到LaF?这是错吗?

问候

答案 2 :(得分:0)

jeah,现在,它运作正常。

我在目录“c:\ program files \ java \ jre \ lib \”中创建一个名为“swing.properties”的文件 与文本

swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel

现在,LookAndFeel有正确的主题:) :)感谢您的回答!

相关问题