2015-03-31 25 views
0

我只是想知道为什么当我使用框架应用程序和混合对话框和动画时,文本框被完全切掉。为什么我的动画剪切了我的消息框?

这里是我的代码:

/* 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 

当我删除了图形代码,该消息框工作,他们应该工作的方式。

+2

你覆盖'paint'(顶层容器),但不能调用'super.paint',打破了油漆链 – MadProgrammer 2015-03-31 01:17:09

+0

我不是一个非常熟练的程序员,因为我几天前才开始工作。所以我不确定什么super.paint的意思是 – 2015-03-31 01:20:41

+0

“Swing程序应该重写'paintComponent()'而不是重写'paint()'。” - [* AWT和Swing中绘制:绘制方法*](http:// www.oracle.com/technetwork/java/painting-140037.html#callbacks)。 – trashgod 2015-03-31 01:23:55

回答

1
  • 重写paint无需调用super.paint首先将打破由父类所提供的功能,并会导致怎样的绘画作品没有问题的结束。而不是从JFrame延伸并覆盖它的绘画例程(足够复杂),从JPanel开始并覆盖它的paintComponent方法(在执行任何自定义绘画之前调用super.paintComponent)。有关更多详细信息,请参见Painting in AWT and SwingPerforming Custom Painting
  • 确保您在事件分派线程的上下文中运行您的UI。见Initial Threads更多细节

例如...

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       ex.printStackTrace(); 
      } 

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

     // Create main UI?? 
    }); 
} 
相关问题