0

I have the problem that I get a error from this two lines

System.out.println(tw.getX());
System.out.println(tw.getY());

because the scope of tw is wrong. How do I set it properly?

package misc;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;

public class TranslucentJframe extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JButton button;

    public TranslucentJframe() {
        super("Frame");
        setLayout(new GridBagLayout());

        setSize(485,860);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        button = new JButton("Frame is set!");
        button.addActionListener(new SetFrame());

        this.getContentPane().add(button);
    }

    public class SetFrame implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            System.out.println(tw.getX());
            System.out.println(tw.getY());

        }

    }

    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() {
            public void run() {
                TranslucentJframe tw = new TranslucentJframe();

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

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

            }
        });

    }
}
Georodin
  • 191
  • 1
  • 2
  • 14

1 Answers1

1

You need to pass the TranslucentJframe into the SetFrame listener.

Change button.addActionListener(new SetFrame());

To this button.addActionListener(new SetFrame(this));

Then define the field in SetFrame:

public class SetFrame implements ActionListener {
    private TranslucentJframe tw;

    public SetFrame(TranslucentJframe tw) {
        this.tw = tw;
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println(tw.getX());
        System.out.println(tw.getY());
    }
}
Matt
  • 828
  • 8
  • 25
  • 1
    This is the correct answer. I was looking into this also and found that after doing this you will get a `java.awt.IllegalComponentStateException: The frame is decorated` error. To resolve put in `tw.setUndecorated(true)` before setting opacity. See also here: https://stackoverflow.com/questions/6259269/is-the-java-tutorials-translucent-window-example-giving-trouble-to-those-playing – Ross Cradock Apr 24 '20 at 17:37
  • thanks a lot, im new to java and had trouble understanding a common approach – Georodin Apr 25 '20 at 10:02