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 her
First, I feel I have to tell you:
You just wrote 5 lines since your previous post.
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.
Code
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.
Some possibilities are:
Create one JFrame and move it's location whenever you want to set it visible
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:
Appear and dissappear 50 times
When it goes visible and invisible waits 1 second
Everytime it's set as visible the location and size of the frame changes
It will always be inside the screen
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.
EDIT: Answers to questions on comments
If I didn't want to change the height of it and keep it 100, frameLocationY = 100 ?
No, that would change its location, you need to change frameHeight
to 100
interruptedException is empty cause nothing would go wrong
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
an object called thread.sleep
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
the f.pack(); so that within the frame the text is proportionate to the random frame size
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
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