有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在java小程序上显示列表中的单词

首先是的,我知道我没有使用布局管理器来创建我的按钮,我也没有使用swing,因为我只是一个初学者,所以我使用了不同的方法。但是,我基本上创建了6个按钮,第一个按钮(b1)允许用户将输入的单词添加到一个不可见的存储列表中,但现在我想在按下第二个按钮(b2)时在java小程序屏幕上显示输入的单词

public class Ex2 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");
    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);
        textf.setText(null);
    }

    if (e.getSource() == b2 ){
        }}}

共 (0) 个答案