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

Java Button Action Example Code

The document describes a Java program that creates a graphical user interface with a button. When the button is clicked or mouse buttons are pressed/released, it prints messages to the console indicating the button interaction. The program imports Swing and AWT classes, creates a JFrame containing a JButton, and adds both an ActionListener and MouseListener to handle button interactions and print responses.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
9 views1 page

Java Button Action Example Code

The document describes a Java program that creates a graphical user interface with a button. When the button is clicked or mouse buttons are pressed/released, it prints messages to the console indicating the button interaction. The program imports Swing and AWT classes, creates a JFrame containing a JButton, and adds both an ActionListener and MouseListener to handle button interactions and print responses.
Copyright
© Attribution Non-Commercial (BY-NC)
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

import import import import import import import import

[Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link]; [Link];

import [Link]; import [Link]; import [Link]; public class ButtonActionSample { public static void main(String args[]) { JFrame frame = new JFrame("Button Sample"); [Link](JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Select Me"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { [Link]("I was selected."); } }; MouseListener mouseListener = new MouseAdapter() { public void mousePressed(MouseEvent mouseEvent) { int modifiers = [Link](); if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { // Mask may not be set properly prior to Java 2 // See [Link]() for workaround [Link]("Left button pressed."); } if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) { [Link]("Middle button pressed."); } if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) { [Link]("Right button pressed."); } } public void mouseReleased(MouseEvent mouseEvent) { if ([Link](mouseEvent)) { [Link]("Left button released."); } if ([Link](mouseEvent)) { [Link]("Middle button released."); } if ([Link](mouseEvent)) { [Link]("Right button released."); } [Link](); } }; [Link](actionListener); [Link](mouseListener); Container contentPane = [Link](); [Link](button, [Link]); [Link](300, 100); [Link](true); } }

You might also like