0% found this document useful (0 votes)
24 views1 page

Import Import Import Import Import Public Class Extends Public New New

This Java code creates a simple GUI application with a button. It imports necessary classes for Swing components and event handling. It defines a MainClass that extends JPanel and adds a button to itself. The main method creates a JFrame, adds the MainClass panel to it, sets properties, and makes the frame visible, displaying the button. An anonymous inner class is used to listen for button clicks and print a message.

Uploaded by

nube2008
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)
24 views1 page

Import Import Import Import Import Public Class Extends Public New New

This Java code creates a simple GUI application with a button. It imports necessary classes for Swing components and event handling. It defines a MainClass that extends JPanel and adds a button to itself. The main method creates a JFrame, adds the MainClass panel to it, sets properties, and makes the frame visible, displaying the button. An anonymous inner class is used to listen for button clicks and print a message.

Uploaded by

nube2008
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/ 1

import java.awt.event.

ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JPanel {

public MainClass() {

JButton btn1 = new JButton("Button1");


btn1.addActionListener(new ButtonListener());
add(btn1);

public static void main(String[] args) {


JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}

class ButtonListener implements ActionListener {


ButtonListener() {
}

public void actionPerformed(ActionEvent e) {


if (e.getActionCommand().equals("Button1")) {
System.out.println("Button1 has been clicked");
}
}
}

You might also like