Display a frame with two JButton components, which are used to change the text on the frame's title bar. - Java Swing

Java examples for Swing:JFrame

Description

Display a frame with two JButton components, which are used to change the text on the frame's title bar.

Demo Code

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame implements ActionListener {
  JButton b1;/* w  w  w  .  j ava2s. c  om*/
  JButton b2;

  public Main() {
    super("Title Bar");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    b1 = new JButton("A");
    b2 = new JButton("B");
    b1.addActionListener(this);
    b2.addActionListener(this);
    FlowLayout flow = new FlowLayout();
    setLayout(flow);
    add(b1);
    add(b2);
    pack();
    setVisible(true);
  }

  public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == b1) {
      setTitle("A");
    } else if (source == b2) {
      setTitle("B");
    }
    repaint();
  }

  public static void main(String[] arguments) {
    Main frame = new Main();
  }
}

Related Tutorials