1

First of all yes I know I have not used a layout manager in order to create my buttons and I am not using swing, as I am only a beginner so I did it a different way. However, I basically created 6 buttons, the first button (b1)allows the user to add their inputted word into a stored list which cannot be seen, but now I want to display that inputted word on the java applet screen when they press the second button (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 ){
        }}}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
JayJay Simpson
  • 155
  • 3
  • 15
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Nov 03 '15 at 02:24
  • *"..as I am only a beginner so I did it a different way.."* The way you've approached this will end up being harder. 1) By not using a layout manager the applet will potentially fail on the next machine or the next version of the JRE, making maintenance harder. 2) Few of us have ever used AWT & those of us that did have forgotten a lot of the finer details! As a result you have a much greater chance of getting help if using a more modern GUI toolkit (e.g. Swing). – Andrew Thompson Nov 03 '15 at 02:29
  • I appreciate that and I'll keep that in mind next time – JayJay Simpson Nov 03 '15 at 09:35

0 Answers0