简体   繁体   English

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

[英]How to display text in JFrame using Netbeans? How to clear text in a JFrame?

How can I display text in JFrame using Netbeans? 如何使用Netbeans在JFrame显示文本? And how to clear text in JFrame? 以及如何清除JFrame中的文字?

to create a label for text: 为文本创建标签:

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

to change the text in the label: 更改标签中的文本:

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

and finally to clear the label: 最后清除标签:

label1.setText("");

and all you have to to is place the label in your layout, or what ever layout system you are using, and then just add it to the JFrame.. 而您所要做的就是将标签放置在布局中或您使用的布局系统中,然后将其添加到JFrame中。

you can use this 你可以用这个

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

or use jTextField 或使用jTextField

 jTextField1.setText("Hello");

This code works with Netbeans.. create one new Frame and set Text for you in the middle of the Frame.. 此代码与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

} }

Replace This jLabel1.setText("Label For The JFrame"); 替换此jLabel1.setText("Label For The JFrame"); with your customized Label in Code. 在代码中带有自定义标签。

and when you want to blank the Label use jLabel1.setText(""); 当您要清空标签时,请使用jLabel1.setText("");

Thanks.. 谢谢..

You can use JLabel to display text, and you can reset by setting its text to "" . 您可以使用JLabel显示文本,也可以通过将其文本设置为“”进行重置。 check doc 检查文件

If you're using the Netbeans GUI builder then there's a good beginner's tutorial . 如果您使用的是Netbeans GUI构建器,那么这里有一个很好的初学者教程 Otherwise, if you're coding manually, there's a good guide to Swing on Oracle's site: http://download.oracle.com/javase/tutorial/uiswing/TOC.html 否则,如果您是手动编码,那么在Oracle网站上有关于Swing的良好指南: http : //download.oracle.com/javase/tutorial/uiswing/TOC.html

You can create Jlabel. 您可以创建Jlabel。

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

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

检查Properties。在Swing控件下,您将在JFRAME中找到JLabel.Drag并拖放它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM