简体   繁体   English

Swing 应用程序无法运行

[英]Swing apps doesn't run

I have a problem in using swing.我在使用 Swing 时遇到问题。 I don't know what is the cause of this but i'm just trying to create a simple frame app using swing at it give me a lot of error.我不知道这是什么原因,但我只是尝试使用 Swing 创建一个简单的框架应用程序,这给了我很多错误。

    import javax.swing.JFrame;
public class StacksGui {
public static void main(String args[]) 
     JFrame frame = new JFrame();
     frame.setVisible(true);
     frame.setSize(100,200);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

    Exception in thread "main" java.lang.ExceptionInInitializerError
    at javax.swing.JPanel.updateUI(Unknown Source)
    at javax.swing.JPanel.<init>(Unknown Source)
    at javax.swing.JPanel.<init>(Unknown Source)
    at javax.swing.JPanel.<init>(Unknown Source)
    at javax.swing.JRootPane.createGlassPane(Unknown Source)
    at javax.swing.JRootPane.<init>(Unknown Source)
    at javax.swing.JFrame.createRootPane(Unknown Source)
    at javax.swing.JFrame.frameInit(Unknown Source)
    at javax.swing.JFrame.<init>(Unknown Source)
    at StacksGui.main(StacksGui.java:9)
Caused by: java.lang.IllegalArgumentException: 0 incompatible with Text-specific LCD contrast key
    at java.awt.RenderingHints.put(Unknown Source)
    at sun.awt.windows.WDesktopProperties.getDesktopAAHints(Unknown Source)
    at sun.awt.windows.WToolkit.getDesktopAAHints(Unknown Source)
    at sun.awt.SunToolkit.getDesktopFontHints(Unknown Source)
    at sun.awt.windows.WDesktopProperties.getProperties(Unknown Source)
    at sun.awt.windows.WToolkit.updateProperties(Unknown Source)
    at sun.awt.windows.WToolkit.lazilyInitWProps(Unknown Source)
    at sun.awt.windows.WToolkit.lazilyLoadDesktopProperty(Unknown Source)
    at java.awt.Toolkit.getDesktopProperty(Unknown Source)
    at javax.swing.UIManager.<clinit>(Unknown Source)
    ... 10 more

I had problem when all java Swing apps crashed silently.当所有 java Swing 应用程序无声地崩溃时,我遇到了问题。 After trying to launch from command line got the following exception:尝试从命令行启动后出现以下异常:

Caused by: java.lang.IllegalArgumentException: 181193932 incompatible with Text-specific LCD contrast key

As it's said in JDK-6503988 , the problem is connected with ClearType registry setting FontSmoothingGamma .正如JDK-6503988 中所说,问题与ClearType注册表设置FontSmoothingGamma 有关

This can be fixed by running " Adjust ClearType text " from Control panel ( cttune.exe ) or by changing the registry directly:这可以通过从控制面板 ( cttune.exe ) 运行“ 调整 ClearType 文本”或直接更改注册表来解决:

reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /t REG_DWORD /v FontSmoothingGamma /d 1024 /f

A reboot may be needed for changes to take the effect.可能需要重新启动才能使更改生效。

See also a 10-year-old investigation of the same problem at https://www.rarst.net/software/cleartype-install4j-java-bug/ .另请参阅https://www.rarst.net/software/cleartype-in​​stall4j-java-bug/上对同一问题的 10 年调查。

This code works for me:这段代码对我有用:

package test;

import javax.swing.JFrame;
public class Test {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setSize(100,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

I only see a difference at我只看到不同之处

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

where I added the frame.我添加框架的地方。 However your stacktrace looks like there's another error, so first try this and if it still don't work, give us a bit more code to look at.但是,您的堆栈跟踪似乎还有另一个错误,因此请先尝试此操作,如果仍然无效,请给我们提供更多代码以供查看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM