2011-07-31 45 views
0

我有一個運行GUI的小程序。我想從我的其他程序調用這個GUI。我知道我需要將這個applet變成一個應用程序。我有一個init()和一個actionPerformed(ActionEvent ae)。我該怎麼做?將java applet更改爲java應用程序

我的代碼:

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import java.io.*; 

public class survey extends Applet implements ActionListener 
{ 
    private TextField question; 
    private Button enter, start; 
    int count = 0; 
    int a = 0; 
    int b = 0; 
    int c = 0; 
    int d = 0; 
    String text, input; 

    private Label intro1, intro2, intro3; 
    private Label qone1, qone2, qone3, qone4, qone5, qone6, qone7, qone8, qone9, qone10, qone11, qone12; 
    private Label qtwo1, qtwo2, qtwo3, qtwo4, qtwo5, qtwo6, qtwo7, qtwo8, qtwo9, qtwo10, qtwo11, qtwo12; 
    private Label qthree1, qthree2, qthree3, qthree4, qthree5, qthree6, qthree7, qthree8, qthree9, qthree10, qthree11, qthree12; 
    private Label qfour1, qfour2, qfour3, qfour4, qfour5, qfour6, qfour7, qfour8, qfour9, qfour10, qfour11, qfour12; 
    private Label qfive1, qfive2, qfive3, qfive4, qfive5, qfive6, qfive7, qfive8, qfive9, qfive10, qfive11, qfive12; 
    private Label qsix1, qsix2, qsix3, qsix4, qsix5, qsix6, qsix7, qsix8, qsix9, qsix10, qsix11, qsix12; 
    private Label qseven1, qseven2, qseven3, qseven4, qseven5, qseven6, qseven7, qseven8, qseven9, qseven10, qseven11, qseven12; 
    private Label qeight1, qeight2, qeight3, qeight4, qeight5, qeight6, qeight7, qeight8, qeight9, qeight10, qeight11, qeight12; 
    private Label qnine1, qnine2, qnine3, qnine4, qnine5, qnine6, qnine7, qnine8, qnine9, qnine10, qnine11, qnine12; 
    private Label qten1, qten2, qten3, qten4, qten5, qten6, qten7, qten8, qten9, qten10, qten11, qten12; 
    private Label qeleven1, qeleven2, qeleven3, qeleven4, qeleven5, qeleven6, 
    private Label finish1, finish2, finish3; 


    public void init() 
    { 
     setLayout(null); 
     start = new Button ("Start"); 
     question = new TextField(10); 
     enter = new Button ("Enter"); 
     if (count == 0) 
     { 
      setBackground(Color.yellow); 
      intro1 = new Label("Target Advertising", Label.CENTER); 
      intro1.setFont(new Font("Times-Roman", Font.BOLD, 16)); 
      intro2 = new Label("Welcome to this questionnaire. First, we would like to know more about your personal preferences."); 
      intro3 = new Label("For each question, Input a rating between 0-9 (zero = least interested, 9 = most interested) in the text box. Click enter for next question."); 

      add(intro1); 
      add(intro2); 
      add(intro3); 
      intro1.setBounds(0,0,800,20); 
      intro2.setBounds(15,20,800,20); 
      intro3.setBounds(15,40,800,20); 
      add(start); 
      start.setBounds(370,60,70,23); 
      start.addActionListener(this); 
     } 
     if(count == 1) 
     { 
      setBackground(Color.yellow); 
      qone1 = new Label("Question 1", Label.LEFT); 
      qone1.setFont(new Font("Times-Roman", Font.BOLD, 16)); 
      qone2 = new Label("How much do you like action movies?"); 
      qone3 = new Label("0"); 
      qone4 = new Label("1"); 
      qone5 = new Label("2"); 
      qone6 = new Label("3"); 
      qone7 = new Label("4"); 
      qone8 = new Label("5"); 
      qone9 = new Label("6"); 
      qone10 = new Label("7"); 
      qone11 = new Label("8"); 
      qone12 = new Label("9"); 


      add(qone1); 
      add(qone2); 
      add(qone3); 
      add(qone4); 
      add(qone5); 
      add(qone6); 
      add(qone7); 
      add(qone8); 
      add(qone9); 
      add(qone10); 
      add(qone11); 
      add(qone12); 


      qone1.setBounds(15,0,800,20); 
      qone2.setBounds(15,20,800,15); 
      qone3.setBounds(15,60,800,15); 
      qone4.setBounds(15,80,800,15); 
      qone5.setBounds(15,100,800,15); 
      qone6.setBounds(15,120,800,15); 
      qone7.setBounds(15,140,800,15); 
      qone8.setBounds(15,160,800,15); 
      qone9.setBounds(15,180,800,15); 
      qone10.setBounds(15,200,800,15); 
      qone11.setBounds(15,220,800,15); 
      qone12.setBounds(15,240,800,15); 


      add(question); 
      add(enter); 
      question.setBounds(15,260,70,15); 
      enter.setBounds(90,260,110,23); 
      question.addActionListener(this); 
      enter.addActionListener(this); 
     } 
     if (count == 2) 
     { 

      qtwo1 = new Label("Question 2", Label.LEFT); 
      qtwo1.setFont(new Font("Times-Roman", Font.BOLD, 16)); 
      qtwo2 = new Label("How much do you like Science Fiction?"); 
      qtwo3 = new Label("0"); 
      qtwo4 = new Label("1"); 
      qtwo5 = new Label("2"); 
      qtwo6 = new Label("3"); 
      qtwo7 = new Label("4"); 
      qtwo8 = new Label("5"); 
      qtwo9 = new Label("6"); 
      qtwo10 = new Label("7"); 
      qtwo11 = new Label("8"); 
      qtwo12 = new Label("9"); 


      add(qtwo1); 
      add(qtwo2); 
      add(qtwo3); 
      add(qtwo4); 
      add(qtwo5); 
      add(qtwo6); 
      add(qtwo7); 
      add(qtwo8); 
      add(qtwo9); 
      add(qtwo10); 
      add(qtwo11); 
      add(qtwo12); 


      qtwo1.setBounds(15,0,800,20); 
      qtwo2.setBounds(15,20,800,15); 
      qtwo3.setBounds(15,60,800,15); 
      qtwo4.setBounds(15,80,800,15); 
      qtwo5.setBounds(15,100,800,15); 
      qtwo6.setBounds(15,120,800,15); 
      qtwo7.setBounds(15,140,800,15); 
      qtwo8.setBounds(15,160,800,15); 
      qtwo9.setBounds(15,180,800,15); 
      qtwo10.setBounds(15,200,800,15); 
      qtwo11.setBounds(15,220,800,15); 
      qtwo12.setBounds(15,240,800,15); 


      add(question); 
      add(enter); 
      question.setBounds(15,260,70,15); 
      enter.setBounds(90,260,110,23); 
      question.addActionListener(this); 
      enter.addActionListener(this); 
     } 
     if(count == 3) 
     { 

      qthree1 = new Label("Question 3", Label.LEFT); 
      qthree1.setFont(new Font("Times-Roman", Font.BOLD, 16)); 
      qthree2 = new Label("How much do you like comedy?"); 
      qthree3 = new Label("0"); 
      qthree4 = new Label("1"); 
      qthree5 = new Label("2"); 
      qthree6 = new Label("3"); 
      qthree7 = new Label("4"); 
      qthree8 = new Label("5"); 
      qthree9 = new Label("6"); 
      qthree10 = new Label("7"); 
      qthree11 = new Label("8"); 
      qthree12 = new Label("9"); 


      add(qthree1); 
      add(qthree2); 
      add(qthree3); 
      add(qthree4); 
      add(qthree5); 
      add(qthree6); 
      add(qthree7); 
      add(qthree8); 
      add(qthree9); 
      add(qthree10); 
      add(qthree11); 
      add(qthree12); 


      qthree1.setBounds(15,0,800,20); 
      qthree2.setBounds(15,20,800,15); 
      qthree3.setBounds(15,60,800,15); 
      qthree4.setBounds(15,80,800,15); 
      qthree5.setBounds(15,100,800,15); 
      qthree6.setBounds(15,120,800,15); 
      qthree7.setBounds(15,140,800,15); 
      qthree8.setBounds(15,160,800,15); 
      qthree9.setBounds(15,180,800,15); 
      qthree10.setBounds(15,200,800,15); 
      qthree11.setBounds(15,220,800,15); 
      qthree12.setBounds(15,240,800,15); 


      add(question); 
      add(enter); 
      question.setBounds(15,260,70,15); 
      enter.setBounds(90,260,110,23); 
      question.addActionListener(this); 
      enter.addActionListener(this); 
     } 
     if(count == 4) 
     { 


      qfour1 = new Label("Question 4", Label.LEFT); 
      qfour1.setFont(new Font("Times-Roman", Font.BOLD, 16)); 
      qfour2 = new Label("How much do you like luxary cars?"); 
      qfour3 = new Label("0"); 
      qfour4 = new Label("1"); 
      qfour5 = new Label("2"); 
      qfour6 = new Label("3"); 
      qfour7 = new Label("4"); 
      qfour8 = new Label("5"); 
      qfour9 = new Label("6"); 
      qfour10 = new Label("7"); 
      qfour11 = new Label("8"); 
      qfour12 = new Label("9"); 


      add(qfour1); 
      add(qfour2); 
      add(qfour3); 
      add(qfour4); 
      add(qfour5); 
      add(qfour6); 
      add(qfour7); 
      add(qfour8); 
      add(qfour9); 
      add(qfour10); 
      add(qfour11); 
      add(qfour12); 


      qfour1.setBounds(15,0,800,20); 
      qfour2.setBounds(15,20,800,15); 
      qfour3.setBounds(15,60,800,15); 
      qfour4.setBounds(15,80,800,15); 
      qfour5.setBounds(15,100,800,15); 
      qfour6.setBounds(15,120,800,15); 
      qfour7.setBounds(15,140,800,15); 
      qfour8.setBounds(15,160,800,15); 
      qfour9.setBounds(15,180,800,15); 
      qfour10.setBounds(15,200,800,15); 
      qfour11.setBounds(15,220,800,15); 
      qfour12.setBounds(15,240,800,15); 


      add(question); 
      add(enter); 
      question.setBounds(15,260,70,15); 
      enter.setBounds(90,260,110,23); 

      question.addActionListener(this); 
      enter.addActionListener(this); 
     } 
     if(count == 5) 
     { 


      qfive1 = new Label("Question 5", Label.LEFT); 
      qfive1.setFont(new Font("Times-Roman", Font.BOLD, 16)); 
      qfive2 = new Label("How much do you like trucks?"); 
      qfive3 = new Label("0"); 
      qfive4 = new Label("1"); 
      qfive5 = new Label("2"); 
      qfive6 = new Label("3"); 
      qfive7 = new Label("4"); 
      qfive8 = new Label("5"); 
      qfive9 = new Label("6"); 
      qfive10 = new Label("7"); 
      qfive11 = new Label("8"); 
      qfive12 = new Label("9"); 


      add(qfive1); 
      add(qfive2); 
      add(qfive3); 
      add(qfive4); 
      add(qfive5); 
      add(qfive6); 
      add(qfive7); 
      add(qfive8); 
      add(qfive9); 
      add(qfive10); 
      add(qfive11); 
      add(qfive12); 


      qfive1.setBounds(15,0,800,20); 
      qfive2.setBounds(15,20,800,15); 
      qfive3.setBounds(15,60,800,15); 
      qfive4.setBounds(15,80,800,15); 
      qfive5.setBounds(15,100,800,15); 
      qfive6.setBounds(15,120,800,15); 
      qfive7.setBounds(15,140,800,15); 
      qfive8.setBounds(15,160,800,15); 
      qfive9.setBounds(15,180,800,15); 
      qfive10.setBounds(15,200,800,15); 
      qfive11.setBounds(15,220,800,15); 
      qfive12.setBounds(15,240,800,15); 


      add(question); 
      add(enter); 
      question.setBounds(15,260,70,15); 
      enter.setBounds(90,260,110,23); 


      question.addActionListener(this); 
      enter.addActionListener(this); 
     } 







     if(count == 7) 
     { 

      finish1 = new Label("Thank You." , Label.CENTER); 
      finish1.setFont(new Font("Times-Roman", Font.BOLD, 50)); 
      finish2 = new Label("Questionnaire Completed.", Label.CENTER); 
      finish2.setFont(new Font("Times-Roman", Font.BOLD, 50)); 

      add(finish1); 
      add(finish2); 

      finish1.setBounds(0,200,800,60); 
      finish2.setBounds(0,300,800,60); 
     } 
    } 
    public void actionPerformed(ActionEvent ae) 
    { 
     String button = ae.getActionCommand(); 
     text = question.getText(); 
     b = 0; 
     c = 0; 






     if (count == 6) 
     { 
      input = text.toUpperCase(); 
      remove(enter); 
      remove(question); 
      question.setText(""); 
      remove(qsix1); 
      remove(qsix2); 
      remove(qsix3); 
      remove(qsix4); 
      remove(qsix5); 
      remove(qsix6); 
      remove(qsix7); 
      remove(qsix8); 
      remove(qsix9); 
      remove(qsix10); 
      remove(qsix11); 
      remove(qsix12); 

      try{ 
        FileWriter fstream = new FileWriter("lets3.txt",true); 
        BufferedWriter out = new BufferedWriter(fstream); 
        out.write(new String(input)); 
        out.write("\n"); 
        out.close(); 
        } 

       catch (Exception e){ 
         System.err.println("Error: " + e.getMessage()); 
        } 
      if(input.equals("OL")) 
      { 
       b = 1; 
       count = 7; 
       init(); 
      } 
      else 
      { 
       b = 2; 
       count = 7; 
       init(); 
      } 

     } 
     if (count == 5) 
     { 

      input = text.toUpperCase(); 
      remove(enter); 
      remove(question); 
      question.setText(""); 
      remove(qfive1); 
      remove(qfive2); 
      remove(qfive3); 
      remove(qfive4); 
      remove(qfive5); 
      remove(qfive6); 
      remove(qfive7); 
      remove(qfive8); 
      remove(qfive9); 
      remove(qfive10); 
      remove(qfive11); 
      remove(qfive12); 

      try{ 
        FileWriter fstream = new FileWriter("lets3.txt",true); 
        BufferedWriter out = new BufferedWriter(fstream); 
        out.write(new String(input)); 
        out.write("\n"); 
        out.close(); 
        } 

       catch (Exception e){ 
         System.err.println("Error: " + e.getMessage()); 
        } 
      if(input.equals("BR")) 
      { 
       b = 1; 
       count = 6; 
       init(); 
      } 
      else 
      { 
       b = 2; 
       count = 6; 
       init(); 
      } 

     } 
     if (count == 4) 
     { 
      input = text.toLowerCase(); 
      remove(enter); 
      remove(question); 
      question.setText(""); 
      remove(qfour1); 
      remove(qfour2); 
      remove(qfour3); 
      remove(qfour4); 
      remove(qfour5); 
      remove(qfour6); 
      remove(qfour7); 
      remove(qfour8); 
      remove(qfour9); 
      remove(qfour10); 
      remove(qfour11); 
      remove(qfour12); 

      try{ 
        FileWriter fstream = new FileWriter("lets3.txt",true); 
        BufferedWriter out = new BufferedWriter(fstream); 
        out.write(new String(input)); 
        out.write("\n"); 
        out.close(); 
        } 

       catch (Exception e){ 
         System.err.println("Error: " + e.getMessage()); 
        } 
      if(input.equals("no")) 
      { 
       b = 1; 
       count = 5; 
       init(); 
      } 
      else 
      { 
       b = 2; 
       count = 5; 
       init(); 
      } 

     } 
     if (count == 3) 
     { 
      input = text.toLowerCase(); 
      remove(enter); 
      remove(question); 
      question.setText(""); 
      remove(qthree1); 
      remove(qthree2); 
      remove(qthree3); 
      remove(qthree4); 
      remove(qthree5); 
      remove(qthree6); 
      remove(qthree7); 
      remove(qthree8); 
      remove(qthree9); 
      remove(qthree10); 
      remove(qthree11); 
      remove(qthree12); 

      try{ 
        FileWriter fstream = new FileWriter("lets3.txt",true); 
        BufferedWriter out = new BufferedWriter(fstream); 
        out.write(new String(input)); 
        out.write("\n"); 
        out.close(); 
        } 

       catch (Exception e){ 
         System.err.println("Error: " + e.getMessage()); 
        } 
      if(input.equals("black")) 
      { 
       b = 1; 
       count = 4; 
       init(); 
      } 
      else 
      { 
       b = 2; 
       count = 4; 
       init(); 
      } 

     } 
     if (count == 2) 
     { 
      input = text.toLowerCase(); 
      remove(enter); 
      remove(question); 
      question.setText(""); 
      remove(qtwo1); 
      remove(qtwo2); 
      remove(qtwo3); 
      remove(qtwo4); 
      remove(qtwo5); 
      remove(qtwo6); 
      remove(qtwo7); 
      remove(qtwo8); 
      remove(qtwo9); 
      remove(qtwo10); 
      remove(qtwo11); 
      remove(qtwo12); 

      try{ 
        FileWriter fstream = new FileWriter("lets3.txt",true); 
        BufferedWriter out = new BufferedWriter(fstream); 
        out.write(new String(input)); 
        out.write("\n"); 
        out.close(); 
        } 

       catch (Exception e){ 
         System.err.println("Error: " + e.getMessage()); 
        } 
      if(input.equals("yes")) 
      { 
       b = 1; 
       count = 3; 
       init(); 
      } 
      else 
      { 
       b = 2; 
       count = 3; 
       init(); 
      } 
     } 
     if (count == 1) 
     { 
      input = text.toUpperCase(); 
      remove(enter); 
      remove(question); 
      question.setText(""); 
      remove(qone1); 
      remove(qone2); 
      remove(qone3); 
      remove(qone4); 
      remove(qone5); 
      remove(qone6); 
      remove(qone7); 
      remove(qone8); 
      remove(qone9); 
      remove(qone10); 
      remove(qone11); 
      remove(qone12); 



      try{ 
       FileWriter fstream = new FileWriter("lets3.txt",true); 
       BufferedWriter out = new BufferedWriter(fstream); 
       out.write(new String(input)); 
       out.write("\n"); 
       out.close(); 
       } 

      catch (Exception e){ 
        System.err.println("Error: " + e.getMessage()); 
       } 

      if(input.equals("i")) 
      { 
       b = 1; 
       count = 2; 
       init(); 
      } 
      else 
      { 
       b = 1; 
       count = 2; 
       init(); 
      } 
     } 
     if (count == 0) 
     { 
      remove(intro1); 
      remove(intro2); 
      remove(intro3); 
      remove(start); 
      count = 1; 
      init(); 
     } 
     this.validate(); 
    } 
}    

回答

0

一般情況下,你會想改變所有的非Swing組件的Swing組件。對於事件,它們對於applet和swing應用程序大致相同。希望能幫助到你。

2

只需添加一個main()方法,爲您的applet創建一個Frame,將該applet添加到該框架,然後調用applet的init()和start()方法。例如,請參閱我的Mandelbrot.java:http://unixshell.jcomeau.com/src/java/com/jcomeau/Mandelbrot.java

此方法的一個優點是,它可以與任何現有的小程序一起使用,以使其可以用作小程序或應用程序。如果您使用Swing組件,請使用JFrame,否則它應該幾乎相同。

+0

你有一個程序可能作爲一個小程序很有用,所以這看起來是最好的方法,因爲你可以學習重構,這是一個非常重要的課程,所以重構它是一個applet,它的作用是相同的,但是通過添加新的方法以及可能的類來改進代碼。通過移動可能在多個應用程序中使用的所有代碼,將它移到它自己的類中,這會使它更容易與應用程序和applet一起使用。 –

3

誠實地說,您需要從頭開始重新編寫程序,以便您可以合併面向對象技術,數組,集合以及Java所提供的其他優勢。我建議:

  • 首先,因爲你的代碼顯示了一系列的問題,並提示響應,不要硬編碼在代碼中的問題,但讓他們中的部分數據。讓您的程序以包含問題的文本文件讀取。這將允許您在不更改代碼的情況下更改問題或添加問題。
  • 創建一個非GUI問題類,其中包含問題和用戶響應,GUI用作其「模型」。
  • 創建問題對象的ArrayList。
  • 對於您的GUI,代碼爲JPanel,而不是applet或JFrame。這將使您可以選擇在JFrame或JApplet中使用GUI,或者根據需要在JDialog中嵌入或嵌入另一個JPanel中。
  • 如果您需要交換顯示面板,請考慮使用CardLayout來達到此目的。
  • 但是,如果您要做的只是更改問題的文本,然後在JLabel中顯示問題文本,並且想要更改問題文本,請在JLabel上調用setText(...)傳入新問題文本。
  • 使用用戶友好的佈局管理器來簡化您在GUI中佈置組件的工作。
  • 你目前的代碼有很多不必要的重複。 ArrayLists等陣列和集合的使用將會刪除許多這樣的redudancies,並使的調試和升級變得更容易,而則更容易。
  • 正如其他人所說的那樣,正如我在之前的評論中所述,您應該向上移動到Swing庫,因爲它比您當前使用的AWT gui庫更加靈活和健壯。 Swing教程將向您展示您需要知道如何創建漂亮的Swing程序。
+0

+1進行誠實,有建設性的評估;編寫良好的代碼將很容易轉換。 – trashgod

0

我不知道你在問什麼,但如果你想成爲一個窗口,裏面所有你需要做的是這樣的:

public Object[] startNewSurvey() 
    { 
     java.awt.Frame f = new java.awt.Frame("You're title here."); 
     f.setSize(new Dimension(<Width>, <Height>)); 
     f.setResizable(false); 

     survey s = new survey(); 
     s.init(); 
     f.add(s); 

     f.setVisible(true); 
     return new Object[] { f, s }; 
    } 

這隻會例如一個全新的框架,並把另一在該框架中的調查課程的新實例,然後顯示它。它還返回包含新框架和調查實例的2個大小的Object []數組。希望有所幫助。

+0

我想在另一個應用程序中調用這個applet。所以當我運行其他應用程序時,此GUI將彈出並同時運行。 –

相關問題