这是我的界面
package client;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Register extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 4257580101437991396L;
JPanel pnlRegister;
JLabel lblUserName,lblGender,lblAge,img1,img2;
JLabel lblPassword,lblConfirmPass,logoPosition;
JTextField txtUserName,txtAge;
JPasswordField pwdUserPassword,pwdConfirmPass;
JRadioButton rbtnMale,rbtnFemale;
ButtonGroup btngGender;
JButton btnOk,btnCancel,btnClear;
String strServerIp;
//用于将窗口用于定位
Dimension scrnsize;
Toolkit toolkit=Toolkit.getDefaultToolkit();
//构造方法
public Register(String ip)
{
super("帐号注册");
strServerIp=ip;
pnlRegister=new JPanel();
this.getContentPane().add(pnlRegister);
lblUserName=new JLabel("用 户 名:");
lblGender=new JLabel("性 别:");
lblAge=new JLabel("年 龄:");
lblPassword=new JLabel("密 码:");
lblConfirmPass=new JLabel("确认密码:");
txtUserName=new JTextField(30);
txtAge=new JTextField(10);
pwdUserPassword=new JPasswordField(30);
pwdUserPassword.setEchoChar('*');
pwdConfirmPass=new JPasswordField(30);
pwdConfirmPass.setEchoChar('*');
rbtnMale=new JRadioButton("男",true);
rbtnFemale=new JRadioButton("女");
btngGender=new ButtonGroup();
btnOk=new JButton("确定(O)");
btnOk.setMnemonic('O');
btnOk.setToolTipText("保存注册信息");
btnCancel=new JButton("返回(B)");
btnCancel.setMnemonic('B');
btnCancel.setToolTipText("返回登录窗口");
btnClear=new JButton("清空(L)");
btnClear.setMnemonic('L');
btnClear.setToolTipText("清空注册信息");
/* 该布局采用手动布局 *
* setBounds设置组件位置 *
* setFont设置字体、字型、字号 *
* setForeground设置文字的颜色 *
* setBackground设置背景色 *
* setOpaque将背景设置为透明 */
pnlRegister.setLayout(null); //组件用手动布局
pnlRegister.setBackground(new Color(180,210,250));
lblUserName.setBounds(30,70,120,30);
txtUserName.setBounds(110,75,120,20);
lblPassword.setBounds(30,95,100,30);
pwdUserPassword.setBounds(110,100,120,20);
lblConfirmPass.setBounds(30,120,100,30);
pwdConfirmPass.setBounds(110,125,120,20);
lblGender.setBounds(30,145,100,30);
rbtnMale.setBounds(110,150,60,20);
rbtnFemale.setBounds(190,150,60,20);
lblAge.setBounds(30,170,100,30);
txtAge.setBounds(110,175,120,20);
btnOk.setBounds(10,210,80,25);
btnClear.setBounds(130,210,80,25);
btnCancel.setBounds(245,210,80,25);
Font fontstr=new Font("宋体",Font.PLAIN,12);
lblUserName.setFont(fontstr);
lblGender.setFont(fontstr);
lblPassword.setFont(fontstr);
lblConfirmPass.setFont(fontstr);
lblAge.setFont(fontstr);
rbtnMale.setFont(fontstr);
rbtnFemale.setFont(fontstr);
txtUserName.setFont(fontstr);
btnOk.setFont(fontstr);
btnCancel.setFont(fontstr);
btnClear.setFont(fontstr);
lblUserName.setForeground(Color.BLACK);
lblGender.setForeground(Color.BLACK);
lblPassword.setForeground(Color.BLACK);
lblAge.setForeground(Color.BLACK);
lblConfirmPass .setForeground(Color.BLACK);
rbtnMale.setForeground(Color.BLACK);
rbtnFemale.setForeground(Color.BLACK);
rbtnMale.setBackground(Color.white);
rbtnFemale.setBackground(Color.white);
rbtnMale.setOpaque(false);
rbtnFemale.setOpaque(false);
pnlRegister.add(lblUserName);
pnlRegister.add(lblGender);
pnlRegister.add(lblPassword);
pnlRegister.add(lblConfirmPass);
pnlRegister.add(lblAge);
pnlRegister.add(txtAge);
pnlRegister.add(txtUserName);
pnlRegister.add(pwdUserPassword);
pnlRegister.add(pwdConfirmPass);
pnlRegister.add(btnOk);
pnlRegister.add(btnCancel);
pnlRegister.add(btnClear);
pnlRegister.add(rbtnMale);
pnlRegister.add(rbtnFemale);
btngGender.add(rbtnMale);
btngGender.add(rbtnFemale);
//设置背景图片
ImageIcon icon1 = new ImageIcon(this.getClass().getResource("/icons/3.png"));
img1=new JLabel(icon1);
pnlRegister.add(img1);
img1.setBounds(0, -20,340, 96);
ImageIcon icon2 = new ImageIcon(this.getClass().getResource("/icons/4.png"));
img2=new JLabel(icon2);
pnlRegister.add(img2);
img2.setBounds(185, 90,200, 106);
this.setSize(340,270);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//将窗口定位在屏幕中央
scrnsize=toolkit.getScreenSize();
this.setLocation(scrnsize.width/2-this.getWidth()/2,
scrnsize.height/2-this.getHeight()/2);
//三个按钮注册监听
btnOk .addActionListener(this);
btnCancel.addActionListener(this);
btnClear .addActionListener(this);
} //构造方法结束
class Register_Customer extends Object implements java.io.Serializable
{
/**
*
*/
private static final long serialVersionUID = 6843334615403912646L;
String custName;
String custPassword;
String age;
String sex;
String email;
}
//按钮监听响应
public void actionPerformed(ActionEvent ae)
{
Object source=new Object();
source=ae.getSource();
if (source.equals(btnOk)) //"确定"按钮
{
register();
}
if (source.equals(btnCancel)) //"返回"按钮
{
new Login();
//this.dispose();
}
if (source.equals(btnClear)) //"清空"按钮
{
txtUserName.setText("");
pwdUserPassword.setText("");
pwdConfirmPass.setText("");
txtAge.setText("");
}
} //actionPerformed()结束
//////////"确定"按钮事件响应//////////
public void register()
{
//接受客户的详细资料
String custName = txtUserName.getText();
String custPassword = pwdUserPassword.getText();
String age = txtAge.getText();
String sex = rbtnMale.isSelected()?"男":"女";
//验证用户名是否为空
if(custName.length()==0)
{
JOptionPane.showMessageDialog(null,"用户名不能为空");
return;
}
//验证密码是否为空
if(custPassword.length()==0)
{
JOptionPane.showMessageDialog(null,"密码不能为空");
return;
}
//验证密码的一致性
if(!custPassword.equals(pwdConfirmPass.getText()))
{
JOptionPane.showMessageDialog(null,"密码两次输入不一致,请重新输入");
return;
}
//验证年龄是否为空
if(age.length()==0)
{
JOptionPane.showMessageDialog(null,"年龄不能为空");
return;
}
//验证年龄的合法性
int age1=Integer.parseInt(txtAge.getText());
if (age1<=0||age1>100){
JOptionPane.showMessageDialog(null,"年龄输入不合法");
return;
}
}
public static void main(String args[])
{
new Register("127.0.0.1");
}
} //class Register 结束
温馨提示:内容为网友见解,仅供参考