Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to add a command to a button java

// 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 :: material motion android navigation arch 
Java :: activity show 1 time in android studio java 2022 
Java :: bukkit detect block that nowbal hit 
Java :: what is this code (long millis=System.currentTimeMillis(); java.sql.Date date=new java.sql.Date(millis); 
Java :: error attribute fabattached not found 
Java :: array erstellen java 
Java :: java object array initialization 
Java :: how to increase a variable once java 
Java :: exmple of methods in java 
Java :: equality primitives java 
Java :: java commands in different packages not working 
Java :: illegal expression 
Java :: clear array in java 
Java :: convert java code to kotlin online 
Java :: how to use protobuf in java 
Java :: problem solving using recursion in java 
Java :: scrollbar in textarea java 
Java :: varargs java 
Java :: java lambda function 
Java :: call activity method from adapter 
Java :: find power of number in method java 
Java :: xml button color not changing 
Java :: start an activity in adapter 
Java :: sololearn bowling game 
Java :: how do I test the reverse method in java using jest 
Java :: ciclo for para percorere duas listas java 
Sql :: postgres get running query 
Sql :: sql disable trigger 
Sql :: print oracle 
Sql :: mysql return 0 if null 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =