当前位置: 代码迷 >> J2SE >> 一个用java写的简略后台登陆程序 求教!
  详细解决方案

一个用java写的简略后台登陆程序 求教!

热度:7937   发布时间:2013-02-25 00:00:00.0
一个用java写的简单后台登陆程序 求教!!
代码如下:
[code=Java][/code]
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class login{
JLabel JLabel1,JLabel2;
JFrame mainJFrame;
Container con;
JButton loginbtn,cancelbtn;
JTextField userText;
JTextField passwordField;
HandleAction handleAction;

public class HandleAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String msg;
if(e.getSource()==loginbtn){
msg="your username:"+userText.getText()+"\n your password:"
+new String(passwordField.getText());
JOptionPane.showMessageDialog(mainJFrame,msg);
}
else if(e.getSource()==cancelbtn){
passwordField.setText("");
userText.setText("");
}
}
}
public login(){
handleAction=new HandleAction();
JLabel1=new JLabel("用户名");
JLabel2=new JLabel("密码");
mainJFrame=new JFrame("后台登陆");
con=mainJFrame.getContentPane();
loginbtn=new JButton("登陆");
loginbtn.addActionListener(handleAction);
cancelbtn=new JButton("取消");
cancelbtn.addActionListener(handleAction);
userText=new JTextField();
userText.setColumns(20);
passwordField=new JPasswordField();
passwordField.setColumns(20);
con.add(JLabel1);
con.add(userText);
con.add(JLabel2);
con.add(passwordField);
con.add(loginbtn);
con.add(cancelbtn);
mainJFrame.setSize(300,300);
mainJFrame.setVisible(true);
mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){new login();}
}
但是运行后结果:是所有的标签和按钮都叠加到了一起,本人初学java求大神指教。

------解决方案--------------------------------------------------------
那是因为con默认的布局是BorderLayout,直接给你代码:
Java code
import javax.swing.*;import java.awt.event.*;import java.awt.*;public class HandleAction implements ActionListener{    JLabel JLabel1,JLabel2;    JFrame mainJFrame;    Container con;    JButton loginbtn,cancelbtn;    JTextField userText;    JTextField passwordField;     public void actionPerformed(ActionEvent e){    String msg;    if(e.getSource()==loginbtn){    msg="your username:"+userText.getText()+"\n your password:"    +new String(passwordField.getText());    JOptionPane.showMessageDialog(mainJFrame,msg);    }    else if(e.getSource()==cancelbtn){        passwordField.setText("");        userText.setText("");    }} public HandleAction(){    JLabel1=new JLabel("用户名");    JLabel1.setBounds(new Rectangle(20,20,60,20));//设置位置左边距、上边距、宽度、高度    JLabel2=new JLabel("密码");    JLabel2.setBounds(new Rectangle(20,50,60,20));//设置位置左边距、上边距、宽度、高度    mainJFrame=new JFrame("后台登陆");    con=mainJFrame.getContentPane();    con.setLayout(null);//要设置成null,不然默认是BorderLayout布局    loginbtn=new JButton("登陆");    loginbtn.setBounds(new Rectangle(20,80,60,20));//设置位置左边距、上边距、宽度、高度    loginbtn.addActionListener(this);    cancelbtn=new JButton("取消");    cancelbtn.setBounds(new Rectangle(90,80,60,20));//设置位置左边距、上边距、宽度、高度    cancelbtn.addActionListener(this);    userText=new JTextField();    userText.setColumns(20);    userText.setBounds(new Rectangle(90,20,60,20));//设置位置左边距、上边距、宽度、高度    passwordField=new JPasswordField();    passwordField.setColumns(20);    passwordField.setBounds(new Rectangle(90,50,60,20));//设置位置左边距、上边距、宽度、高度    con.add(JLabel1);    con.add(userText);    con.add(JLabel2);    con.add(passwordField);    con.add(loginbtn);    con.add(cancelbtn);    mainJFrame.setSize(300,300);    mainJFrame.setVisible(true);    mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }public static void main(String[] args){new HandleAction();}}
  相关解决方案