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