[英]How to Pass String from one java file to another java file
我創建了兩個Java文件,一個是Reg.java,第二個是Get.java。 在Reg.java中,我創建了一個JFrame,其名稱的文本字段和年齡的文本字段以及一個按鈕。 我想要的就是當您在文本字段中輸入名稱和年齡並單擊按鈕時,它將傳遞字符串名稱和年齡並顯示在Get.java中。
這是我的Reg.java代碼
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Reg extends JFrame implements ActionListener {
private Container con = getContentPane();
FlowLayout fl = new FlowLayout();
JLabel lb1 = new JLabel(": ");
JTextField tf1 = new JTextField(14);
JLabel lb2 = new JLabel("Enter your Age: ");
JTextField tf2 = new JTextField(14);
JButton btnSub = new JButton("Submit");
public Reg(){
setLayout(fl);
setSize(350, 275);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(btnSub);
lb1.setAlignmentX(LEFT_ALIGNMENT);
lb2.setAlignmentX(LEFT_ALIGNMENT);
lb1.setPreferredSize(new Dimension(120,50));
lb2.setPreferredSize(new Dimension(120,50));
tf1.setAlignmentX(RIGHT_ALIGNMENT);
tf2.setAlignmentX(RIGHT_ALIGNMENT);
btnSub.setHorizontalAlignment(JButton.CENTER);
btnSub.setToolTipText("Click to Submit");
btnSub.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String name = tf1.getText();
String age = tf2.getText();
}
public static void main(String[] args){
Reg fr = new Reg();
fr.setVisible(true);
}
}
對於更一般的情況,您可能提供訪問用戶名和密碼的方法,以及從主類到登錄類的引用以調用這些方法。
但這不需要兩個Java類,更不用說兩個Java文件了。 它也不需要使用兩個框架,實際上,這是一種情況,其中多個框架使任務更加棘手,因為一個框架是非模態的。 使用模式JDialog
或JOptionPane
代替“登錄框架”。 這樣,可以立即取消密碼檢查。
可能是這樣的:(提示: 所有用戶的有效密碼均為 “來賓”)
解決方法如下:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class LoginRequired {
private JTextField usernameField = new JTextField("Joe Blogs");
private JPasswordField passwordField = new JPasswordField();
char[] password = {'g', 'u', 'e', 's', 't'};
JPanel loginPanel;
LoginRequired() {
JFrame f = new JFrame("Login Required");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JLabel output = new JLabel(
"Login is required to use this application!",
SwingConstants.CENTER);
output.setBorder(new EmptyBorder(50, 100, 50, 100));
f.add(output);
f.pack();
f.setResizable(false);
f.setLocationByPlatform(true);
f.setVisible(true);
boolean loginValid = false;
while (!loginValid) {
showLogin(f);
loginValid = isLoginValid();
}
String user = usernameField.getText();
output.setText("Welcome back, " + user + "!");
f.setTitle("Logged In: " + user);
}
private boolean isLoginValid() {
char[] passwordEntered = passwordField.getPassword();
if (passwordEntered.length != password.length) {
return false;
} else {
for (int ii = 0; ii < password.length; ii++) {
if (password[ii] != passwordEntered[ii]) {
return false;
}
}
return true;
}
}
private void showLogin(JFrame frame) {
if (loginPanel==null) {
loginPanel = new JPanel(new BorderLayout(5, 5));
JPanel labels = new JPanel(new GridLayout(0, 1, 2, 2));
labels.add(new JLabel("User Name", SwingConstants.RIGHT));
labels.add(new JLabel("Password", SwingConstants.RIGHT));
loginPanel.add(labels, BorderLayout.WEST);
JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
usernameField = new JTextField("Joe Blogs");
controls.add(usernameField);
passwordField = new JPasswordField();
controls.add(passwordField);
loginPanel.add(controls, BorderLayout.CENTER);
}
passwordField.setText("");
JOptionPane.showMessageDialog(
frame, loginPanel, "Log In", JOptionPane.QUESTION_MESSAGE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new LoginRequired();
}
});
}
}
public class Reg extends JFrame implements ActionListener {
.....
'''''
Get get;
public Reg(){
.....
.....
get = new Get();
}
@Override
public void actionPerformed(ActionEvent e){
String name = tf1.getText();
String age = tf2.getText();
get.print(name);
get.print(age);
}
}
class Get{
public void print(String txt) {
System.out.println(txt);
}
}
將BuferedReader與get.java一起使用,並將BufferedWriter與reg.java一起使用。 在reg.java中使用BufferedWriter在兩行中編寫Name和age,然后使用BufferedReader在Get.java中讀取這些值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.