|
-
JLabel setText problem, help please
Here are my two pieces of code
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class RatRace {
public static void main(String[] args) throws IOException {
JFileChooser file = new JFileChooser();
file.setDialogTitle("Where is your Rat Race map ?");
if (file.showDialog(null,"OK") == JFileChooser.APPROVE_OPTION) {
File f = file.getSelectedFile();
BufferedReader read = new BufferedReader(new FileReader(f));
//determine the dimensions of the character array
String lineRead = "";
int rows = 0;
int columns = 0;
while ((lineRead = read.readLine())!=null) {
columns = lineRead.length();
rows++;
}
//put the characters in the map file into a 2d array of characters
String temp = "";
BufferedReader br = new BufferedReader(new FileReader(f));
char[][] myMap = new char[rows][columns];
for (int r = 0; r < rows; r++) {
while ((temp=br.readLine())!= null) {
for (int c = 0; c < columns ; c++) {
myMap[r][c] = temp.charAt(c);
}
}
}
MapWindow window = new MapWindow(myMap);
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MapWindow extends JFrame{
public MapWindow(char[][] map){
JFrame window = new JFrame("Rat Race");
window.getContentPane().setLayout(new GridLayout(map.length, map[0].length));
JLabel[][]label = new JLabel[map.length][map[0].length];
for (int r = 0; r < map.length; r++) {
for (int c = 0; c < map[0].length ; c++) {
label[r][c] = new JLabel();
label[r][c].setText(Character.toString(map[r][c]));
window.getContentPane().add(label[r][c]);
}
}
window.pack();
window.show();
}
}
Im using Dr Java. When I call the main method in RatRace, by
java RatRace
a window will show up, prompting me for a map file with the content depicting a maze. After I choose that file, Iwant its content to appear in
a JFrame window. That is, I want to have sth like this in
the JFrame window called "Rat Race"
#######################
###....@..#........####
#####.....#..........##
#.........#####....#..#
#####.............##..#
#..@#.....@...#......@#
##..#......####.......#
#......J...#......##..#
####.......@.....###..#
#@.........###...#....#
#...###.....#....######
##....#....####......@#
###.@....#..........#.#
#....#####..@.....###.#
#...##########........#
#.#.@.......##.......@#
#............@........#
#..................S..#
#@........#######.....#
#.........#######.@...#
#.........####........#
#......@..............#
#######################
but why do I get this

Does anyone know why? Thanks for help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks