我只是想知道为什么当我使用Frame Application并混合对话框和动画时,文本框被完全删除。
这是我的代码:
/* The "SpaceTravels" class.
Date: March 2015
Author Barrington Reid
Description: This program asks user for identification,
then will display a spaceship travelling through the night sky using Graphics.
The spaceship is controlled by a slow setting, a medium setting and a fast setting.
*/
import javax.swing.*; //Allows JOptionPane to work
import java.awt.*;
public class SpaceTravels extends JFrame
{
ImageIcon backGnd, rckt;
int x = 0; //Setting the x variable as 0
int y = 600; //Setting the y variable as 600
int xSpeed = 10; //Setting the xSpeed as 10
int ySpeed = 10; //Setting the ySpeed as 10
int number = 0; //stores the verification number
public SpaceTravels ()
{
super ("SpaceTravels"); // Set the frame's name
setSize (1360, 730); // Set the frame's size
backGnd = new ImageIcon ("night.gif"); //Night background
rckt = new ImageIcon ("orangerocket.png"); //Rocket
setDefaultCloseOperation (EXIT_ON_CLOSE); //Close the program by clicking exit
setVisible (true); // Show the frame
} // Constructor
public void paint (Graphics g)
{
for (int i = 0 ; i < 1000000 ; i = i + 1)
{
backGnd.paintIcon (this, g, 0, 0);
rckt.paintIcon (this, g, x, y);
y = y + ySpeed;
for (int j = 0 ; j < 1000000 ; j = j + 1)
{
double k = 1;
Math.pow (k, 2);
}
if ((y > 600) || (y < 50))
{
ySpeed = ySpeed * -1;
}
for (int k = 0 ; k < 1000000 ; k = k + 1)
{
double l = 1;
Math.pow (l, 2);
}
if ((x > 600 || (x < 50))
{
xSpeed = xSpeed * -1;
}
}
} // paint method
public static void main (String[] args)
{
new SpaceTravels (); // Create a SpaceTravels frame
String name; //Stores names
int tries = 0; //Stores the amount of tries
String verCode = "1234567"; //Verification Code is 1234567
String code = "1234567"; //Verification Code is 1234567
JOptionPane.showMessageDialog (null, "Hello, and welcome to the Canadian Space Agency's Spacecraft Control and Simulation Program!"); //Welcome
JOptionPane.showMessageDialog (null, "Your verification code is 1234567."); //This is the verification code
name = JOptionPane.showInputDialog (null, "For safety purposes, please enter your name"); //Asking for user name
verCode = JOptionPane.showInputDialog (null, "Now, " + name + ", please enter your verification code."); //Enter the verification code: 1234567
while ((verCode.equalsIgnoreCase ("1234567") != true) && (tries < 3)) //3 tries to get this correct
{
verCode = JOptionPane.showInputDialog (null, "You have entered the wrong verification code, please try again.");
tries = tries + 1; //Tries goes up once each time this loop occurs.
}
if (code.equals (verCode))
{
String[] options = new String[] //This is a code that I found on (http://stackoverflow.com/questions/1257420/making-a-joptionpane-with-4-options)
{
"Slow", "Medium", "Fast" //Speed options, slow, medium and fast
}
;
int response = JOptionPane.showOptionDialog (null, "What would you like the velocity of the rocket to be?", "Rocket speed",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options [0]); //This is the name of the box, and what the box displays inside.
}
else
{
JOptionPane.showMessageDialog (null, "Sorry, " + name + ", you have unsuccessfully identified your session. Please try again in 10 minutes.");
}
}
// main method
} // SpaceTravels class
当我删除图形代码时,消息框的工作方式应该是正确的。
答案 0 :(得分:1)
paint
而不调用super.paint
会破坏父类提供的功能,并且不会导致绘画工作方式出现问题。不是从JFrame
延伸并覆盖它的绘图例程(它足够复杂),而是从JPanel
开始并覆盖它的paintComponent
方法(调用{{ 1}}在你做任何自定义绘画之前)。有关详细信息,请参阅Painting in AWT and Swing和Performing Custom Painting。例如......
super.paintComponent