Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
34 views2 pages

JButton ActionListener Example

This document defines a class called MyFrame that extends JFrame and implements ActionListener. It creates a JButton and JLabel, adds an action listener to the button, and overrides the actionPerformed method. When the button is clicked, it disables the button, makes the label visible, and prints a message to the console. The frame sets properties on the button like icon, text position, font, and colors. It adds the button and label to the frame, makes the frame visible, and closes it when closed.

Uploaded by

Momed Groove
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

JButton ActionListener Example

This document defines a class called MyFrame that extends JFrame and implements ActionListener. It creates a JButton and JLabel, adds an action listener to the button, and overrides the actionPerformed method. When the button is clicked, it disables the button, makes the label visible, and prints a message to the console. The frame sets properties on the button like icon, text position, font, and colors. It adds the button and label to the frame, makes the frame visible, and closes it when closed.

Uploaded by

Momed Groove
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

4 – JButton

//**************************************************

public class Main{

public static void main(String[] args) {

// JButton = a button that performs an action when clicked on

new MyFrame();

}
}

//**************************************************

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

public class MyFrame extends JFrame implements ActionListener


{

JButton button;
JLabel label;

MyFrame()
{

ImageIcon icon = new ImageIcon("point.png");


ImageIcon icon2 = new ImageIcon("face.png");

label = new JLabel();


label.setIcon(icon2);
label.setBounds(150, 250, 150, 150);
label.setVisible(false);

button = new JButton();


button.setBounds(100, 100, 250, 100);
button.addActionListener(this);
button.setText("I'm a button!");

button.setFocusable(false);
button.setIcon(icon);
button.setHorizontalTextPosition(JButton.CENTER);
button.setVerticalTextPosition(JButton.BOTTOM);
button.setFont(new Font("Comic Sans",Font.BOLD,25));
button.setIconTextGap(-15);
button.setForeground(Color.cyan);
button.setBackground(Color.lightGray);
button.setBorder(BorderFactory.createEtchedBorder());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(500,500);
this.setVisible(true);
this.add(button);
this.add(label);
}

@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
System.out.println("poo");
button.setEnabled(false);
label.setVisible(true);
}
}
}

//**************************************************

You might also like