AWT Applications: Login Form and Tic-Tac-Toe

Site Admin · 11 Sep 2026 · 1 views

A Login Form

Put labels, text fields and a button into a panel. Register an ActionListener that checks the credentials:

Button login = new Button("Login");
login.addActionListener(e -> {
    String u = user.getText().trim();
    String p = pass.getText();
    status.setText(("admin".equals(u) && "1234".equals(p))
        ? "Welcome!" : "Invalid credentials");
});

AWT login form demo

Tic-Tac-Toe Game

A 3x3 GridLayout of buttons toggling X and O, with a turn variable:

Button[] cells = new Button[9];
for (int i = 0; i < 9; i++) {
    cells[i] = new Button(" ");
    int idx = i;
    cells[i].addActionListener(e -> {
        cells[idx].setLabel(turn ? "X" : "O");
        cells[idx].setEnabled(false);
        turn = !turn;
    });
    add(cells[i]);
}

Tic tac toe written in AWT

Key Points

  • Small apps combine layout managers, controls and listeners.
  • Use getText / setLabel for reading and updating controls.
  • GridLayout is perfect for board-style games.
Share this post:

Comments (0)

Please login or register to comment.