Java JFrame设置自定义不透明度

时间:2014-02-22 01:46:00

标签: java jframe

如何从用户输入更改jframe的不透明度,比如说从微调器?

int opacity = 7;
double dOpacity = opacity/10;
String sOpacity = Double.toString(dOpacity)+"f";
this.setOpacity(sOpacity); //???

上面的代码会产生一堆错误。无论如何得到不透明度数字,比方说6,然后将其转换为'this.setOpacity(sOpacity);'的值会接受吗?

错误:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: GuiClock.Main.setOpacity
    at GuiClock.Main.cinitComponents(Main.java:97)
    at GuiClock.Main.<init>(Main.java:16)
    at GuiClock.Main$6.run(Main.java:168)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

3 个答案:

答案 0 :(得分:2)

代码中的

dOpacity= 0.0

    int opacity = 7;
    float dOpacity = (float)opacity/10; //casting to float , here dOpacity=0.7
    String sOpacity = Float.toString(dOpacity)+"f"; // sOpacity="0.7f"
    this.setOpacity(Float.parseFloat(sOpacity)); //setOpacity(0.7f);

答案 1 :(得分:1)

setOpacity方法使用float作为参数。您正在传递String

尝试this.setOpacity(Float.parseFloat(sOpacity));

答案 2 :(得分:0)

您正在将一个String作为参数插入setOpacity方法,该方法只需要双精度。