这种方法创建简单的登录是错误的。

huangapple 未分类评论46阅读模式
英文:

is the method to create simple login wrong

问题

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

public class testing {

    private JFrame frame;
    private JTextField username;
    private JTextField password;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    testing window = new testing();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public testing() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnNewButton_1 = new JButton("cancel");
        btnNewButton_1.setBounds(266, 181, 109, 56);
        frame.getContentPane().add(btnNewButton_1);

        username = new JTextField();
        username.setBounds(227, 11, 128, 39);
        frame.getContentPane().add(username);
        username.setColumns(10);

        password = new JTextField();
        password.setBounds(227, 76, 128, 39);
        frame.getContentPane().add(password);
        final int num;
        num=Integer.parseInt(password);
        password.setColumns(10);

        JButton btnNewButton = new JButton("login");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(num==3124)
                    {JOptionPane.showMessageDialog(null, "correct");}
                else
                    {JOptionPane.showMessageDialog(null, "wrong");}
            }
        });
        btnNewButton.setBounds(62, 181, 123, 56);
        frame.getContentPane().add(btnNewButton);
    }
}
英文:

I am a newbie in Java GUI development and I am stuck in the following code.
I am open to suggestions. I am actually trying to create a simple login that gives OK if the password is matched to the number 3124, and otherwise shows the error message. Please help me.

	import java.awt.EventQueue;
	
	    import javax.swing.JFrame;
	    import javax.swing.JOptionPane;
	    import javax.swing.JButton;
	    import java.awt.event.ActionListener;
	    import java.awt.event.ActionEvent;
	    import javax.swing.JTextField;
	
	public class testing {
	
		private JFrame frame;
		private JTextField username;
		private JTextField password;
	
		/**
		 * Launch the application.
		 */
		public static void main(String[] args) {
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						testing window = new testing();
						window.frame.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
	
		/**
		 * Create the application.
		 */
		public testing() {
			initialize();
		}
	
		/**
		 * Initialize the contents of the frame.
		 */
		private void initialize() {
			frame = new JFrame();
			frame.setBounds(100, 100, 450, 300);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.getContentPane().setLayout(null);
			
			JButton btnNewButton_1 = new JButton("cancel");
			btnNewButton_1.setBounds(266, 181, 109, 56);
			frame.getContentPane().add(btnNewButton_1);
			
			username = new JTextField();
			username.setBounds(227, 11, 128, 39);
			frame.getContentPane().add(username);
			username.setColumns(10);
			
			password = new JTextField();
			password.setBounds(227, 76, 128, 39);
			frame.getContentPane().add(password);
				final int num;
				num=Integer.parseInt(password);
			password.setColumns(10);
			
			JButton btnNewButton = new JButton("login");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(num==3124)
						{JOptionPane.showMessageDialog(null, "correct");}
					else
						{JOptionPane.showMessageDialog(null, "wrong");}
				}
			});
			btnNewButton.setBounds(62, 181, 123, 56);
			frame.getContentPane().add(btnNewButton);
		}
	}

答案1

得分: 0

你甚至在用户有机会输入任何内容到文本框之前就检查了密码。你需要在事件监听代码中获取并检查num的值,即在actionPerformed方法中。此外,不要将密码转换为int(因为有人可能输入非数字字符串)。

以下是修复后的代码。

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

public class Testing {

    private JFrame frame;
    private JTextField username;
    private JTextField password;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Testing window = new Testing();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Testing() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnNewButton_1 = new JButton("cancel");
        btnNewButton_1.setBounds(266, 181, 109, 56);
        frame.getContentPane().add(btnNewButton_1);

        username = new JTextField();
        username.setBounds(227, 11, 128, 39);
        frame.getContentPane().add(username);
        username.setColumns(10);

        password = new JTextField();
        password.setBounds(227, 76, 128, 39);
        frame.getContentPane().add(password);
        password.setColumns(10);

        JButton btnNewButton = new JButton("login");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                final String num;
                num = (password.getText());

                if (num.equalsIgnoreCase("3124")) {
                    JOptionPane.showMessageDialog(null, "correct");
                } else {
                    JOptionPane.showMessageDialog(null, "wrong");
                }
            }
        });
        btnNewButton.setBounds(62, 181, 123, 56);
        frame.getContentPane().add(btnNewButton);
    }
}
英文:

You were checking the password even before the user has had the chance to enter anything into the text box. You need to get and check the value of num in the event listener code i.e. in actionPerformed. Also, don't convert the password to int (someone may enter some non-numeric string).

This code below works better.

	import javax.swing.JFrame;
	import javax.swing.JOptionPane;
	import javax.swing.JButton;
	import java.awt.event.ActionListener;
	import java.awt.EventQueue;
	import java.awt.event.ActionEvent;
	import javax.swing.JTextField;
	
	public class Testing {
	
		private JFrame frame;
		private JTextField username;
		private JTextField password;
	
		/**
		 * Launch the application.
		 */
		public static void main(String[] args) {
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						Testing window = new Testing();
						window.frame.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
	
		/**
		 * Create the application.
		 */
		public Testing() {
			initialize();
		}
	
		/**
		 * Initialize the contents of the frame.
		 */
		private void initialize() {
			frame = new JFrame();
			frame.setBounds(100, 100, 450, 300);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.getContentPane().setLayout(null);
	
			JButton btnNewButton_1 = new JButton("cancel");
			btnNewButton_1.setBounds(266, 181, 109, 56);
			frame.getContentPane().add(btnNewButton_1);
	
			username = new JTextField();
			username.setBounds(227, 11, 128, 39);
			frame.getContentPane().add(username);
			username.setColumns(10);
	
			password = new JTextField();
			password.setBounds(227, 76, 128, 39);
			frame.getContentPane().add(password);
			password.setColumns(10);
	
			JButton btnNewButton = new JButton("login");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
	
					final String num;
					num = (password.getText());
	
					if (num.equalsIgnoreCase("3124")) {
						JOptionPane.showMessageDialog(null, "correct");
					} else {
						JOptionPane.showMessageDialog(null, "wrong");
					}
				}
			});
			btnNewButton.setBounds(62, 181, 123, 56);
			frame.getContentPane().add(btnNewButton);
		}
	}

huangapple
  • 本文由 发表于 2020年4月7日 17:40:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/61077040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定