gpt4 book ai didi

java - 如何引用另一个类中的字符串?

转载 作者:行者123 更新时间:2023-11-30 03:45:05 25 4
gpt4 key购买 nike

我正在制作一个登录小程序以安装在我的无线硬盘上,并且目前正在为其构建框架,(每次我尝试将字符串声明为任何内容但最终时,它都会显示一个错误,指出仅允许最终)我正在尝试使用在我的 Skeleton 类中找到的 String user 和 String pass in if 语句:

package open;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Skeleton extends JFrame implements ActionListener{
private static final long serialVersionUID = 1248L;

public static void addComponentsToPane(Container pane, Container container) {
//Adding in text fields for login
JButton b1 = new JButton("Login");
final JTextField field2 = new JTextField(2);
final JTextField field = new JTextField(1);

//Creating Box Layout - Subject to change
GroupLayout layout = new GroupLayout(pane);
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

//Setting alignments
b1.setAlignmentX(Component.CENTER_ALIGNMENT);
field.setAlignmentY(BOTTOM_ALIGNMENT);
field2.setAlignmentY(BOTTOM_ALIGNMENT);
b1.setAlignmentY(CENTER_ALIGNMENT);

//Dimensions for User
field.setMaximumSize(new Dimension(235, 20));
field.setMinimumSize(new Dimension(235, 20));

//Dimensions for Password
field2.setMaximumSize(new Dimension(235, 20));
field2.setMinimumSize(new Dimension(235, 20));

//Dimensions for Button
b1.setMaximumSize(new Dimension(100, 40));
b1.setMinimumSize(new Dimension(100, 40));

//Adding space between components
container.add(Box.createRigidArea(new Dimension(275,20)));
container.add(field);
container.add(Box.createRigidArea(new Dimension(275,10)));
container.add(field2);
container.add(Box.createRigidArea(new Dimension(275,12)));
container.add(b1);

//Listen to the login button
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String user = field.getText();
String pass = field2.getText();
System.out.println("Value: " + user + " " + pass);
};
});
}

public static void createAndShowGUI() {

JFrame frame = new JFrame("User Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
addComponentsToPane(frame.getContentPane(), frame);

//Create a grey label
JLabel greyLabel = new JLabel();
greyLabel.setOpaque(true);
greyLabel.setBackground(new Color(205, 209, 209));
greyLabel.setPreferredSize(new Dimension(300, 400));

//Adding the label to the pane
frame.getContentPane().add(greyLabel, BorderLayout.CENTER);

//Display the window.
frame.setSize(275, 175);
frame.setVisible(true);
}

public static void closer(boolean close, JFrame frame){
System.out.println(close);
if(close == true){
frame.setVisible(false);
frame.dispose();
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public void actionPerformed(ActionEvent arg0) {
}
}

在我的另一个类中几乎没有任何内容,因为类本身依赖于导入字符串变量

package password;

import open.Skeleton;
import refrence.Resources;

public class PasswordCompare
{
public static void main(String[] args)
{
System.out.println(user);
System.out.println(pass);
}
}

最佳答案

我自己,我会将上面的 GUI 显示为模态 JDialog,而不是 JFrame,我将使用 JPasswordField 而不是第二个 JTextField,并且我会给我的类提供公共(public) getter 方法,允许调用类进行查询其字段的状态,类似于

public String getUserName() {
return userNameTextField.getText();
}

public char[] getPassword() {
return passwordField.getPassword();
}

请注意,密码几乎不应该作为字符串处理,因为这样做会给您的应用程序带来重大漏洞,从而使其他人更容易提取密码。

因此,作为模式对话框,当对话框可见时,调用代码将停止,然后仅当对话框不再可见时重新启动,此时您可以使用上述方法查询对话框的状态,提取相关信息。

<小时/>

请注意,您的代码使用静态方法,这是您不想做的事情,因为通过这样做,您的类没有“状态”,因此查询 JTextFields 保存的值将不起作用。

<小时/>

编辑
例如:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

public class MySkeleton extends JPanel {
private static final int COLUMN_COUNT = 10;
private static final int I_GAP = 3;
private JTextField userNameField = new JTextField();
private JPasswordField passwordField = new JPasswordField();

public MySkeleton() {
super(new GridBagLayout());
userNameField.setColumns(COLUMN_COUNT);
passwordField.setColumns(COLUMN_COUNT);

GridBagConstraints gbc = getGbc(0, 0, GridBagConstraints.BOTH);
add(new JLabel("User Name:"), gbc);
gbc = getGbc(1, 0, GridBagConstraints.HORIZONTAL);
add(userNameField, gbc);
gbc = getGbc(0, 1, GridBagConstraints.BOTH);
add(new JLabel("Password:"), gbc);
gbc = getGbc(1, 1, GridBagConstraints.HORIZONTAL);
add(passwordField, gbc);
}

public static GridBagConstraints getGbc(int x, int y, int fill) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.insets = new Insets(I_GAP, I_GAP, I_GAP, I_GAP);
gbc.fill = fill;

return gbc;
}

public String getUserName() {
return userNameField.getText();
}

public char[] getPassword() {
return passwordField.getPassword();
}
}

可以通过以下方式在另一个类中进行测试:

public class MySkeletonTest {
private static void createAndShowGui() {
MySkeleton mainPanel = new MySkeleton();

int input = JOptionPane.showConfirmDialog(null, mainPanel, "Login",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (input == JOptionPane.OK_OPTION) {
System.out.println("User Name: " + mainPanel.getUserName());

// **** for testing purposes only. Never do this in a real app.
System.out.println("Password: " + new String(mainPanel.getPassword()));
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 如何引用另一个类中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25953533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com