• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

for loop

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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",
"imagesL.gif",
"imagesM.gif", "imagesN.gif", "imagesO.gif", "imagesP.gif", "imagesQ.gif", "imagesR.gif",
"imagesS.gif",
"imagesT.gif", "imagesU.gif", "imagesV.gif", "imagesW.gif", "imagesX.gif", "imagesY.gif",
"imagesZ.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",
"Name",
JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(null,
"Hi " + name + "!",
"Greetings",
JOptionPane.INFORMATION_MESSAGE);
// ???
AudioClip mySong = getAudioClip(getDocumentBase(), "spacemusic.au");
mySong.play();
Container container = getContentPane();
container.setLayout(new BorderLayout(5,10));
topPanel();
container.add(strPanel, BorderLayout.NORTH);
rightPanel();
container.add(right, BorderLayout.EAST);
loadPics();
loadImages();
centrePanel();
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();
picsBtn[i].setIcon(pictures[i]);
centre.add(picsBtn[i], BorderLayout.NORTH);
picsBtn[i].setBackground(Color.BLACK);
picsBtn[i].addActionListener(this);
}
for(int i =0; i<images.length; i++) {
imagesBtn[i] = new JButton();
imagesBtn[i].setIcon(images[i]);
centre.add(imagesBtn[i], BorderLayout.SOUTH);
imagesBtn[i].setBackground(Color.BLACK);
imagesBtn[i].addActionListener(this);
}
}
//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));
right.add(play);
right.add(exit);
right.add(score);
right.add(scOre);
right.add(time);
right.add(tiMe);
right.add(jp);
}
//method to count marks
public static int count(int points, int counts) {
switch (counts) {
case 1:
points += 5;
break;
case 2:
points += 4;
break;
case 3:
points += 3;
break;
default:
points += 2;
break;
}
return points;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == play)
init();
else if (e.getSource() == exit)
System.exit(0);
else {
}
}
}//applet
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
li qing
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh... ok... it's fixed... thanks...
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum...
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic