Built-in Arrow Button In Java


This is a tutorial in which we will going to create a program that have a built-in arrow button in Java.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of arrowButton.java.

2. Import the following packages:

import java.awt.*; //used to access the FlowLayout class
import javax.swing.*; //used to access the JFrame class
import javax.swing.plaf.basic.*; //used to access the BasicArrowButton class

We will use the BasicArrowButton class of the javax.swing.plaf package to have the arrow buttons.

3. Initialize your variable in your Main, variable frame for JFrame only.

JFrame frame = new JFrame("Arrow Button Sample");

4. To add the built-in arrow to the frame, we will use the getContentPane.add method then instantiate the BasicArrowButton class and the position of the arrows that you want.

frame.getContentPane().add(new BasicArrowButton(BasicArrowButton.NORTH));
frame.getContentPane().add(new BasicArrowButton(BasicArrowButton.SOUTH));
frame.getContentPane().add(new BasicArrowButton(BasicArrowButton.WEST));
frame.getContentPane().add(new BasicArrowButton(BasicArrowButton.EAST));

 

 

As you can see the code above, we have created four different buttons the North, South, West, and East position, respectively.

5. Now, have the frameset to a FlowLayout as the layout manager and set visibility, close operation of the frame, and pack the frame. Have this code below:

frame.getContentPane().setLayout(new FlowLayout());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

 

Output:

output

Here’s the full code of this tutorial:

import java.awt.*; //used to access the FlowLayout class
import javax.swing.*; //used to access the JFrame class
import javax.swing.plaf.basic.*; //used to access the BasicArrowButton class
 
public class arrowButton{
  public static void main(String args[]) {
    JFrame frame = new JFrame("Arrow Button Sample");
 
    frame.getContentPane().add(new BasicArrowButton(BasicArrowButton.NORTH));
    frame.getContentPane().add(new BasicArrowButton(BasicArrowButton.SOUTH));
    frame.getContentPane().add(new BasicArrowButton(BasicArrowButton.WEST));
    frame.getContentPane().add(new BasicArrowButton(BasicArrowButton.EAST));
 
    frame.getContentPane().setLayout(new FlowLayout());
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
 
}

 



Sharing is caring!

Facebook Comments

Leave a Reply