Java JFrame 中的登录屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20708749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Login screen in JFrame
提问by genfy
I am trying to create a JFrame login screen in Java. I have searched the internet and only found things on Netbeans which I do not want to use because I would like to make it entirely in code.
我正在尝试用 Java 创建一个 JFrame 登录屏幕。我在互联网上搜索过,只在 Netbeans 上找到了我不想使用的东西,因为我想完全用代码来制作。
It would need to be that you could not see the usernames and passwords just by looking at the code.
必须是您无法仅通过查看代码来看到用户名和密码。
I have created a JFrame:
我创建了一个 JFrame:
JFrame launcher = new JFrame("Login");
And some widgets:
还有一些小部件:
JTextField User = new JTextField("Username");
JPasswordField Pass = new JPasswordField("Password");
JButton Login = new JButton("Login");
But every time i do:
但每次我这样做:
launcher.add(User)
launcher.add(Pass)
launcher.add(Login)
The Login button would coveer the entire window.
登录按钮将覆盖整个窗口。
So here is the pseudo code of what I'm trying to do:
所以这是我想要做的伪代码:
Login = create new button
Pass = create new Password Field
User = create new Text Field
Check = 0
add User at top of window
add Pass at middle of window
add Login at bottom of window
if Pass input == any password from list then {
Check = Check + 1
}
if User input == any username from list then {
Check = Check + 1
}
if Check == 2 {
Change to new class
}else{
Show on screen "INVALID" then restart
}
采纳答案by XQEWR
You don't have a layout manager set, so the JFrame's Default which is BorderLayout, is adding everything to the center, which means when you add pass, it covers over the user, then when you add the login button, it covers over the pass, so it looks like you only have a login button. In order to get what you want:
您没有设置布局管理器,因此 JFrame 的默认设置是 BorderLayout,将所有内容添加到中心,这意味着当您添加 pass 时,它会覆盖用户,然后当您添加登录按钮时,它会覆盖通过,所以看起来你只有一个登录按钮。为了得到你想要的:
add User at top of window
add Pass at middle of window
add Login at bottom of window
An example of the code could be:
代码的一个例子可能是:
launcher.setLayout(new BorderLayout());
launcher.add(user, BorderLayout.NORTH);
launcher.add(pass, BorderLayout.CENTER);
launcher.add(login, BorderLayout.SOUTH);
If you want to make it check login when the button is clicked add an ActionListenerto login.
如果要在单击按钮时检查登录,请添加ActionListener以登录。
login.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Check login stuff here...
//with maybe something like this?
String sLogin = login.getText();
String sPass = pass.getText();
//Then compare with some other string/data you already have saved somewhere...
}
});
However if you want more exact layout, you can try searching up GridBagLayout
但是,如果您想要更精确的布局,可以尝试搜索GridBagLayout
If you want to read more about layouts in general go here: LayoutManagers
如果你想阅读更多关于布局的一般信息,请访问:LayoutManagers
回答by camickr
The Login button would coveer the entire window.
登录按钮将覆盖整个窗口。
That is because the default layout manager for a JFrame is a BorderLayout and you are adding all the components to the "CENTER" (by default) and only the last component added is displayed.
这是因为 JFrame 的默认布局管理器是 BorderLayout 并且您将所有组件添加到“CENTER”(默认情况下)并且只显示添加的最后一个组件。
So you need to choose a more appropriate layout manager.
所以你需要选择一个更合适的布局管理器。
Read the section from the Swing tutorial on Layout Managersfor more information and examples.
阅读有关布局管理器的 Swing 教程中的部分以获取更多信息和示例。
回答by Jeus
You can use GridBagLayout in netBeans . this layout is stable way for complex form and panel .
您可以在 netBeans 中使用 GridBagLayout。这种布局是复杂形式和面板的稳定方式。
回答by ravibagul91
Try this code for login screen
class Login extends JFrame implements ActionListener{
Connection con=null;
Statement st=null;
ResultSet rs=null;
JLabel l1,l2;
JButton b1,b2;
JTextField t1;
JPasswordField pf;
Login(){
Toolkit tk=Toolkit.getDefaultToolkit();
Image img=tk.getImage("C:/Image.jpg");
setIconImage(img);
Icon icon1=new ImageIcon("C:/Image.jpg");
JLabel i=new JLabel(icon1);
JPanel p;
p=(JPanel)getContentPane();
p.add(i);
this.getContentPane().setBackground(new Color(243,67,226));
setVisible(true);
setLocation(225,50);
//setLocationRelativeTo(null);
setSize(900,600);
setTitle("Login");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
l1=new JLabel("UserName");
l2=new JLabel("Password");
t1=new JTextField(10);
AbstractAction action = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof JButton){
JButton button = (JButton) e.getSource();
button.doClick();
} else if(e.getSource() instanceof JComponent){
JComponent component = (JComponent) e.getSource();
component.transferFocus();
}
}
};
t1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus");
t1.getActionMap().put("TransferFocus", action);
pf=new JPasswordField(10);
pf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus");
pf.getActionMap().put("TransferFocus", action);
ImageIcon ic=new ImageIcon("C:/key.gif");
b1=new JButton("Login",ic);
b1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus");
b1.getActionMap().put("TransferFocus", action);
ImageIcon ic1=new ImageIcon("C:/cancel.jpg");
b2=new JButton("Exit",ic1);
b2.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus");
b2.getActionMap().put("TransferFocus", action);
b1.addActionListener(this);
b2.addActionListener(this);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
p1.setBackground(new Color(243,67,226));
p2.setBackground(new Color(243,67,226));
p3.setBackground(new Color(243,67,226));
p4.setBackground(new Color(243,67,226));
p1.add(l1);
p1.add(t1);
p2.add(l2);
p2.add(pf);
p3.add(b1);
p3.add(b2);
p4.setLayout(new GridLayout(3,1));
p4.add(p1);
p4.add(p2);
p4.add(p3);
add(p4);
t1.requestFocus();
//b1.requestFocus();
//b1.requestFocusInWindow();
validate();
} //eof cons.
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==b1){
try {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url=null,url1=null,userID=null,password=null;
String dbFileName=null;
String dbFileName1=null;
String sql=null;
dbFileName = "C:/db.accdb";
url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
"DBQ="+dbFileName+";";
con=DriverManager.getConnection(url);//,"system","manager"
st=con.createStatement();
}catch(Exception e){
System.out.println(e);
dispose();
}
if(t1.getText().trim().length()==0){
JOptionPane.showMessageDialog(this,"Enter User Name");
return;
}
if(pf.getText().trim().length()==0){
JOptionPane.showMessageDialog(this,"Enter Password");
return;
}
ResultSet rs=st.executeQuery("select password from LOGIN1 where uname='"+t1.getText().trim()+"'");
if(rs.next()){
if(rs.getString(1).equals(pf.getText())){
String smess="WEL-COME ";
String ss="Congratulations......";
int res=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component) null,smess,ss,res);
JFrame frm=new JFrame("Title");
className b=new className(frm);
frm.setSize(800,500);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setResizable(false);
frm.setLocationRelativeTo(null);
dispose();
frm.show();
}else{
JOptionPane.showMessageDialog(this,"Invalid Password");
}
}else{
JOptionPane.showMessageDialog(this,"Invalid User name");
}
} catch (SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}if(ae.getSource()==b2){
dispose();
}
}
public static void main(String args[]){
new Login();
}
}