半透明的JFrame除了JTextField?

时间:2016-01-17 02:26:22

标签: translucency

我想知道是否有办法制作半透明的JFrame,除了一个特定的东西,在我的例子中,是一个JTextField。可能吗? 这是我得到的:

public class TranslucentWindowDemo extends JFrame {

Font font1 = new Font("SansSerif", Font.BOLD, 40);
private static String bullets;
private static JTextField text;
Color c = new Color(133,255,50);

public TranslucentWindowDemo() {
    super();
    setLayout(new GridBagLayout());

    setSize(300,200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    text = new JTextField(bullets);
    text.setFont(font1);
    text.selectAll();
    text.setSelectionColor(c);
    add(text);
    text.setEditable(false);
    setAlwaysOnTop(true);
}


public static void main(String[] args) {

    // Determine if the GraphicsDevice supports translucency.
    GraphicsEnvironment ge = 
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();

    //If translucent windows aren't supported, exit.
    if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
        System.err.println(
            "Translucency is not supported");
            System.exit(0);
    }

    JFrame.setDefaultLookAndFeelDecorated(true);

    // Create the GUI on the event-dispatching thread
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            bullets = JOptionPane.showInputDialog(null, "Number of bullets in magazine:");
            TranslucentWindowDemo tw = new TranslucentWindowDemo();

            // Set the window to 55% opaque (45% translucent).
            tw.setOpacity(0.55f);

            // Display the window.
            tw.setVisible(true);
        }
    });
}

}

我做了一些研究,没有一个是我想要的,希望很快就能听到答案,提前谢谢!

0 个答案:

没有答案