2013-05-01 29 views
0

我正在創建一個小程序,發生的所有事情是它會打開顯示「有多少流派?然後出現一個textField。我輸入一個數字,然後回車,但沒有任何事情發生! (我沒有得到任何錯誤,但沒有任何反應)試圖創建一個簡單的小程序,但它不會貫穿整個程序

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
public class appletPracticw extends Applet implements ActionListener { 
TextField numG; 
TextField g ; 
TextField numS; 
TextField sog; 
private int number; 
private int numberOfSongs; 
String gener; 
String songName; 
public void go(){ 
    numG= new TextField(5); 
    numS= new TextField(5); 
    g= new TextField(5); 
    sog= new TextField(5); 
    numG.addActionListener(this); 
    g.addActionListener(this); 
    sog.addActionListener(this); 
    numS.addActionListener(this); 
    Tracker t=new Tracker(); 
    add(new Label("How many genres are there? ")); add(numG); 
    for(int i=0;i<number;i++){ 
     catogories c=new catogories(); 
     add(new Label("Name of genere: ")); add(g); 
     t.addCatogory(c,gener); 
    } 
    for(int x=0;x<number;x++){ 
     add(new Label("How many songs are there in "+t.getCatogories().get(x).getGenere())); add(numS); 
     for(int i=0;i<numberOfSongs;i++){ 
      Songs s=new Songs(); 
      add(new Label("The name of song "+(i+1)+" is")); add(sog); 
      t.getCatogories().get(x).addSong(s, songName); 
     } 
    } 
} 
public void actionPerformed(ActionEvent e) { 
    if(e.getSource()==numG){ 
     String num=numG.getText(); 
     number=Integer.parseInt(num); 
    } 
    if(e.getSource()==numS){ 
     String num=numS.getText(); 
     numberOfSongs=Integer.parseInt(num); 
    } 
    if(e.getSource()==g){ 
     gener=g.getText(); 
    } 
    if(e.getSource()==sog){ 
     songName=sog.getText(); 
    } 
} 
public void init() { 
    go(); 

} 公共appletPracticw(){

}}

+0

*「試圖創建一個簡單的小程序..」*「簡單的小程序」是一個重言式。有關詳細信息,請參閱[爲什麼CS教師應停止教授小程序?](http://programmers.stackexchange.com/questions/196499/why-should-cs-teachers-stop-teaching-applets)。如果你想'簡單',編碼一個基於Swing的框架。 – 2013-05-02 04:11:53

回答

2

領域number與價值0初始化。

您構建小程序,添加標籤和文本字段。它看起來像你假設程序在該行的等待用戶輸入:

add(new Label("How many genres are there? ")); add(numG); 

在現實中,它只是創建用戶界面和不等待輸入。

因此,兩個for循環被執行,但是因爲number仍然是0,所以從不輸入循環。

你應該做的,而不是爲執行實際操作(在你的情況下,這是通過添加新的標籤和字段改變GUI)在actionPerformed方法,使類別標籤和輸入框都後創建用戶有進入了流派的數量。第二個循環也必須完成。

+0

等我不能延長2班。 – 2013-05-01 21:18:18

+0

@LouisB不客氣,我希望你明白了。爲什麼使用'ActionListener'的原因是用戶輸入可以在任何時候完成,並且您的程序需要以某種方式對其進行反應。 – Sky 2013-05-01 21:24:17

+0

等待,但我仍然不明白如何初始化一個基本結構,然後發生什麼事後繼續?像我應該把我的for循環在actionlistner? – 2013-05-01 21:33:38

相關問題