如何将textfield(JFrame 1)的输入值传递给label(JFrame 2)

时间:2013-12-28 19:23:01

标签: java swing netbeans jframe jtextfield

我被困在我的节目中很长一段时间了。原因是,我不知道如何将JFrame1的文本字段中输入的文本传递给位于JFrame2中的标签。文本字段中输入的文本应该在JFrame 2中显示为标签。我是java的新手,所以我真的不知道我将使用什么代码。请帮帮我。

我的JFrame1是LoginForm。

enter image description here

以下是您需要的代码:

import java.sql.*;
import javax.swing.*;

public class LoginForm extends javax.swing.JFrame {

Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;

public LoginForm() {
    initComponents();
    conn = dbconnect.ConnectDB();
}


private void b_loginMouseClicked(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:

String sql = "select * from tbl_login where username = '"+username.getText()+"' and password = '"+password.getText()+"'";
        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
                if (rs.next()){
                    String type = rs.getString("type");
                        if (type.equals("admin")) {
                            AdminHome ah = new AdminHome();
                            ah.setVisible(true);
                        }
                        else {
                            EmployeeHome eh = new EmployeeHome();
                            eh.setVisible(true);
                        }
                }
                else {
                    JOptionPane.showMessageDialog(null, "Incorrect username or password.");
                }
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
}                                    

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new LoginForm().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton b_login;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPasswordField password;
private javax.swing.JTextField username;
// End of variables declaration                   
}

这是JFrame2,即EmployeeHome

enter image description here

以下是JFrame2的代码:

import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;


public class EmployeeHome extends javax.swing.JFrame {

Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;

/**
 * Creates new form EmployeeHome
 */
public EmployeeHome() {
    initComponents();
    conn = dbconnect.ConnectDB();
    DateTime ();
}


public void DateTime (){

    Thread clock = new Thread (){
        public void run () {
            for (;;){
                Calendar cal = new GregorianCalendar ();
                String month = cal.getDisplayName(Calendar.LONG, Calendar.MONTH, Locale.getDefault());
                int day = cal.get(Calendar.DAY_OF_MONTH);
                int year = cal.get(Calendar.YEAR);
                String week;
                    if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
                        week = "Sunday";
                    }
                    else if (cal.get(Calendar.DAY_OF_WEEK) == 2) {
                        week = "Monday";
                    }
                    else if (cal.get(Calendar.DAY_OF_WEEK) == 3) {
                        week = "Tuesday";
                    }
                    else if (cal.get(Calendar.DAY_OF_WEEK) == 4) {
                        week = "Wednesday";
                    }
                    else if (cal.get(Calendar.DAY_OF_WEEK) == 5) {
                        week = "Thursday";
                    }
                    else if (cal.get(Calendar.DAY_OF_WEEK) == 6) {
                        week = "Friday";
                    }
                    else {
                        week = "Saturday";
                    }
                l_date.setText(week + ", " + month + " " + day + ", " + year);

                int hour = cal.get(Calendar.HOUR);
                int minute = cal.get(Calendar.MINUTE);
                int second = cal.get(Calendar.SECOND);
                String ampm;
                    if (cal.get(Calendar.AM_PM) == 0) {
                        ampm = "AM";
                    }
                    else {
                        ampm = "PM";
                    }
                l_time.setText(hour + " : " + minute + " : " + second + ampm);
                    try {
                        sleep (1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(EmployeeHome.class.getName()).log(Level.SEVERE, null, ex);
                    }
            }
        }
    };
    clock.start();
}


/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new EmployeeHome().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton b_logout;
private javax.swing.JButton b_tin;
private javax.swing.JButton b_tout;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JLabel l_address;
private javax.swing.JLabel l_age;
private javax.swing.JLabel l_bdate;
private javax.swing.JLabel l_contact;
private javax.swing.JLabel l_date;
private javax.swing.JLabel l_dept;
private javax.swing.JLabel l_id;
private javax.swing.JLabel l_name;
private javax.swing.JLabel l_time;
private javax.swing.JLabel l_username;
// End of variables declaration                   
}

我的目标是将输入的文本传递给用户名文本框(image1)到用户名标签(image2)旁边的标签。你能帮帮我怎么做吗?

2 个答案:

答案 0 :(得分:3)

您可以在创建另一个JFrame实例时注入该属性。

示例:

EmployeeHome eh = new EmployeeHome(myTextField.getText());

或者

EmployeeHome eh = new EmployeeHome();
eh.setUserNameText(myTextField.getText());

您需要在EmployeeHome中提供这些方法。因此,请更改此类。

public EmployeeHome(String userName) {
    initComponents();
    userLabel.setText(userName);
    conn = dbconnect.ConnectDB();
    DateTime ();
}

public void setUserNameText(String name){
    this.userLabel.setText(name);
}

答案 1 :(得分:0)

如果你想这样做,你可以在创建了employeehome的对象之后在frame1中插入以下代码

     eh.l_username.setText(username.getText());
瞧瞧......完成了!