Java: How to Create Button

If you don’t know how to make a frame, I refer you to my old blog on it.

Use your already made method “main” in your “Main” class, this method is where you will stock all your information for your “Visual application” aspect. To make a button, we will use the Swift method “JButton”. Start by writing this is your method:

JButton button = new JButton(“My first button!”);
button.setBounds(150, 60, 200, 30);
frame.add(button); // This display the button “My First Button”.

Now add a Label to display the action of the button.

JLabel label = new JLabel();
label.setBounds(175, 10, 200, 30);
frame.add(label); // This display the label.

And finally add an action to the button:

button.addActionListener(new ActionListener(){
@Override
public static void actionPerformed(ActionEvent e){
label.setText(“This is my first Label”); // This change the text of the label
}
});

And when you Run your application that will show your frame with your button and your label:
capture-decran-2017-02-10-a-13-40-14

Other Tutorial for your Level:
Java: How to Create a Label
Java: How To Create an Organize New Project
Java: Create a Basic Application

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s