如何使帧显示50次,持续1秒,并在每次显示时消失 [英] how to make a frame display 50 times for 1 second and disappear each time it's displayed

查看:120
本文介绍了如何使帧显示50次,持续1秒,并在每次显示时消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的JFrame在我的屏幕尺寸内显示它,它应该显示带有消息的框架然后消失1秒然后重新出现50次这里是我的代码到目前为止:

I need my JFrame to display itself within the dimensions of my screen and it should display the frame with the message then disappear for 1 second then reappear 50 times here's my code so far:

 import javax.swing.*;
 import java.awt.*;
 import java.util.*;


public class OurMain {

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

    Random rand = new Random();

    int z = rand.nextInt(sSize.width);
    int c = rand.nextInt(sSize.height);

    int x = (int)((Math.random()* w) - z);
    int y = (int)((Math.random()* h) - 100);
    for(int g = 0; g < 51; g++){
    JFrame f = new MyFrame(z, 100, x, y);
    f.pack();
    f.setVisible(true);
    f.setVisible(false);
    }
}

}

这是我的JFrame:

and here's my JFrame:

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class MyFrame extends JFrame{

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

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("R and T's Main Frame");
    setSize(width, height);
    setLocation(x, y);


    int v;
    v = 5 + (int)(Math.random() * (1 - 5) + 1);

    JLabel label = new JLabel("Software Engineering");
    label.setFont(new Font("Impact", Font.BOLD, height/v));
    label.setForeground(Color.BLUE);
    getContentPane().add(label);
    setVisible(true);
}

}

所以是的,我有两个问题1.框架似乎有时会出现在我的屏幕分辨率和2的尺寸范围内。第二个是循环的迭代50次,持续1秒应该是显示然后消失1秒并继续显示50帧出现。

So yeah I have two problems 1. the frame seems to go outside of the screen sometimes when it should be displayed within the dimensions of my screen resolution and 2. The second is the iteration of the loop 50 times and for 1 second it should be displayed and then disappear for 1 second and continue on like that for 50 frames appearing.

推荐答案

首先,我觉得我必须告诉你:

你的之前的帖子。

我们希望人们付出更多努力,尝试一下,然后问,明确他们被困的地方。我们不是来做你的功课,我们来帮忙,所以首先,亲自试试。看起来你没有对我做任何研究。如果你想获得帮助,这必须改变,因为人们注意到并且不喜欢它。

Here we expect people to put more effort, try things and then ask, explining where they are stuck. We are not here to do your homework, we are here to help, so first, try it out by yourself. It doesn't look like you did any kind of research to me. If you want to get help, this must change because people notice that and don't like it.

即使你没有得到结果,也要发布你尝试的内容,结果是什么,你期望它们是什么等等。

Even if you did not get results, post what you tried, what were the results, what you expected them to be etc.

代码

这可以通过多种方式完成,因为您似乎并不关心它,也没有添加任何方式我喜欢这样做的方法,我自己做了。

This could be done in several ways, since you did not seem to care about it, neither added any preffered way to do it, I did it on my own.

有些可能性是:


  • 创建一个JFrame并在想要将其设置为可见时移动它的位置

  • Create one JFrame and move it's location whenever you want to set it visible

在循环的每次迭代中创建一个JFrame(可能比前一个慢?

Create a JFrame on every iteration of the loop (likely to be slower than the previous one?)

还有更多......

Many more...

我更喜欢第一种方法,所以我做了那个。这就是它的作用:

I liked the first approach more, so I did that one. This is what it does:


  • 出现和消失50次

  • Appear and dissappear 50 times

当它可见并且不可见等待1秒

When it goes visible and invisible waits 1 second

每当它被设置为可见时,帧的位置和大小会发生变化

Everytime it's set as visible the location and size of the frame changes

它将永远在屏幕内

OurMain .java

public class OurMain {

    public static void main(String[] args) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;

        Random rand = new Random();

        MyFrame frame = new MyFrame();

        for (int g = 0; g < 50; g++) {
            int frameWidth = rand.nextInt(screenSize.width);
            int frameHeight = rand.nextInt(screenSize.height);

            int frameLocationX = rand.nextInt(screenWidth - frameWidth);
            int frameLocationY = rand.nextInt(screenHeight - frameHeight);

            frame.setSize(frameWidth, frameHeight);
            frame.setLocation(frameLocationX, frameLocationY);
            frame.setVisible(true);

            try {
                Thread.sleep(1000); // Waits 1000 miliseconds = 1 second
            } catch (InterruptedException e) { }

            frame.setVisible(false);

            try {
                Thread.sleep(1000); // Waits 1000 miliseconds = 1 second
            } catch (InterruptedException e) { }
        }
    }
}

MyFrame.java

public class MyFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    private JLabel label;

    public MyFrame() {
        super();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("R and T's Main Frame");

        label = new JLabel("Software Engineering");
        label.setForeground(Color.BLUE);
        getContentPane().add(label);
    }
}

我没有评论代码,这很简单。无论如何,如果您需要一些解释,请随时在评论中提出。

I did not comment the code, it's pretty simple. Anyway, if you need some explanation feel free to ask in the comments.

编辑:评论问题的答案

Answers to questions on comments


  • 如果我不想改变它的高度并保持100,frameLocationY = 100?

  • If I didn't want to change the height of it and keep it 100, frameLocationY = 100 ?

不,这会改变它的位置,你需要改变 frameHeight 到100

No, that would change its location, you need to change frameHeight to 100


  • interruptedException为空,因为什么都没有错误

  • interruptedException is empty cause nothing would go wrong

在这种情况下,这是真的,但是,线程可以停止(参见 interrupt())因此它会执行 catch中的内容 brakets

In this case, that's true, however, a Thread can be stopped (see interrupt()) and therefore it would execute what it's inside the catch brakets


  • 一个名为thread.sleep的对象

  • an object called thread.sleep

public static void Thread.sleep(long milis)抛出InterruptedExcept ion Thread 类中的函数。由于它是静态的,因此不需要创建 Thread 的实例

public static void Thread.sleep(long milis) throws InterruptedException is a function inside the Thread class. Since it's static, it doesn't need an instance of Thread to be created


  • f.pack();所以在框架内文本与随机框架大小成比例

  • the f.pack(); so that within the frame the text is proportionate to the random frame size

如果我是没错, pack()会给帧的每个组件提供所需的所有空间,但是,因为文本太少而且框架太大,给出了它的优先大小的标签最终会包装它,调整我们之前设置的大小

If I am not mistaken, what pack() does is give all the space they need to each component of the frame, however, since the text is so little and the frame so big, giving the label it's preffered size would eventually wrap it, resizing the size we set before


  • 每次它都是设置为可见,框架的位置和大小会发生变化,因为它的内容与其大小成比例

  • Everytime it's set as visible the location and size of the frame changes'' as does the content in it is proportionate to its size

然而,这更棘手,因为你只有一个JLabel,我们可以计算占据帧的所有空间所需的字体值。查看链接并尝试将其添加到代码中,告诉我们您是否可以

That's more tricky, nevertheless, since you only have a JLabel, we can calculate the value of the font it needs to take all the space of the frame. Look this link and try to add that to the code, tell us if you can't

这篇关于如何使帧显示50次,持续1秒,并在每次显示时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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