有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使一帧在1秒内显示50次,每次显示时消失

我需要我的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帧那样继续出现


共 (1) 个答案

  1. # 1 楼答案

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

    自从你的previous帖子之后,你只写了5行

    在这里,我们希望人们付出更多努力,尝试一些事情,然后提问,解释他们的困境。我们不是来帮你做作业的,我们是来帮助你的,所以首先,你自己试试看。看起来你没有对我做任何研究。如果你想得到帮助,这一点必须改变,因为人们注意到了这一点,并且不喜欢它

    即使你没有得到结果,也要公布你的尝试、结果、你的预期等等

    代码

    这可以通过几种方式来实现,因为你似乎并不关心它,也没有添加任何首选的方式来实现它,我是自己完成的

    一些可能性是:

    • 创建一个JFrame并移动其位置,只要您想将其设置为可见

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

    • 还有很多

    我更喜欢第一种方法,所以我选择了那一种。它就是这样做的:

    • 出现和消失50次

    • 当它变为可见和不可见时,等待1秒

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

    • 它将始终位于屏幕内

    我们的家。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) { }
            }
        }
    }
    

    我的框架。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()),因此它将执行它在catchbrakets

    • 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,我们可以计算字体的值,它需要占用框架的所有空间。查看this链接并尝试将其添加到代码中,如果无法