如何使帧显示50次,持续1秒,并在每次显示时消失

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

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:

 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帧出现。

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

自上一篇文章以来你刚写了5行。

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

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

这可以通过几种方式完成,因为你似乎并不关心它,也没有添加任何优先的方法,我自己做了。

一些可能性是:

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

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

  • 还有很多…

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

  • 出现并消失50次

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

  • 每次将其设置为可见时,框架的位置和大小都会发生变化

  • 它总是在屏幕内

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

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

编辑: 评论问题的答案

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

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

  • interruptedException is empty cause nothing would go wrong

在这种情况下,这是真的,但是,一个Thread可以被停止(参见interrupt() ),因此它将执行它在catch brakets中的内容

  • an object called thread.sleep

public static void Thread.sleep(long milis) throws InterruptedExceptionThread类中的一个function 由于它是静态的,因此不需要创建Thread的实例

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

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

  • 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,我们可以计算占据帧的所有空间所需的字体值。 查看此链接并尝试将其添加到代码中,告诉我们您是否可以