首先是的,我知道我没有使用布局管理器来创建我的按钮而且我没有使用swing,因为我只是一个初学者,所以我以不同的方式做到了。但是,我基本上创建了6个按钮,第一个按钮(b1)允许用户将输入的单词添加到无法看到的存储列表中,但现在我想在java applet屏幕上按下第二个按钮时显示输入的单词按钮(b2)。
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 ){
}}}