public class MyDialog extends JDialog
{
private int[][] array;
GridLayout gridLayout = new GridLayout(2, 1);
JPanel panel = new JPanel();
JButton jbCreate = new JButton("Datei erzeugen");
JLabel jlZeilen = new JLabel("Zeilenanzahl:");
JLabel jlSpalten = new JLabel("Spaltenanzahl:");
JTextField jtfZeilen = new JTextField();
JTextField jtfSpalten = new JTextField();
MyDialog()
{
this.setSize(300, 300);
panel.setLayout(new GridLayout(2,2));
panel.add(jlZeilen);
panel.add(jtfZeilen);
panel.add(jlSpalten);
panel.add(jtfSpalten);
this.getContentPane().setLayout(gridLayout);
this.getContentPane().add(panel);
this.getContentPane().add(jbCreate);
jbCreate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
createFile();
}
});
}
private void createFile()
{
int iZeilen = Integer.parseInt(jtfZeilen.getText());
int iSpalten = Integer.parseInt(jtfSpalten.getText());
NumberGenerator ng = new NumberGenerator(iZeilen, iSpalten);
array = ng.getResult();
saveToFile();
System.exit(0);
}
public void saveToFile()
{
try
{
FileOutputStream fos =
new FileOutputStream("c:\\Dokumente und Einstellungen\\zufallstest.txt");
PrintStream output = new PrintStream(fos);
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
output.print(array[i][j] + "\t");
}
output.println();
}
output.close();
System.out.println("Datei zufallstest.txt wurde erstellt");
}
catch (FileNotFoundException e)
{
System.out.println("Schreiben fehlgeschlagen");
}
} //-- End method saveToFile()
private class NumberGenerator
{
private int[][] array;
public NumberGenerator(int row, int column)
{
create(row, column);
}
public int[][] getResult()
{
return array;
}
public void create(int row, int column)
{
array = new int[row][column];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
System.out.print((int) (Math.random() * 10000) + " ");
array[i][j] = (int) (Math.random() * 10000);
}
System.out.println();
}
}
public void generate(int rows, int column)
{
BitSet b = new BitSet();
Random r = new Random();
int cnt = 0;
while (cnt < rows)
{
//int num = 1 + Math.abs(r.nextInt()) % 49000;
int num = 1 + Math.abs(r.nextInt()) % 49000;
if (!b.get(num))
{
b.set(num);
++cnt;
}
}
for (int i = 5; i <= 49000; ++i)
{
if (b.get(i))
{
System.out.println(i + " ");
}
}
System.out.println("");
}
} //-- End class NumberGenerator
public static void main(String args[])
{
MyDialog myDialog = new MyDialog();
myDialog.show();
} //-- End method main
}