[英]How to display text in JFrame using Netbeans? How to clear text in a JFrame?
如何使用Netbeans在JFrame
顯示文本? 以及如何清除JFrame中的文字?
為文本創建標簽:
JLabel label1 = new JLabel("your text here");
更改標簽中的文本:
label1.setText("your new text here");
最后清除標簽:
label1.setText("");
而您所要做的就是將標簽放置在布局中或您使用的布局系統中,然后將其添加到JFrame中。
你可以用這個
@Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
g.drawString("Hello", 0, 0);
}
或使用jTextField
jTextField1.setText("Hello");
此代碼與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("");
謝謝..
您可以使用JLabel
顯示文本,也可以通過將其文本設置為“”進行重置。 檢查文件
如果您使用的是Netbeans GUI構建器,那么這里有一個很好的初學者教程 。 否則,如果您是手動編碼,那么在Oracle網站上有關於Swing的良好指南: http : //download.oracle.com/javase/tutorial/uiswing/TOC.html
您可以創建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.