0

I am theoretically not new on Java, but a beginner on practice. I am experimenting with JavaFX and to begin with, I am implementing a GUI where the window (Stage) has the size of the screen.

The problem: I want to use this screen size object  which is type of Rectangle and named screenBorders in another class(called Second). Here is my simple code:

public class Test extends Application 
{
    public static void main(String[] args) 
    { launch(args);}

    public static Rectangle screenBorders;

     @Override
     public void start(Stage primaryStage) 
     {
      Group grp=new Group();
      Scene sc = new Scene(grp);
      primaryStage.setScene(sc);
      primaryStage.show();

      screenBorders= new Rectangle (
      Screen.getPrimary().getBounds().getWidth(),
      Screen.getPrimary().getBounds().getHeight()
      );

      Second second= new Second();

     }

}

New file, new class

class Second extends Group
{

   public Second () 
   {
    Rectangle second =new Rectangle(screenBorders.getWidth(), screenBorders.getHeight());
   }
//code, code, code, don't know yet what code i will put...
}

In this second class, I can't catch/use the screensize defined in the first class.( I want to use screenBorders)

I've tried to declare screenBorders with public and static keyword in the first and main class called Test, thinking that it could be used anywhere in my package, but it does'not work.

Does anyone have an idea about how to link an object with other classes ?

4

2 回答 2

1

如果要从Second类中访问静态变量,则需要直接声明第一个类名:

Rectangle second = new Rectangle(
    Test.screenBorders.getWidth(), 
    Test.screenBorders.getHeight());

在类的开头使用 import static Second

import static Test.*;
于 2013-04-28T18:02:14.463 回答
0

试试看Stage.setFullScreen(true),文档在这里

于 2013-04-28T17:36:12.790 回答