Button demo

PHOTO EMBED

Wed Dec 27 2023 06:11:55 GMT+0000 (Coordinated Universal Time)

Saved by @dsce

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonDemo  
{   private JFrame frame;
    private JLabel label;
	private JButton b1, b2; 
	
	public ButtonDemo()
	{
		frame = new JFrame("A Simple Swing App");

		Toolkit tk = frame.getToolkit();
		Dimension dim = tk.getScreenSize();
		int width = (int)dim.getWidth();
		int height = (int)dim.getHeight();
		frame.setSize(width, height);
		
		frame.setLayout(new FlowLayout());	
		
		label = new JLabel("I show the button text");
		label.setFont(new Font("Verdana", Font.BOLD, 18));	
		frame.add(label);
		
		b1 = new JButton("The First Button");
		b1.setFont(new Font("Verdana", Font.BOLD, 18));
		frame.add(b1);
		b1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae)
			{   label.setText(b1.getText()+" is pressed!"); 	}
		});
		
		b2 = new JButton("The Second Button");
		b2.setFont(new Font("Verdana", Font.BOLD, 18));
		frame.add(b2);
		b2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae)
			{   label.setText(b2.getText()+" is pressed!"); 	}
		});
				
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public static void main(String[] args)
	{   new ButtonDemo();
	}
}
content_copyCOPY