Need to create 26 JButtons(A-Zbuttons) when i click one of the button the only 1 JLabel will display the letter) for example if i click QButton the Label will display "Q". Need to use GridLayout and 5 JPanel(first 4 panels hold 6 letters and the 5th panel hold 2 button and 1 JLabel).
package JFileCaninet;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class AlphabetFiles extends JFrame implements ActionListener
{
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private JPanel panel5;
private JLabel displayLabel;
private JButton[] alphabetButton = new JButton[26];
//char[] abc = new char[26];
public AlphabetFiles()
{
super("file sort");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildedPanel();
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);
panel1.setLayout(new GridLayout(1,6));
panel2.setLayout(new GridLayout(1,6));
panel3.setLayout(new GridLayout(1,6));
panel4.setLayout(new GridLayout(1,6));
panel5.setLayout(new GridLayout(1,6));
setVisible(true);
}
private void buildedPanel()
{
int num=0;
for(char i='a'; i<='z'; i++)
{
alphabetButton[num++] = new JButton(""+i);
alphabetButton[num++].addActionListener(this);
}
for(int i=0 ; i<6 ;i++)//a to f
{
panel1.add(alphabetButton[i]);
}
for(int i=6 ; i<12 ;i++)//g to l
{
panel2.add(alphabetButton[i]);
}
for(int i=12 ; i<18 ;i++)//m to r
{
panel3.add(alphabetButton[i]);
}
for(int i=18 ; i<24 ;i++)//r to x
{
panel4.add(alphabetButton[i]);
}
for(int i=24 ; i<27 ;i++)//r to x
{
panel5.add(alphabetButton[i]);
}
panel5.add(displayLabel);
}
public void actionPerformed(ActionEvent e)
{
JButton holder8 =(JButton) e.getSource();
displayLabel.setText(holder8.getActionCommand());
}
}
package JFileCaninet;
public class Main {
public static void main(String[] args) {
//AlphabetFiles al =
new AlphabetFiles();
/*try
{
new AlphabetFiles();
}
catch(NullPointerException nullPointer)
{
System.out.println("NullPointer occur");
}*/
}
}