Comment puis-je afficher du texte en JFrame
en utilisant NetBeans? Et comment effacer du texte dans JFrame?Comment afficher du texte dans JFrame en utilisant Netbeans? Comment effacer du texte dans un JFrame?
Répondre
Vous pouvez utiliser JLabel
pour afficher du texte et vous pouvez réinitialiser en définissant son texte sur "". check doc
Si vous utilisez le générateur d'interface graphique Netbeans, il y a un bon débutant tutorial. Dans le cas contraire, si vous codez manuellement, il y a un bon guide Swing sur le site d'Oracle: http://download.oracle.com/javase/tutorial/uiswing/TOC.html
vous pouvez utiliser cette
@Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
g.drawString("Hello", 0, 0);
}
ou utiliser JTextField
jTextField1.setText("Hello");
Vous pouvez créer JLabel.
JLabel l1=new JLabel("Your text");
l1.setText(""); // clear the text
pour créer une étiquette pour le texte:
JLabel label1 = new JLabel("your text here");
pour modifier le texte de l'étiquette:
label1.setText("your new text here");
et enfin pour effacer l'étiquette:
label1.setText("");
et tout ce que vous avez à faire est de placer l'étiquette en vous r layout, ou quel système de mise en page que vous utilisez, puis ajoutez-le simplement au JFrame.
Vérifiez les propriétés.Sous la commande Swing, vous trouverez le JLabel.Drag & DROP dans votre JFRAME.
Ce code fonctionne avec Netbeans .. créer un nouveau cadre et définir texte pour vous au milieu du cadre ..
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Label For The JFrame");
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(107, 107, 107)
.addComponent(jLabel1)
.addContainerGap(141, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(88, 88, 88)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(187, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @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/tutorial/uiswing/lookandfeel/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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
Remplacer cette jLabel1.setText("Label For The JFrame");
avec votre étiquette personnalisée dans le code.
et quand vous voulez effacer l'étiquette utiliser jLabel1.setText("");
Merci ..
- 1. Comment afficher/masquer JPanels dans un JFrame?
- 2. Comment afficher deux JTables dans un JFrame
- 3. changement JFrame ("java") logo en utilisant NetBeans
- 4. en ajoutant jpanel dans une jframe. NetBeans
- 5. Afficher l'aperçu du code HTML dans JFrame?
- 6. Passer un paramètre du champ de texte d'une jframe à une autre jframe
- 7. Texte JFrame affichant mal
- 8. Comment obtenir du texte à partir d'un JFrame
- 9. JFrame dans un autre JFrame
- 10. JPanel dans un dessin JFrame au-dessus du menu JFrame
- 11. Afficher l'image dans un JFrame
- 12. Java: Texte non enroulant dans JFrame dialogue
- 13. Comment afficher un JFrame à partir d'une autre méthode JFrame?
- 14. Comment implémenter une animation dans un JFrame
- 15. JCommandButton dans JFrame
- 16. Effacer JPanel ou JFrame
- 17. en utilisant thread dans JFrame
- 18. comment afficher une carte dans jframe
- 19. Effacer toutes les sélections JFrame
- 20. Comment afficher du texte dans un ListBox?
- 21. Comment ajouter des animations interactives dans jFrame?
- 22. Comment obtenir une chaîne d'un Jframe (champ de texte) et mettre sur une autre jFrame (txtArea)? - Netbeans - Bien expliqué
- 23. Comment redimensionner la jframe à la résolution d'écran dans netbeans?
- 24. Comment afficher un JFreeChart dans un JLabel JFrame
- 25. Comment fermer JFrame suivi d'un autre JFrame?
- 26. Comment puis-je afficher un BufferedImage dans un JFrame?
- 27. Afficher JFrame dans JPanel dans java
- 28. Afficher une image .png dans un JFrame?
- 29. Console dans un JFrame
- 30. comment changer l'espace entre JFrame et JLabel dans NetBeans?
Note: cela n'a rien à voir avec Netbeans. Considérez Netbeans uniquement comme un outil d'édition de vos sources, et non comme une partie du langage lui-même. – jabal