This is my current code; how can I edit the main method to get it to run?
import javax.swing.JFrame;
public class Virus {
public static void main(String[] args) {
Thread virus = new Thread();
}
}
class pop implements Runnable
{
public void run()
{
int x = 1000;
int y = 1000;
JFrame popup = new JFrame();
popup.setLocation(x, y);
popup.setVisible(true);
javax.swing.JOptionPane.showMessageDialog(popup, "Virus fun time");
}
}
You're close. A Runnable
needs a thread to run it, and you pass an instance of the Runnable
into the thread. So using the Thread(Runnable)
constructor:
Thread virus = new Thread(new pop());
Then you start the thread:
virus.start();
Then you need to wait for it to finish, via join
:
virus.join();
(You'll need to handle the exception that may throw.)
This tutorial may help you with these fundamentals.
But , heed what @MadProgrammer commented on the question:
Don't modify, change or create UI components outside the Event dispatching thread!
This tutorial may help you there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.