如何创建用于在Java applet中存储字符串的列表

时间:2015-11-01 17:08:33

标签: java list applet

我正在尝试创建一个包含用户输入的所有字符串的列表。我不确定下面的代码是否是创建字符串的正确方法,但是,我创建了字符串列表,一个名为x的字符串,它将被分配给textf以检索用户输入但是当我按下按钮时没有任何反应。我想确保下面的代码是按下“b1”按钮时将用户输入添加到秘密存储列表的正确方法

import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.util.ArrayList; 
import java.util.List;

public class test 1 extends Applet implements ActionListener {

List<String> wordList = new ArrayList <String>();
Font fonttext;
TextField textf;
String x; 
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;

public void init(){
    setBackground(Color.lightGray); 
    fonttext = new Font("Times New Roman", Font.BOLD, 24);

    textf = new TextField("", 40);

    add(textf);
    b1 = new Button ("Add word to list");
    b2 = new Button ("Display words from list with that letter");
    b3 = new Button ("Search list for this word(show occurence)");
    b4 = new Button ("Remove first occurence of this word");
    b5 = new Button ("Remove all occurence of this word");
    b6 = new Button ("Clear the list ");

     b1.addActionListener(this);
     b2.addActionListener(this);
     b3.addActionListener(this);
     b4.addActionListener(this);
     b5.addActionListener(this);
     b6.addActionListener(this);

     add(b1);
     add(b2);
     add(b3);
     add(b4);
     add(b5);
     add(b6);
}

public void paint(Graphics g){
    this.b1.setLocation(20,600);
    this.b2.setLocation(150,600);
    this.b3.setLocation(400,600);
    this.b4.setLocation(680,600);
    this.b5.setLocation(930,600);
    this.b6.setLocation(1170,600);

}

public void actionPerformed(ActionEvent e){
    if (e.getSource() == b1 ){
        x = textf.getText();
        wordList.add(x);
    }

}

}

0 个答案:

没有答案