Swing apps doesn't run

大憨熊 提交于 2020-05-23 21:36:27

问题


I have a problem in using 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.

    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

回答1:


I had problem when all java Swing apps crashed silently. 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.

This can be fixed by running "Adjust ClearType text" from Control panel (cttune.exe) or by changing the registry directly:

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/.




回答2:


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.



来源:https://stackoverflow.com/questions/21444846/swing-apps-doesnt-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!