Ecco tre classi , poi ci vuoe un file nel mio caso txt (c'ho messo solo la parola casa),il jframe è autogeneato da netbeans:
codice:
package forum;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author stefano
*/
public class Label extends javax.swing.JFrame {
/**
* Creates new form Label
*/
public Label() {
initComponents();
try {
LeggiFile f=new LeggiFile("/home/stefano/Label.txt");
} catch (FileNotFoundException ex) {
Logger.getLogger(Label.class.getName()).log(Level.SEVERE, null, ex);
}
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Label");
jLabel2.setText("(f.");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(167, 167, 167)
.addComponent(jLabel2)
.addContainerGap(218, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(148, 148, 148)
.addComponent(jLabel2)
.addContainerGap(216, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public void setTitleLabel(String title){
jLabel2.setText(title);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tu...feel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Label.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Label.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Label.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Label.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Label().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel2;
// End of variables declaration
}
classe LeggiFile:
codice:
package forum;
import java.io.*;
import java.util.*;
/**
*
* @author stefano
*/
public class LeggiFile{
private Scanner sc;
private String stringaLabel="";
public LeggiFile(String path) throws FileNotFoundException{ //path è il percorso del file da cui recuperareil testo
sc = new Scanner(new File(path));
sc.useDelimiter("\n");
}
public String testoPerLabel(){
while(sc.hasNext()){
stringaLabel+=(sc.next());
}
return stringaLabel;
}
public static void main(String args[]) throws FileNotFoundException{
LeggiFile f=new LeggiFile("/home/stefano/Label.txt");
System.out.println(f.testoPerLabel()) ;
}
}
e la classe per provare tutto
ps creati prima il file di testo e cambia il path ....
codice:
package forum;
import java.io.FileNotFoundException;
/**
*
* @author stefano
*/
public class ProvaLabel {
public static void main(String args[]) throws FileNotFoundException{
Label l=new Label();
LeggiFile f=new LeggiFile("/home/stefano/Label.txt");
l.setTitleLabel(f.testoPerLabel());
l.setVisible(true);
}
}