0

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");
        }*/

    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    What's the problem? Do you have a specific question about the posted code? – copeg Sep 08 '16 at 19:27
  • I bet it has something to you iterating `num++` twice in your for loop. Try debugging and checking the line numbers for Null Pointer Exceptions. – Compass Sep 08 '16 at 19:31
  • If i run the code it gives me Exception in thread "main" java.lang.NullPointerException at JFileCaninet.AlphabetFiles.buildedPanel(AlphabetFiles.java:51) at JFileCaninet.AlphabetFiles.(AlphabetFiles.java:29) at JFileCaninet.Main.main(Main.java:7) – Jace Chan Sep 08 '16 at 19:31
  • `alphabetButton[1]` is `null` in the first run of your for loop. Don't use `num++` as an array index. Set once, use twice. – Compass Sep 08 '16 at 19:34
  • I changed to this but still give me the same error private void buildedPanel() { int num=0; for(char i='a'; i<='z'; i++) { alphabetButton[num] = new JButton(""+i); alphabetButton[num].addActionListener(this); num++; } – Jace Chan Sep 08 '16 at 19:53
  • I found the problem; for got to add panel1 =new JPanel(); in the buildpanel() – Jace Chan Sep 27 '16 at 18:28

0 Answers0