J'utilise Java Swing. Au départ, je veux lire un fichier (qui est assez gros). Ainsi, le cadre est affiché après que le fichier est complètement. Alors que je veux d'abord charger le cadre (affiché), puis le fichier doit être lu.Comment lire un fichier en Java uniquement après l'affichage de JFrame?
class Passwd {
JFrame jfrm;
// other elements
Passwd() {
start();
// Display frame.
jfrm.setVisible(true);
}
public void start() {
// Create a new JFrame container.
jfrm = new JFrame("Password Predictability & Strength Measure");
// Specify FlowLayout for the layout manager.
//jfrm.setLayout(new FlowLayout());
jfrm.setLayout(null);
// Give the frame an initial size.
jfrm.setSize(450, 300);
// align window to center of screen
jfrm.setLocationRelativeTo(null);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// some elements
File file = new File("file.txt");
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// operation
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
// Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Passwd();
}
});
}
}
Comment lire le fichier après l'affichage du cadre?
Pourquoi y a-t-il "->" sur la première ligne de code? – user5155835
@ user5155835: partie de la syntaxe lambda de Java 8. Veuillez lire [Que fait l'opérateur de flèche, '->' en Java?] (Https://stackoverflow.com/questions/15146052/what-does-the-arrow-operator-do-in-java) –
Please pouvez-vous dire pour Pre Java 8? – user5155835