Search code examples
javaawtactionlistenercounting

I am writing a program using awt pakage in java but my components are not getting added


I am writing a program using the AWT package in which I also implemented ActionListner which shows the number of clicks I made on the button.

package awtlistenerdemo;
import java.awt.*;
import java.awt.event.*;
public class AwtListenerDemo implements ActionListener {
  TextField t1 = new TextField(30);
  Button b;
  int count = 0;
  Frame f;
  AwtListenerDemo() {
    f = new Frame("Action Listener Example");
    b = new Button("Ok");
    f.setLayout(null);
    b.setBounds(100, 100, 60, 20);
    t1.setBounds(100, 200, 80, 30);
    f.add(b);
    f.add(t1);
    b.addActionListener(this);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == b) {
      count++;
      t1.setText("Button Clicked" + count + "Times");
    }
  }
  public static void main(String[] args) {
    Frame f = new Frame("Action Listener Example");
    f.setVisible(true);
    f.setSize(300, 300);

  }

}

Solution

  • The main method never constructs an AwtListenerDemo so all you see is the standard, blank frame created in that method. Once that problem is fixed, some of the statements in the main method need to be moved into the constructor and applied to the frame it creates.

    This is the result:

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    
    public class AwtListenerDemo implements ActionListener {
    
        TextField t1 = new TextField(30);
        Button b;
        int count = 0;
        Frame f;
    
        AwtListenerDemo() {
            f = new Frame("Action Listener Example");
            b = new Button("Ok");
            f.setLayout(null);
            b.setBounds(100, 100, 60, 20);
            t1.setBounds(100, 200, 80, 30);
            f.add(b);
            f.add(t1);
            b.addActionListener(this);
            // ADD this!
            f.setSize(300,300);
            // then set it VISIBLE
            f.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == b) {
                count++;
                t1.setText("Button Clicked" + count + "Times");
            }
        }
    
        public static void main(String[] args) {
            // Change the main code to..
            new AwtListenerDemo();
        }
    }
    

    General Tips

    1. See this answer for many good reasons to abandon AWT components in favor of Swing. Added to that is that few people in this day & age have ever used AWT (so can't help with problems) or if they did, have forgotten the finer details (so give faulty advice). As mentioned in the linked answer, Swing is built on AWT, but has differences that need to be unlearned when making the transition. Seriously, start with Swing or go directly to Java-FX (an entirely different GUI toolkit which is based on neither Swing nor AWT). Warning: This is the one & only time I will be helping to fix broken AWT code for you. (3)
    2. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space. The (beginnings of these) problems can be seen even in that short example, in that: a) Either the button, the text field, or both are not center aligned. If the frame is resized even slightly, it would be both. b) The size guess for the text field is also wrong - it is not long enough to display the text!
    3. Relevant to (1), there are still some problems with this code. I did the minimum needed to answer the question and will do no more.