繁体   English   中英

试图创建一个简单的小程序,但它不会贯穿整个程序

[英]Trying to create a simple applet but it won't run through the whole program

我正在创建一个applet,所有发生的事情就是打开显示“有多少类型?” 然后出现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();

public appletPracticw(){

}}

字段number初始化为值0

您构建小程序,添加标签和文本字段。 看起来您假设程序在此行等待用户输入:

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

实际上,它只是创建用户界面而不是等待输入。

因此执行了两个for循环,但由于number仍为0 ,因此永远不会输入循环。

您应该做的是在actionPerformed方法中执行实际操作(在您的情况下,这是通过添加新标签和字段来更改GUI),以便在用户输入类型数量创建类别标签和输入字段。 对于第二个循环也必须这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM