首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何在另一个类中读出我的帧的x和y?java作用域

我如何在另一个类中读出我的帧的x和y?java作用域
EN

Stack Overflow用户
提问于 2020-04-25 01:12:26
回答 1查看 14关注 0票数 0

我有一个问题,我从这两行得到一个错误

代码语言:javascript
复制
System.out.println(tw.getX());
System.out.println(tw.getY());

因为tw的范围是错误的。我如何正确设置它?

代码语言:javascript
复制
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);

            }
        });

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-25 01:25:52

您需要将TranslucentJframe传递给SetFrame侦听器。

更改button.addActionListener(new SetFrame());

添加到此button.addActionListener(new SetFrame(this));

然后在SetFrame中定义该字段

代码语言:javascript
复制
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());
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61413778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档