在另一节课上,我如何读出框架的x和y?java作用域
我有一个问题,我从这两行得到一个错误在另一节课上,我如何读出框架的x和y?java作用域,java,scope,Java,Scope,我有一个问题,我从这两行得到一个错误 System.out.println(tw.getX()); System.out.println(tw.getY()); 因为tw的范围是错误的。如何正确设置它 package misc; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import static java
System.out.println(tw.getX());
System.out.println(tw.getY());
因为tw
的范围是错误的。如何正确设置它
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);
}
});
}
}
您需要将
半透明jframe
传递到SetFrame
侦听器中
更改按钮.addActionListener(新的SetFrame())代码>
添加ActionListener(新的SetFrame(this))代码>
然后在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());
}
}
这是正确的答案。我也对此进行了研究,发现在执行此操作后,您将得到一个java.awt.IllegalComponentStateException:框架被修饰
错误。要在设置不透明度之前解析put intw.setUndercorated(true)
。这里也可以看到:非常感谢,我是java新手,在理解通用方法时遇到了困难