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

justxueqingxiao 2012-10-08 10:52:38
代码如下:

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求大神指教。
...全文
282 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenhua 2012-10-09
  • 打赏
  • 举报
回复
那是因为con默认的布局是BorderLayout,直接给你代码:

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();}
}

xj_struggle 2012-10-09
  • 打赏
  • 举报
回复
布局问题,学会用布局就ok了
KelvenCheung 2012-10-09
  • 打赏
  • 举报
回复
2楼都帮你搞好了
liujun3512159 2012-10-09
  • 打赏
  • 举报
回复
看了,都头疼。呵呵。
迷尘 2012-10-08
  • 打赏
  • 举报
回复
感觉是GUI布局方面的问题,应该再增加几个容器。再把那一章看一下吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧