如何使JFrame透明/不透明而不必setUndecorated(true)

时间:2015-06-03 04:30:20

标签: java swing jframe gradient transparent

  1. 有没有办法可以将JFrame设置为透明,同时仍然保持按钮/文字不受影响?
  2. 如果没有,如何在不使用JFrame的情况下使.setUndecorated(true)透明?
  3. 这是一个完全不同的问题,但我如何添加渐变作为背景颜色而不是将其设置为纯色?
  4. 点击here查看程序运行时JFrame的样子!
  5.  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();
        }
    

2 个答案:

答案 0 :(得分:0)

以下是透明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);
            }
        });
    }         
}

答案 1 :(得分:0)

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

您可以阅读更多here

相关问题