如何随机设置一个带有简单消息的JFrame

How to randomly set a JFrame with a simple message

所以我检查了另一个 post 在这里没有帮助,我试图让我的框架和它的消息随机出现在屏幕上的一个区域但是当我 运行 它说 x 和 y 不能解析为变量,代码如下:

    public class MyFrame extends JFrame{
       MyFrame(int width, int height, int x, int y){
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setTitle("R and Ts Main Frame");
       setSize(width, height);

       Random random = new Random();
       x = random.nextInt();
       y = random.nextInt();
       setLocation(x, y);

       JLabel label = new JLabel("Random Message");
       label.setFont(new Font("Impact", Font.BOLD|Font.PLAIN, height/3));
       label.setForeground(Color.BLUE);
       getContentPane().add(label);
}

}

这是我的主要内容:

 public class OurMain {

  public static void main(String[] args) {
    Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
    int w = sSize.width;
    JFrame f = new MyFrame(w/3, 100, x, y); //my errors are underlined here under the x and y
    f.setVisible(true);
}

}

这将解决您的问题。 declare/create xy 之前没有用过,但你不需要,所以直接在本地使用即可。

public class OurMain {
    public static void main(String[] args) {
        Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
        int w = sSize.width;

        Random random = new Random();
        int x = random.nextInt(sSize.width);
        int y = random.nextInt(sSize.height);

        JFrame f = new MyFrame(w/3, 100, x, y);
    }
}

public class MyFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    public MyFrame(int width, int height, int x, int y) {
        super();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("R and Ts Main Frame");
        setSize(width, height);

        setLocation(x, y);

        JLabel label = new JLabel("Random Message");
        label.setFont(new Font("Impact", Font.BOLD | Font.PLAIN, height / 3));
        label.setForeground(Color.BLUE);

        getContentPane().add(label);
        setVisible(true);
    }
}

结果(每次加载到不同的位置):

您应该只添加一些属性并替换一些东西:

我的框架:

public class MyFrame extends JFrame {

    int x;
    int y;

    MyFrame(int width, int height, int x, int y) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("R and Ts Main Frame");
        setSize(width, height);
        this.x = x;
        this.y = y;
        setLocation(x, y);

        JLabel label = new JLabel("Random Message");
        label.setFont(new Font("Impact", Font.BOLD | Font.PLAIN, height / 3));
        label.setForeground(Color.BLUE);
        getContentPane().add(label);
    }
}

我们的主线:

public class OurMain {

    public static void main(String[] args) {
        Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
        int w = sSize.width;
        Random random = new Random();
        int x = random.nextInt();
        int y = random.nextInt();
        JFrame f = new MyFrame(w / 3, 100, x, y); //my errors are underlined here under the x and y
        f.setVisible(true);
    }
}


编辑:
但是,random.nextInt(); 不是一个好主意,因为您的屏幕宽度和高度略小于 2^32 像素...所以我将其限制为例如:

 public static void main(String[] args) {
       Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
       int w = sSize.width;
       int h = sSize.height;
       int x = (int)((Math.random()* w) - w/3);
       int y = (int)((Math.random()* h) - 100);
       System.out.println(x + " " + y);
       JFrame f = new MyFrame(w / 3, 100, x, y); 
       f.setVisible(true);
 }

编辑之编辑:

public class OurMain {

    public static void main(String[] args) {
        Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
        int w = sSize.width;
        int h = sSize.height;

        Random rand1 = new Random();

        int randomWidth = rand1.nextInt(w);
        int randomHeight = rand1.nextInt(h);

        Random rand2 = new Random(); // I would still use rand1

        int randomX = rand2.nextInt(w - randomWidth + 1);
        int randomY = rand2.nextInt(h - randomHeight + 1);

        JFrame f = new MyFrame(randomWidth, randomHeight, randomX, randomY);
        f.setVisible(true);
    }
}