i want to display buttons like this in applet
a b c d e f
g h i j k l
m n o p q r
s t u v w x
y z 0 1 2 3
4 5 6 7 8 9
when i click to any of this character it should display. and each time all the letters should be in different place.........
please inform me how to create this using applet.. i already developed using static button but its very lengthy process so please help me to get the solutions

Dani AI

Generated

A concise, maintainable approach is to generate the 36 labels once, shuffle them, and create the buttons in a loop instead of writing 36 static declarations. Use a GridLayout(6,6) and attach the same ActionListener to every button; the listener can read the button label (or actionCommand) and display it. As suggested, understanding the basics makes this trivial; 's links are helpful background. The solution below uses Swing inside a JApplet for clarity — the same pattern works with AWT Button or a standalone JFrame.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class ShuffleApplet extends JApplet {
    private JPanel grid = new JPanel(new GridLayout(6,6,4,4));
    private JButton[] btns = new JButton[36];

    public void init() {
        List<String> labels = new ArrayList<>();
        for (char c='a'; c<='z'; c++) labels.add(String.valueOf(c));
        for (char d='0'; d<='9'; d++) labels.add(String.valueOf(d));
        Collections.shuffle(labels);

        ActionListener al = e -> {
            String ch = ((JButton)e.getSource()).getText();
            System.out.println("Clicked: " + ch); // or update a JLabel
        };

        for (int i=0;i<36;i++) {
            btns[i] = new JButton(labels.get(i));
            btns[i].addActionListener(al);
            grid.add(btns[i]);
        }
        add(grid);
    }

    // call to reshuffle labels at any time
    private void reshuffle() {
        List<String> labels = new ArrayList<>();
        for (char c='a'; c<='z'; c++) labels.add(String.valueOf(c));
        for (char d='0'; d<='9'; d++) labels.add(String.valueOf(d));
        Collections.shuffle(labels);
        for (int i=0;i<36;i++) btns[i].setText(labels.get(i));
        grid.revalidate(); grid.repaint();
    }
}

Tips and caveats: update Swing components only on the Event Dispatch Thread (use SwingUtilities.invokeLater when needed). To change positions rather than labels, removeAll and re-add buttons in the new order. Avoid mixing AWT and Swing components (lightweight vs heavyweight issues). Finally, note that browser support for Java applets is effectively gone; for future-proofing consider a Swing/JavaFX desktop app or an HTML/JavaScript UI instead of an in-browser applet.

Recommended Answers

All 3 Replies

1) download jdk
2) download jdk documentation
3) install both
4) study the Java tutorial until you can complete your assignment

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.