2015-03-31 55 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?? 
    }); 
}