Wednesday, October 4, 2023

Button click event using Java Swing


Button click event using Java Swing





Write a Java program to create a frame with label and button. When a button clicked some text to be printed in label. 


Output





Source code
;

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

public class ButtonClick extends JFrame {

    public ButtonClick() {
        setTitle("Print Some Text");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());           

        JLabel label=new JLabel("");
        JButton click=new JButton("Click Here");

       click.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){
        label.setText("Hello everyone! Welcome");
}
        });


        add(label);
        add(click);

    }

    public static void main(String[] args) {

            ButtonClick ck  = new ButtonClick();
            ck.setVisible(true);
    }
}

    


No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...