我正在制作一个登录小程序,以便在我的无线硬盘上安装,并且我正在为它构建框架,(每当我尝试将Strings声明为任何内容但是最终它会显示错误说只有最终允许)我试图使用String用户和String传递在我的Skeleton类中的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) {
}
}
在我的其他类中几乎没有任何东西,因为类本身依赖于导入String变量
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);
}
}
答案 0 :(得分:2)
我自己,我会将上面的GUI显示为模态JDialog,而不是作为JFrame,我会使用JPasswordField而不是第二个JTextField,我会给我的类公共getter方法,这将允许调用类查询其领域的状态,如
public String getUserName() {
return userNameTextField.getText();
}
和
public char[] getPassword() {
return passwordField.getPassword();
}
请注意,密码几乎不应该作为字符串处理,因为这样做会给您的应用程序带来很大的漏洞,使其他人更容易提取密码。
因此,作为模态对话框,当对话框可见时,调用代码将停止,然后仅在对话框不再可见时重新启动,此时您可以使用上述方法查询对话框的状态,提取相关信息。
请注意,您的代码使用静态方法,这是您不想做的事情,因为这样做,您的类没有“状态”,因此查询JTextFields所持有的值将不起作用。 / p>
修改强>
例如:
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();
}
});
}
}
答案 1 :(得分:0)
你真的确定你需要一堂课来比较密码吗?这应该是您ActionListener
的代码。另外,您甚至无法在代码中实例化Skeleton
,因此您无法访问这些字段。如果你真的需要这样做,只需在Skeleton
上定义一些getter。
public String getPassword() {
return field2.getText(); //you should really rename this field
}
public String getUser() {
return field.getText(); //you should really rename this field
}