Create buttons using the JButton class in Java - ProgrammerTech


Create buttons using the JButton class in Java

  • Share this:
Create buttons using the JButton class in Java

Class JButton for creating buttons

The JButton class enables us to create buttons in interfaces in Java by calling the swing library. Today we will learn how to create an interface and how to add a button in the interface. Button content can be text, icon, or text and an icon next to it. We also learned that when we want to program interfaces in Java, we must first write a name and import the javax.swing library. This library provides us with many elements such as buttons and tools that are used in window programming. And we write * that is, we abbreviate it as if we were telling him to import all the tools in the swing library.

Example 1

package button;
import javax.swing.*;
public class Button{
     public static void main(String []args){
        JFrame F=new JFrame("ProgrammerTech");
        JButton B=new JButton("Click here");
        F.setSize(300, 250);
        B.setBounds(200, 100, 100, 50);

At first, we called the swing library and its complete components via javax.swing.* and entered the main program and included the JFrame interface class and the name of the interface that appears at the top is ProgrammerTech. Then we created the JButton class and started by setting the interface size with the command F.setSize, which was 300*250. Then we set the properties for the button, its height and width via setBounds, and then we put the button inside the container after we finished its properties and created it via add. And then by setLayout we set the sizes and special points in the button because if we don't specify these the button will not appear in the interface and by setVisible we show the interface.

And when you press run to run the code, the interface will appear as:

Create JButton in Java


Example 2

package button;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.Font;

public class Button {
    public static void main(String[] args) {

        JFrame frame = new JFrame("Programmer Tech");
        JButton button_1 = new JButton("button 1");
        JButton button_2 = new JButton("button 2");
        JButton button_3 = new JButton("button 3");
        Font newFont = new Font("Arial", Font.BOLD, 16);
        frame.setSize(300, 250);
        frame.setLayout(new FlowLayout());

At first we called some offices for buttons and colors and entered the main program and included the JFrame interface class and the name of the interface Programmer Tech. Then we created the three buttons, then we created a font object, the font size is 16, and we made the first button. newFont and we set it's font color via setForeground to blue and its background via setBackground to dark grey. The same is true for the remaining two buttons. We set a color and background for them, then add them to the interface via add and set the window size to be 300*250. And we made the exit button of the window close the program via JFrame.EXIT_ON_CLOSE. Then we used FlowLayout to make all the things in the window sorted and in the end we made the window visible with setVisible and its value true is visible.

And when the program is executed, the result will be as follows.

Create colored JButton in Java

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.