0
  1. Is there a way that I can set the JFrame to be transparent while still leaving the Buttons / text unaffected?
  2. If not, how can I make the JFrame transparent without using .setUndecorated(true)?
  3. This is a totally different question, but how would I go about adding a gradient as the background color instead of having it be set to one solid color?
  4. Click here to see what the JFrame looks like when the program runs!

 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();
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Novice
  • 1
  • 1
    1: Yes, make a transparent window, then add a transparent JPanel onto, overriding it paintComponent and filling it with a transparent color; 2: No; 3: See answer one – MadProgrammer Jun 03 '15 at 04:51
  • 1: [example](http://stackoverflow.com/questions/21771215/how-to-draw-images-on-transparent-window/21771615#21771615) – MadProgrammer Jun 03 '15 at 04:53

2 Answers2

0

Here is the Source code for transparent 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);
            }
        });
    }         
}
Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29
Programmer
  • 445
  • 4
  • 12
0

For the Opacity, you can try Pane.setOpaque(false); And for the contents you should change to setBackground(new Color(0, 0, 0, 0));

You can read more here

XOR-Manik
  • 493
  • 1
  • 4
  • 19