繁体   English   中英

如何使JFrame透明/不透明而无需设置Undecorated(true)

[英]How do I make a JFrame transparent / Opaque without having to setUndecorated(true)

  1. 有没有一种方法可以将JFrame设置为透明,同时又不影响Button /文本?
  2. 如果没有,如何不使用.setUndecorated(true)使JFrame透明?
  3. 这是一个完全不同的问题,但是我将如何添加渐变作为背景色而不是将其设置为一种纯色呢?
  4. 单击此处查看程序运行时JFrame的外观!

 class PlayAgain extends JPanel
{
    private JFrame nextFrame;
    public PlayAgain()
    {
        nextFrame = new JFrame("Tic-Tac-Toe");
        nextFrame.setSize(250,125);
        nextFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new GridBagLayout());
        nextFrame.add(panel);

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(10,10,10,10);

        class ButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                nextFrame.dispose();
                frame.dispose();
                XorOFrameGRID obj = new XorOFrameGRID();
            }
        }

        class ClickNo implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                frame.dispose();
                nextFrame.dispose();
            }
        }

        //CREATING BUTTONS & LABELS
        JLabel WLT;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 3;

        JLabel title = new JLabel("Would you like to play again?");
        if (isWin() == 1)
        {
            WLT = new JLabel("YOU WON!");
            panel.add(WLT,c);
        }
        else if (isWin() == 2)
        {
            WLT = new JLabel("YOU LOST!");
            panel.add(WLT,c);
        }
        else if (isWin() == 3)
        {
            WLT = new JLabel("YOU TIED!");
            panel.add(WLT,c);
        }
        JLabel or = new JLabel("or");

        JButton yes = new JButton("Yes");
        ActionListener listener1 = new ButtonListener();
        yes.addActionListener(listener1);

        JButton no = new JButton("No");
        ActionListener listener2 = new ClickNo();
        no.addActionListener(listener2);

        c.gridwidth = 0;
        //1ST COLUMN

        c.anchor = GridBagConstraints.LINE_END;
        c.weighty = 10;
        c.gridx = 0;
        c.gridy = 2;
        panel.add(no,c);

        //2ND COLUMN

        //adds "or"
        c.anchor = GridBagConstraints.CENTER;
        c.gridx = 1;
        c.gridy = 2;
        panel.add(or,c);

        //adds title
        c.weighty = 0;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 1;
        panel.add(title,c);

        //3RD COLUMN
        c.gridwidth = 0;
        c.weighty = 3; // changes weight
        c.anchor = GridBagConstraints.LINE_START;
        c.gridx = 2;
        c.gridy = 2;
        panel.add(yes,c);

        nextFrame.pack();
        nextFrame.setLocationRelativeTo(null);
        nextFrame.setResizable(false);
        nextFrame.setVisible(true);
        nextFrame.toFront();
    }

这是透明JFrame的源代码:

public class Example extends JFrame {
    public Example() {
        super("TranslucentWindowDemo");
        setLayout(new GridBagLayout());
        setSize(500,300);
        setLocationRelativeTo(null);
        setUndecorated(true);
        getContentPane().setBackground(Color.blue);

        JButton Close = new JButton("Close");
        add(Close);
        ActionListener al;
        al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        };
        Close.addActionListener (al);
    }

    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
        pack();
    }                     

    public static void main(String args[]) {
       GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
       GraphicsDevice gd = ge.getDefaultScreenDevice();

        if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
            System.err.println("Translucency is not supported");
            System.exit(0); 
        }
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {

            java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Example e = new Example();
                e.setOpacity(0.55f);
                e.setVisible(true);
            }
        });
    }         
}

对于不透明度,您可以尝试Pane.setOpaque(false); 对于内容,您应该更改为setBackground(new Color(0, 0, 0, 0));

你可以在这里阅读更多

暂无
暂无

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

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