This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "for loop" Watch "for loop" New topic

for loop

li qing

Joined: Sep 27, 2003
Posts: 11
i'm doing a program on game... showing 26 alphabet in the center panel but the result only show 1 Z... can someone help me pls~!
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class gameApplet extends JApplet implements ActionListener {
//create all variable
private int points = 0;
private int counts = 0;
//declare all instances and objects
private JLabel topLabel = new JLabel("Picture Matching Game");
private JLabel bottomLabel = new JLabel("Do you know your ABC?");
private JPanel strPanel= new JPanel();
private JButton play = new JButton("Play Game");
private JButton exit = new JButton ("Exit Game");
private JLabel score = new JLabel("Score:");
private JTextField scOre= new JTextField(5);
private JPanel right = new JPanel();
private JLabel time = new JLabel("Time ellapsed:");
private JTextField tiMe= new JTextField(5);
private JPanel ctrPanel= new JPanel();
private JPanel picsPanel = new JPanel();
private JPanel imagesPanel = new JPanel();
private final int NUM_OF_PICS = 26;
private final int NUM_OF_IMAGES = 26;
private JButton[] picsBtn = new JButton[NUM_OF_PICS];
private JButton[] imagesBtn = new JButton[NUM_OF_IMAGES];
private JPanel centre = new JPanel();
//declare an ImageIcon arr for pics
ImageIcon[] pictures = new ImageIcon[NUM_OF_PICS];
//declare an array string for pictures
String[] arrPics = {
"a.gif", "b.gif", "c.gif", "d.gif", "e.gif", "f.gif", "g.gif", "h.gif",
"i.gif", "j.gif", "k.gif", "l.gif", "m.gif",
"n.gif", "o.gif", "p.gif", "q.gif", "r.gif", "s.gif", "t.gif", "u.gif",
"v.gif", "w.gif", "x.gif", "y.gif", "z.gif"};
//method to load pictures to the button
public void loadPics() {
//load the picture onto the button
for (int i = 0; i < pictures.length; i++) {
//img[i] = new ImageIcon(getImage(getCodeBase(), imgFile[i]));
pictures[i] = new ImageIcon (getImage(getCodeBase(), arrPics[i]));
//declare an ImageIcon arr for images
ImageIcon[] images = new ImageIcon[NUM_OF_IMAGES];
//declare an array string for images
String[] arrImages = {
"imagesA.gif", "imagesB.gif", "imagesC.gif", "imagesD.gif", "imagesE.gif",
"imagesF.gif", "imagesG.gif", "imagesH.gif", "imagesI.gif", "imagesJ.gif", "imagesK.gif",
"imagesM.gif", "imagesN.gif", "imagesO.gif", "imagesP.gif", "imagesQ.gif", "imagesR.gif",
"imagesT.gif", "imagesU.gif", "imagesV.gif", "imagesW.gif", "imagesX.gif", "imagesY.gif",
//method to load images to the button
public void loadImages() {
for (int i=0; i<images.length; i++) {
images[i] = new ImageIcon(getImage(getCodeBase(),arrImages[i]));
//init() method to start the applet
public void init(){
String name = JOptionPane.showInputDialog(null,
"Please enter your name",
"Hi " + name + "!",
// ???
AudioClip mySong = getAudioClip(getDocumentBase(), "");;
Container container = getContentPane();
container.setLayout(new BorderLayout(5,10));
container.add(strPanel, BorderLayout.NORTH);
container.add(right, BorderLayout.EAST);
container.add(centre, BorderLayout.CENTER);
//method to draw the centre panel
public void centrePanel() {
centre.setLayout(new BorderLayout(5, 10));
//create the array of cells to represent pictures
for(int i =0; i<pictures.length; i++) {
picsBtn[i] = new JButton();
centre.add(picsBtn[i], BorderLayout.NORTH);
for(int i =0; i<images.length; i++) {
imagesBtn[i] = new JButton();
centre.add(imagesBtn[i], BorderLayout.SOUTH);
//method to draw string
public void topPanel() {
strPanel.setLayout(new BorderLayout(5, 10));
strPanel.add(topLabel, BorderLayout.NORTH);
strPanel.add(bottomLabel, BorderLayout.CENTER);
//method to add play,exit and score to thr right panel
public void rightPanel() {
// right.setLayout(new GridLayout(4, 0, 5, 5));
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(100, 300));
//method to count marks
public static int count(int points, int counts) {
switch (counts) {
case 1:
points += 5;
case 2:
points += 4;
case 3:
points += 3;
points += 2;
return points;
public void actionPerformed(ActionEvent e) {
if (e.getSource() == play)
else if (e.getSource() == exit)
else {

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

You wrote:

Each position in a BorderLayout can only hold one Component. If you add two JButtons in a row to the NORTH position in a Container with a BorderLayout, then the second one replaces the first one, which is no longer in the the JPanel. This is why you only see the "Z"s.
To fix your lovely program, so that the world may bask in its rightful greatness, you could create another JPanel using the default FlowLayout, and add all the JButtons to this JPanel; then add this JPanel to the original Container's "NORTH" position.
[ October 09, 2003: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
li qing

Joined: Sep 27, 2003
Posts: 11
oh... ok... it's fixed... thanks...
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: for loop
jQuery in Action, 3rd edition