Java program to change label text on button click

Change label text

This program illustrate the concept of Java program to change label text on button click.

Program

scan.java

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

public class scan 
{
	
	 public static void main(String arg[])
	 {
		 
		 final JLabel l1;
	      JButton t1;
           JFrame f1=new JFrame("Scanfcode");
	     l1=new JLabel("www.scanfcode.java");  
		 t1=new JButton("Change Text");
	     l1.setBounds(100,100, 150,30); 
         f1.add(l1);
		 
		 t1.setBounds(100,130,150,30);
		 t1.addActionListener(new ActionListener()
		 {
			 public void actionPerformed(ActionEvent e)
			 {
				 l1.setText("Welcome to scanfcode");
			 }
		 }
		 );
         f1.add(t1);             
        f1.setSize(400,400);  
		   f1.setLayout(null);
       f1.setVisible(true);	 
    	   
		
	}
	
	
	
}

Command

change label text


Before click

change label text


After click

change label text


Comment below if you are facing any problem and subscribe to our newsletter for latest updates.


Also read

Java first applet program to display WELCOME TO SCANFCODE
Java applet program to display numerical values
Java program to display sum of two input numbers in applet


Summary
Java program to change label text on button click
Article Name
Java program to change label text on button click
Description
The program illustrate the concept of Java swing program to change label text on button click
Author
Publisher Name
Scanfcode
Publisher Logo

Leave a Reply

Your email address will not be published. Required fields are marked *