Hvorfor bliver der ikke noget displayed?
Min kode producere ikke nogen form for GUI som den gerne skulle, hvorfor?import java.awt.*;
import javax.swing.*;
import java.io.*;
public class FileSorter
{
public static void main(String[] args) throws IOException
{
new FileSorter();
}
public FileSorter() throws IOException
{
RandomAccessFile ccraf = new RandomAccessFile("cc.dat","rw");
}
class Player //----- inner class simulates "records" -----------
{
String firstname;
int age;
final static int CC_RECORD_SIZE = 212;
final static int NAMELENGTH = 15;
public void writeToFile(RandomAccessFile ccraf, long recordNum)
//-----------------------------------------------------------
// Writes one record into ccraf, which must already be open
// IOExceptions are detected and reported
//-----------------------------------------------------------
{
try
{
ccraf.seek( recordNum * CC_RECORD_SIZE);
ccraf.writeChars(setLength(firstname,NAMELENGTH));
ccraf.writeInt(age);;
}
catch(IOException exc)
{
JOptionPane.showMessageDialog (null, "While writing " + exc.toString());
}
}
String setLength(String s,int len)
//-----------------------------------------------------------
// Forces length of string to a specific value
// Necessary before writing into a random-access file
//-----------------------------------------------------------
{
StringBuffer sb = new StringBuffer(s);
sb.setLength(len);
return sb.toString();
}
}
//---- end of Player class -----------------
void create(RandomAccessFile ccraf) throws IOException
//-----------------------------------------------------------------
// Puts records into ccraf, which must already be open
//-----------------------------------------------------------------
{
Player thisRec = new Player();
for (int c=0; c < 2; c++)
{
thisRec.firstname = JOptionPane.showInputDialog("");
thisRec.age = Integer.parseInt(JOptionPane.showInputDialog(""));
thisRec.writeToFile(ccraf,c);
}
}
}
