📜  Java摇摆 | Java中的半透明和形状窗口

📅  最后修改于: 2022-05-13 01:55:48.444000             🧑  作者: Mango

Java摇摆 | Java中的半透明和形状窗口

Java提供了不同的函数,我们可以通过这些函数来控制窗口或框架的半透明。为了控制框架的不透明度,不得进行装饰。框架的不透明度是框架或组件的半透明度的量度。
在Java中,我们可以通过两种方式创建形状窗口,首先使用作为 com.sum.awt 包的一部分的 AWTUtilities AWTUtilities 类有一个函数- setWindowShape(Window w, Shape s) 设置窗口的形状,第二个是通过使用 setShape(Shape s) 将窗口的形状设置为指定的形状。
在本文中,我们将讨论窗口的均匀半透明。
使用方法:

  1. setWindowShape(Window w, Shape s) :将窗口 w 的形状设置为指定的形状 s
  2. setShape(Shape s) :将窗口的形状设置为指定的形状s
  3. setOpacity(float f) :设置框架的不透明度值

1.程序创建一个半透明框架并在JSlider的帮助下控制它的半透明

Java
// Java  program to create a
// translucent frame and control
// its translucency with
// the help of a JSlider
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
class solveit extends JFrame implements ChangeListener {
 
    // frame
    static JFrame f;
 
    // slider
    static JSlider b;
 
    // label
    static JLabel l;
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame
        f = new JFrame("translucent window");
 
        // create a object
        solveit s = new solveit();
 
        // create label
        l = new JLabel();
 
        // create a panel
        JPanel p = new JPanel();
 
        // create a slider
        b = new JSlider(0, 100, 100);
 
        // paint the ticks and tracks
        b.setPaintTrack(true);
        b.setPaintTicks(true);
        b.setPaintLabels(true);
 
        // set spacing
        b.setMajorTickSpacing(20);
        b.setMinorTickSpacing(5);
 
        // setChangeListener
        b.addChangeListener(s);
 
        // add slider to panel
        p.add(b);
        p.add(l);
 
        f.add(p);
 
        // set the text of label
        l.setText("Opacity value is =" + b.getValue());
 
        // set the size of frame
        f.setSize(300, 300);
 
        // decorated frame's opacity cant be set
        // so make the frame undecorated
        f.setUndecorated(true);
 
        // set opacity value for the window
        f.setOpacity(b.getValue() * 0.01f);
 
        f.setLocation(500, 300);
 
        f.show();
    }
 
    // if opacity value is changed
    public void stateChanged(ChangeEvent e)
    {
        l.setText("opacity value is =" + b.getValue());
 
        // set opacity value for the window
        f.setOpacity(b.getValue() * 0.01f);
    }
}


Java
// Java Program to create a shaped translucent
// window (using AWTUtilities)and
// control its translucency using a slider
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import com.sun.awt.AWTUtilities;
class solveit extends JFrame implements ChangeListener {
 
    // frame
    static JFrame f;
 
    // slider
    static JSlider b;
 
    // label
    static JLabel l;
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame
        f = new JFrame("translucent window");
 
        // create a object
        solveit s = new solveit();
 
        // create label
        l = new JLabel();
 
        // create a panel
        JPanel p = new JPanel();
 
        // create a slider
        b = new JSlider(0, 100, 100);
 
        // paint the ticks and tracks
        b.setPaintTrack(true);
        b.setPaintTicks(true);
        b.setPaintLabels(true);
 
        // set spacing
        b.setMajorTickSpacing(20);
        b.setMinorTickSpacing(5);
 
        // setChangeListener
        b.addChangeListener(s);
 
        // add slider to panel
        p.add(b);
        p.add(l);
 
        f.add(p);
 
        // set the text of label
        l.setText("Opacity value is =" + b.getValue());
 
        // set the size of frame
        f.setSize(300, 300);
 
        // decorated frame's opacity cant be set so make the frame undecorated
        f.setUndecorated(true);
 
        // set opacity value for the window
        f.setOpacity(b.getValue() * 0.01f);
 
        // set window shape using AWTUtilities class
        AWTUtilities.setWindowShape(f, new Ellipse2D.Float(20f, -30f, 250.0f, 150.0f));
 
        f.setLocation(500, 300);
 
        f.show();
    }
 
    // if opacity value is changed
    public void stateChanged(ChangeEvent e)
    {
        l.setText("opacity value is =" + b.getValue());
 
        // set opacity value for the window
        f.setOpacity(b.getValue() * 0.01f);
    }
}


Java
// Program to create a shaped translucent
// window (using setShape)
// and control its translucency using a slider
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import com.sun.awt.AWTUtilities;
class solveit extends JFrame implements ChangeListener {
 
    // frame
    static JFrame f;
 
    // slider
    static JSlider b;
 
    // label
    static JLabel l;
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame
        f = new JFrame("translucent window");
 
        // create a object
        solveit s = new solveit();
 
        // create label
        l = new JLabel();
 
        // create a panel
        JPanel p = new JPanel();
 
        // create a slider
        b = new JSlider(0, 100, 100);
 
        // paint the ticks and tracks
        b.setPaintTrack(true);
        b.setPaintTicks(true);
        b.setPaintLabels(true);
 
        // set spacing
        b.setMajorTickSpacing(20);
        b.setMinorTickSpacing(5);
 
        // setChangeListener
        b.addChangeListener(s);
 
        // add slider to panel
        p.add(b);
        p.add(l);
 
        f.add(p);
 
        // set the text of label
        l.setText("Opacity value is =" + b.getValue());
 
        // set the size of frame
        f.setSize(300, 300);
 
        // decorated frame's opacity cant be set so make the frame undecorated
        f.setUndecorated(true);
 
        // set opacity value for the window
        f.setOpacity(b.getValue() * 0.01f);
 
        // set window shape
        f.setShape(new Ellipse2D.Float(20f, -30f, 250.0f, 150.0f));
 
        f.setLocation(500, 300);
 
        f.show();
    }
 
    // if opacity value is changed
    public void stateChanged(ChangeEvent e)
    {
        l.setText("opacity value is =" + b.getValue());
 
        // set opacity value for the window
        f.setOpacity(b.getValue() * 0.01f);
    }
}


输出 :

2.程序创建一个形状的半透明窗口(使用AWTUtilities)并使用滑块控制其半透明

Java

// Java Program to create a shaped translucent
// window (using AWTUtilities)and
// control its translucency using a slider
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import com.sun.awt.AWTUtilities;
class solveit extends JFrame implements ChangeListener {
 
    // frame
    static JFrame f;
 
    // slider
    static JSlider b;
 
    // label
    static JLabel l;
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame
        f = new JFrame("translucent window");
 
        // create a object
        solveit s = new solveit();
 
        // create label
        l = new JLabel();
 
        // create a panel
        JPanel p = new JPanel();
 
        // create a slider
        b = new JSlider(0, 100, 100);
 
        // paint the ticks and tracks
        b.setPaintTrack(true);
        b.setPaintTicks(true);
        b.setPaintLabels(true);
 
        // set spacing
        b.setMajorTickSpacing(20);
        b.setMinorTickSpacing(5);
 
        // setChangeListener
        b.addChangeListener(s);
 
        // add slider to panel
        p.add(b);
        p.add(l);
 
        f.add(p);
 
        // set the text of label
        l.setText("Opacity value is =" + b.getValue());
 
        // set the size of frame
        f.setSize(300, 300);
 
        // decorated frame's opacity cant be set so make the frame undecorated
        f.setUndecorated(true);
 
        // set opacity value for the window
        f.setOpacity(b.getValue() * 0.01f);
 
        // set window shape using AWTUtilities class
        AWTUtilities.setWindowShape(f, new Ellipse2D.Float(20f, -30f, 250.0f, 150.0f));
 
        f.setLocation(500, 300);
 
        f.show();
    }
 
    // if opacity value is changed
    public void stateChanged(ChangeEvent e)
    {
        l.setText("opacity value is =" + b.getValue());
 
        // set opacity value for the window
        f.setOpacity(b.getValue() * 0.01f);
    }
}

输出 :

3. 程序创建一个有形状的半透明窗口(使用setShape)并使用滑块控制它的半透明

Java

// Program to create a shaped translucent
// window (using setShape)
// and control its translucency using a slider
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import com.sun.awt.AWTUtilities;
class solveit extends JFrame implements ChangeListener {
 
    // frame
    static JFrame f;
 
    // slider
    static JSlider b;
 
    // label
    static JLabel l;
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame
        f = new JFrame("translucent window");
 
        // create a object
        solveit s = new solveit();
 
        // create label
        l = new JLabel();
 
        // create a panel
        JPanel p = new JPanel();
 
        // create a slider
        b = new JSlider(0, 100, 100);
 
        // paint the ticks and tracks
        b.setPaintTrack(true);
        b.setPaintTicks(true);
        b.setPaintLabels(true);
 
        // set spacing
        b.setMajorTickSpacing(20);
        b.setMinorTickSpacing(5);
 
        // setChangeListener
        b.addChangeListener(s);
 
        // add slider to panel
        p.add(b);
        p.add(l);
 
        f.add(p);
 
        // set the text of label
        l.setText("Opacity value is =" + b.getValue());
 
        // set the size of frame
        f.setSize(300, 300);
 
        // decorated frame's opacity cant be set so make the frame undecorated
        f.setUndecorated(true);
 
        // set opacity value for the window
        f.setOpacity(b.getValue() * 0.01f);
 
        // set window shape
        f.setShape(new Ellipse2D.Float(20f, -30f, 250.0f, 150.0f));
 
        f.setLocation(500, 300);
 
        f.show();
    }
 
    // if opacity value is changed
    public void stateChanged(ChangeEvent e)
    {
        l.setText("opacity value is =" + b.getValue());
 
        // set opacity value for the window
        f.setOpacity(b.getValue() * 0.01f);
    }
}

输出 :

注意:以上程序可能无法在在线编译器中运行,请使用离线 IDE。建议使用最新版本的Java来运行上述程序,如果使用旧版本的Java ,用户可能会遇到问题。