如何随机设定一个JFrame用一个简单的消息 [英] How to randomly set a JFrame with a simple message

查看:340
本文介绍了如何随机设定一个JFrame用一个简单的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经签出了其他的帖子里面没有帮助就在这里,我试图让我的框架与它的消息随机出现在屏幕上的一个区域,但是当我运行它,它说x和Y不能被解析为一个变量,这里的code:

So I've checked out the other post which didn't help on here, I'm trying to get my frame with it's message to randomly appear on an area on the screen but when I run it, it says x and y cannot be resolved to a variable, here's the code:

    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);
}

}

推荐答案

您应该只是添加一些属性,并更换一些事情:搜索结果
MyFrame:

You should just add some attributes and replace some things:

MyFrame:

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);
    }
}

OurMain:结果

OurMain:

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(); 是不是一个好主意,因为你的屏幕宽度和放大器; heigth拥有少一点则2 ^ 32个像素,所以,我将限制它以如:结果



However, random.nextInt(); is not a good idea, because your screen width&heigth has a little less then 2^32 pixels... so I would limit it to e.g.:

 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);
    }
}

这篇关于如何随机设定一个JFrame用一个简单的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆