如何使用Netbeans在JFrame中显示文本?如何清除JFrame中的文本?

问题描述:

如何使用Netbeans在JFrame中显示文本?以及如何清除JFrame中的文本?如何使用Netbeans在JFrame中显示文本?如何清除JFrame中的文本?

+2

注:此无关使用Netbeans。将Netbeans视为仅用于编辑源的工具,而不是语言本身的一部分。 – jabal 2012-03-14 14:18:24

您可以使用JLabel来显示文本,您可以通过将其文本设置为“”来重置。检查doc

如果您使用Netbeans GUI构建器,那么有一个很好的初学者的tutorial。否则,如果你手工编码,有一个很好的指南,秋千上的Oracle的网站:http://download.oracle.com/javase/tutorial/uiswing/TOC.html

您可以使用此

@Override 
public void paintComponents(Graphics g) { 
    super.paintComponents(g); 
    g.drawString("Hello", 0, 0); 
} 

,或者使用了JTextField

jTextField1.setText("Hello"); 

您可以创建一个JLabel。

JLabel l1=new JLabel("Your text"); 

l1.setText(""); // clear the text 

创建标签的文字:

JLabel label1 = new JLabel("your text here"); 

更改标签的文本:

label1.setText("your new text here"); 

,并最终清除标签:

label1.setText(""); 

而你所要做的就是在你身上放置标签r布局或你正在使用的布局系统,然后将其添加到JFrame中。

检查属性。在Swing控件中,您将找到JLabel.Drag &将它放在JFRAME中。

此代码使用Netbeans工程..创建一个新的框架,并为您在框架中间设置文本..

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 

}

替换此jLabel1.setText("Label For The JFrame");使用代码自定义标签。

,当你想空白标签使用jLabel1.setText("");

谢谢..