1.去掉原来的界面的装饰,改用自己装饰:
先看看效果,如下图
如何实现:
1.去掉原来的windows的装饰:
this.setSize(340, 256);
this.setUndecorated(true);
//虽然去掉了装饰,但是下面的界面图标与界面的标题是不可以去掉的,否则在桌面最下面的工具栏中会看不到标题与图标的。
URL url5 = this.getClass().getResource("/p_w_picpath/client_title.png");
this.setIconImage(new ImageIcon(url5).getImage());
this.setTitle("用户登录");
this.setLocation(new ScreenLocation(348,250).getLocation());
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setResizable(false);
2.移动界面的代码:
Point loc = null;
Point tmp = null;
boolean isDragged = false;
//拖动窗体的方法
private void setDragable() {
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent e) {
isDragged = false;
//当鼠标不拖动时,设置鼠标显示的样式
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public void mousePressed(java.awt.event.MouseEvent e) {
tmp = new Point(e.getX(), e.getY());
isDragged = true;
//当鼠标点击时,鼠标显示的样式
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
});
//添加鼠标运动监听器
this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent e) {
if (isDragged) {
//重新设置界面的位置
loc = new Point(getLocation().x + e.getX() - tmp.x,
getLocation().y + e.getY() - tmp.y);
setLocation(loc);
}
}
});
}
3.添加界面最下化与关闭按钮:
//处理关闭窗口按钮
//com为界面的容器,exit为按钮JButton
com.add(exit);
exit.setBounds(300, 1, 37, 20);
exit.setBorder(null);
exit.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_exit.jpg")));
exit.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
exit.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_exit2.jpg")));
}
public void mouseExited(MouseEvent e) {
exit.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_exit.jpg")));
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 1){
System.exit(0);
}
}
});
//处理最小化窗口按钮
//com为界面的容器,minux为按钮JButton
com.add(minux);
minux.setBounds(272,1, 29, 20);
minux.setBorder(null);
minux.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_minux.jpg")));
minux.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
minux.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_minux2.jpg")));
}
public void mouseExited(MouseEvent e) {
minux.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_minux.jpg")));
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 1){
//从界面管理容器中取得主界面,然后最小化
FaceManager.getFace("login").setExtendedState(JFrame.ICONIFIED);
}
}
});
4.要实现界面为一张图片,主界面的布局要设置为null,然后用一个JLabel来放置背景图片。
URL url1 = this.getClass().getResource("/p_w_picpath/login.jpg");
background = new JLabel(new ImageIcon(url1));
Container com = this.getContentPane();
com.setLayout(null);
com.add(background);
background.setBounds(0, 0, 340, 256);
//下面是添加可以移动的方法
setDragable();
全部代码如下
- public class ClientLogin extends JFrame implements ActionListener {
- private static final long serialVersionUID = 1L;
- public static String serverIp = "127.0.0.1";
- JLabel background,jl_uname,jl_psd,jl_ip;
- JPanel con,control;
- JTextField username,ipaddr;
- JPasswordField password;
- JButton jb_login,jb_cannel,jb_regist;
- JButton exit = new JButton();
- JButton minux = new JButton();
- Point loc = null;
- Point tmp = null;
- boolean isDragged = false;
- //拖动窗体的方法
- private void setDragable() {
- this.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseReleased(java.awt.event.MouseEvent e) {
- isDragged = false;
- setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
- }
- public void mousePressed(java.awt.event.MouseEvent e) {
- tmp = new Point(e.getX(), e.getY());
- isDragged = true;
- setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
- }
- });
- this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
- public void mouseDragged(java.awt.event.MouseEvent e) {
- if (isDragged) {
- loc = new Point(getLocation().x + e.getX() - tmp.x,
- getLocation().y + e.getY() - tmp.y);
- setLocation(loc);
- }
- }
- });
- }
- //构造函数
- public ClientLogin() {
- FaceManager.addFace("login", this);
- URL url1 = this.getClass().getResource("/p_w_picpath/login.jpg");
- background = new JLabel(new ImageIcon(url1));
- con = new JPanel(null);
- jl_uname = new JLabel("用户名:",JLabel.CENTER);
- jl_psd = new JLabel("密码:",JLabel.CENTER);
- username = new JTextField();
- password = new JPasswordField();
- jl_ip = new JLabel("服务IP:");
- ipaddr = new JTextField(serverIp);
- con.add(jl_uname);
- con.add(username);
- con.add(jl_psd);
- con.add(password);
- con.add(jl_ip);
- con.add(ipaddr);
- control = new JPanel();
- //处理南部
- URL url2 = this.getClass().getResource("/p_w_picpath/denglu.gif");
- jb_login = new JButton(new ImageIcon(url2));
- jb_login.addActionListener(this);
- URL url3 = this.getClass().getResource("/p_w_picpath/quxiao.gif");
- jb_cannel = new JButton(new ImageIcon(url3));
- jb_cannel.addActionListener(this);
- URL url4 = this.getClass().getResource("/p_w_picpath/xiangdao.gif");
- jb_regist = new JButton(new ImageIcon(url4));
- jb_regist.addActionListener(this);
- //control.setBackground(Color.pink);
- Container com = this.getContentPane();
- com.setLayout(null);
- com.add(jl_uname);
- jl_uname.setBounds(50, 120, 50,20);
- com.add(username);
- username.setBounds(110, 120, 150, 20);
- com.add(jl_psd);
- jl_psd.setBounds(50, 155, 50,20);
- com.add(password);
- password.setBounds(110, 155, 150, 20);
- com.add(jl_ip);
- jl_ip.setBounds(50, 190, 50,20);
- com.add(ipaddr);
- ipaddr.setBounds(110, 190, 150, 20);
- com.add(jb_login);
- jb_login.setBounds(40, 225, 66, 20);
- com.add(jb_cannel);
- jb_cannel.setBounds(130, 225, 66, 20);
- com.add(jb_regist);
- jb_regist.setBounds(220, 225, 66, 20);
- com.add(background);
- background.setBounds(0, 0, 340, 256);
- setDragable();
- //处理关闭窗口按钮
- com.add(exit);
- exit.setBounds(300, 1, 37, 20);
- exit.setBorder(null);
- exit.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_exit.jpg")));
- exit.addMouseListener(new MouseAdapter() {
- public void mouseEntered(MouseEvent e) {
- exit.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_exit2.jpg")));
- }
- public void mouseExited(MouseEvent e) {
- exit.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_exit.jpg")));
- }
- public void mouseClicked(MouseEvent e) {
- if(e.getClickCount() == 1){
- System.exit(0);
- }
- }
- });
- //处理最小化窗口按钮
- com.add(minux);
- minux.setBounds(272,1, 29, 20);
- minux.setBorder(null);
- minux.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_minux.jpg")));
- minux.addMouseListener(new MouseAdapter() {
- public void mouseEntered(MouseEvent e) {
- minux.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_minux2.jpg")));
- }
- public void mouseExited(MouseEvent e) {
- minux.setIcon(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_minux.jpg")));
- }
- public void mouseClicked(MouseEvent e) {
- if(e.getClickCount() == 1){
- FaceManager.getFace("login").setExtendedState(JFrame.ICONIFIED);
- }
- }
- });
- this.setUndecorated(true);
- URL url5 = this.getClass().getResource("/p_w_picpath/client_title.png");
- this.setIconImage(new ImageIcon(url5).getImage());
- this.setSize(340, 256);
- this.setTitle("用户登录");
- this.setLocation(new ScreenLocation(348,250).getLocation());
- this.setVisible(true);
- this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- this.setResizable(false);
- }
- //处理事件
- public void actionPerformed(ActionEvent e) {
- if(e.getSource() == jb_cannel){
- //取消
- username.setText("");
- password.setText("");
- ipaddr.setText("");
- }else if(e.getSource() == jb_login){
- String ip = ipaddr.getText().trim();
- if(ip != null && ip.length()>0)
- {
- serverIp = ip;
- }
- User u = new User();
- u.setUsername(username.getText().trim());
- u.setPassword(new String(password.getPassword()));
- u.setAction(1);
- //检测用户
- AlmClientUser qcu = new AlmClientUser();
- int result = qcu.checkUser(u);
- if(1 == result){
- new ClientFace(u.getUsername());
- //关闭登陆页面
- this.dispose();
- }else if(2 == result){
- JOptionPane.showMessageDialog(this, "连接服务器的ip错误,暂时无法连接,请稍后再试");
- }else if(3 == result){
- JOptionPane.showMessageDialog(this, "服务器暂时关闭,请稍后再试");
- }else if(4 == result){
- JOptionPane.showMessageDialog(this, "无法与服务器通信,请稍后再试");
- }else{
- JOptionPane.showMessageDialog(this, "用户名或密码错误");
- }
- }else if(e.getSource() == jb_regist){
- //注册
- UserReg reg = new UserReg(this);
- reg.setVisible(true);
- }
- }
- }