JPanels in a five-by-one grid
Question
Make 26 JButtons, each labeled with a single, different letter of the alphabet. Create a JFrame to hold five JPanels in a five-by-one grid. Place six Buttons within each of the first four Janels and two Buttons within the fifth Janel of the JFrame. Add a JLabel to the fifth Janel. When the user clicks a JButton, the text of the JLabel is set to “Folder X*, where X is the letter of the alphabet that is clicked.
Answer
The Java code that creates a JFrame with five JPanels in a five-by-one grid, where each of the first four JPanels has six JButtons labeled with a single letter of the alphabet, and the fifth JPanel has two JButtons and a JLabel that updates with the letter of the clicked JButton:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AlphabetButtons extends JFrame {
private JLabel folderLabel;
public AlphabetButtons() {
super("Alphabet Buttons");
setSize(800, 200);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// create the five JPanels
JPanel panel1 = new JPanel(new GridLayout(1, 6));
JPanel panel2 = new JPanel(new GridLayout(1, 6));
JPanel panel3 = new JPanel(new GridLayout(1, 6));
JPanel panel4 = new JPanel(new GridLayout(1, 6));
JPanel panel5 = new JPanel(new GridLayout(1, 2));
// create the 26 JButtons labeled with a single letter of the alphabet
for (char c = 'A'; c <= 'Z'; c++) {
JButton button = new JButton(String.valueOf(c));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
folderLabel.setText("Folder " + c);
}
});
if (c <= 'F') {
panel1.add(button);
} else if (c <= 'L') {
panel2.add(button);
} else if (c <= 'R') {
panel3.add(button);
} else {
panel4.add(button);
}
}
// create the two JButtons and the JLabel for the fifth JPanel
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
folderLabel = new JLabel("Folder ", JLabel.CENTER);
folderLabel.setFont(new Font("Arial", Font.PLAIN, 24));
folderLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// add the components to the fifth JPanel
panel5.add(button1);
panel5.add(button2);
panel5.add(folderLabel);
// add the five JPanels to the JFrame
setLayout(new GridLayout(5, 1));
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);
setVisible(true);
}
public static void main(String[] args) {
new AlphabetButtons();
}
}
In this code, each JButton is created with a label that corresponds to a single letter of the alphabet, and an ActionListener is added to each button that sets the text of the JLabel to “Folder X” when it is clicked, where X is the letter of the alphabet that corresponds to the clicked JButton. The buttons are added to the first four JPanels based on their alphabetical order, and the two buttons and the JLabel are added to the fifth JPanel. Finally, the five JPanels are added to the JFrame, which is displayed to the user.
The program I provided creates a graphical user interface with five JPanels in a five-by-one grid, where the first four JPanels each contain six JButtons labeled with a single letter of the alphabet, and the fifth JPanel contains two JButtons and a JLabel.
When a user clicks on one of the JButtons labeled with a letter of the alphabet, the text of the JLabel in the fifth JPanel is updated to display “Folder X”, where X is the letter of the clicked JButton.
The expected output of this program is a window with five rows of components, where each row contains either six JButtons labeled with a letter of the alphabet or two JButtons and a JLabel. When a user clicks on one of the JButtons labeled with a letter of the alphabet, the text of the JLabel is updated to display “Folder X”, where X is the letter of the clicked JButton.
Greetings! Very useful advice in this particular article! Its the little changes that will make the biggest changes. Thanks for sharing!