首页 文章

如何在不重新启动框架的情况下将用户输入的数据显示到netbeans GUI构建器中的JLebel中?

提问于
浏览
0

我正在做一个小项目,这是项目的一部分 . 它就像退出原型测试一样 .

在程序中,我有一个文本字段,一个按钮和一个标签,用于显示将在文本字段中插入的文本 . (由于声誉低,我无法上传GUI的PIC . )

在这里,我在文本字段中输入 . 如果按下“take_System_date”按钮,则文本字段可以将系统日期作为输入 . 然后,作为文本字段的操作,输入将显示在标签中 . 我这样做是使用netbeans gui builder . 对于JLabel值设置文本值我已经给出了自定义代码并推出了变量“text” . 消息来源如下 .

package test_custom_label_printing;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JOptionPane;

public class customLebel_Printing extends javax.swing.JFrame {

    String text=null;
    String initializatioDate=null;
    public customLebel_Printing(String textForConstructor, String dateForConstructor) 
    {
//     textInput();
     text=textForConstructor; 
     initializatioDate=dateForConstructor;
     initComponents();
    }
    public customLebel_Printing() 
    {
     initComponents();
    }

public void textInput()
{
     text=JOptionPane.showInputDialog("Enter ur text here :");

}
@SuppressWarnings("unchecked")//Computer generated code has not given

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            

        String date=jTextField1.getText();

        text=jTextField1.getText();
//        text=JOptionPane.showInputDialog("Enter ur text Here( In Text Field event):");
        initComponents();   
        customLebel_Printing call = new customLebel_Printing(text,date);
        call.setVisible(true);
        this.dispose();

    }                                           

    private void take_System_date_jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                          

        DateFormat onlyDate = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        jTextField1.setText(onlyDate.format(date));
    }                                                         

    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(customLebel_Printing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(customLebel_Printing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(customLebel_Printing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(customLebel_Printing.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 customLebel_Printing().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel input_jLabel2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton take_System_date_jButton1;
    // End of variables declaration                   
}

计划正在运作 . 但我的问题是,为了显示输入的文本,我重新启动窗口,我当前的窗口正在关闭,同一个窗口又来了 .

我认为这不是一个有效的想法 . 正如我之前所说,我将其作为原型 . 我会将这个相同的概念用于一个项目,该项目具有退出大窗口和该窗口上的许多选项,并且重新启动大型内容并不好看 . 在我的项目中,用户ID将在文本字段中输入,在标签中将通过读取数据库中的值来显示ID的用户名和地址 .

现在我想知道有没有办法在netbeans中执行此操作而不重新启动窗口 . 我不明白如何在netbeans GUI构建器中为JPanel编写自定义代码用于查看文本 .

1 回答

  • 0

    如果我理解的话,你需要添加这一行

    private void take_System_date_jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                          
    
        DateFormat onlyDate = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        jTextField1.setText(onlyDate.format(date));
        jLabel1.setText(jTextField1.getText());
    }
    

相关问题