This is a Java Program to Create a Group of Right Justified Buttons and Arrange them in Container using Flow Layout Manager
We have to write a program in Java such that it creates a frame with few buttons which are right justified using the Flow Layout Manager.
For creating frame with right justified buttons, we have the following set of input and output.
1. When the frame is created :
On the execution of the program, it is expected that a frame appears with a few right justified buttons. Buttons - Button 1, Button 2, Button 3
1. Create a frame and few buttons.
2. Set the layout of frame as FlowLayout with alignment of components as RIGHT
3. Display the frame.
Here is source code of the Java Program to create right justified buttons. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.
/*Java Program to create right justified buttons and
arrange in conatiner using Flow Layout Manager*/
import javax.swing.*;
import java.awt.*;
import java.awt.FlowLayout;
class Push_Buttons
{
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame = new JFrame("Buttons");
frame.setSize(500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set the layout of frame as right aligned flow layout
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
//Create 3 push buttons
JButton[] button = new JButton[3];
for(int i=0;i<3;i++)
{
button[i]=new JButton("Button "+(i+1));
frame.add(button[i]);
}
//Display the frame
frame.setVisible(true);
}
}
1. Set the layout of frame as FlowLayout.
2. Set the alignment of the layout as FlowLayout.RIGHT. This ensures that all the components are right aligned.
Here’s the run time test case for creating a frame with right justified buttons.
Test case 1 – To view the frame with buttons.
Sanfoundry Global Education & Learning Series – Java Programs.
- Practice Programming MCQs
- Check Java Books
- Practice BCA MCQs
- Apply for Computer Science Internship
- Practice Information Technology MCQs