Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to add a command to a button

// import the just the libraries that you gonna need dont be like me
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class App {
    public static void main(String[] args) {
        JFrame f = new JFrame("Test");	//make a window
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600,300);
        
        JButton bhelloworld = new JButton("Hello World!");	//make the button

        ActionListener l = new ActionListener() {	//make the listener (l is its name)
            public void actionPerformed(ActionEvent event) {	//make the command here
                if (event.getActionCommand() == "func1") {	//to specify what the button gonna do put this if (the "event.getActionCommand() == "func1"" is checking the setActionCommand() command)
                    System.out.println("Button pressed");
                }
            }
        };	//these ; are needed :v

        bhelloworld.setSize(new Dimension(40,40));
        f.add(bhelloworld);	//add your button
        bhelloworld.setActionCommand("func1");	//optional
        bhelloworld.addActionListener(l);	//determine which action listener you gonna use

        f.setVisible(true);	//show the screen
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java reverse a array 
Java :: java cors issue 
Java :: factorial function in java recursion 
Java :: try catch not working java 
Java :: throws multiple exception 
Java :: document inserted succesfully but not present in the collection java 
Java :: force orientation programmatically android 
Java :: set countdown timer to play audio file android studio 
Java :: excel data formatter in java 
Java :: hide tablayout tab android 
Java :: java schleife abbrechen 
Java :: Java Enum toString() 
Java :: compare string length of two strings 
Java :: java var keyword with example 
Java :: connectionpool in jdbc 
Java :: random years java 
Java :: how does minus works in Java 
Java :: how to clear scoreboard with offline players java 
Java :: How to Access Elements of an Array in Java? 
Java :: open bottomsheet from adapter java 
Java :: domain validation test spring boot 
Java :: how to put array in array list stack overflow 
Java :: session-less control using java 
Java :: break statement in Java switch...case 
Java :: javax big int validation in spring boot 
Java :: try catch still prints java 
Java :: tutorialedge working with docker 
Java :: java 8 retrieve all list from object into single list and ignore duplicates 
Java :: java hashset api 
Java :: how do I change the opacity of a JButton 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =