使用Java Swing实现QQ管家界面

学会JavaSwing构建程序界面后,小伙伴们最大的困惑可能是“为什么我们做出来的界面那么丑、不跟市面流行的程序界面一样呢?”
在这里插入图片描述
像这个界面,我们发现标题栏跟默认状态不一样,关闭按钮的样式也变了。
实现的方法其实很简单,使用setUndecorated(true);取消窗体装饰,其他效果通过图片完成即可。
下面的代码实现了无标题栏窗体,使用背景拉伸的技术为处于上部分的面板设置了背景图片;实现了关闭按钮的鼠标经过以及鼠标单击事件;实现了鼠标拖拽新标题栏移动整个窗体的功能。
其他功能及特效大家可以自行实现。

Constants.java

/**
 * @Description: 用来保存常量 
 * @author: 老九学堂·窖头   
 * @date:   2017年12月25日 下午2:47:31   
 * @version V1.0 
 * @Copyright: 2017 http://www.xuetang9.com Inc. All rights reserved.
 */
public class Constants {
    /** 全局字体名称 */
    public static String SysFontName = "宋体";
    /** 登录窗体的宽 */
    public static int Width_LoginFrame = 387;
    /** 登录窗体的高 */
    public static int Height_LoginFrame = 266;
}

LoginFrame.java

/**
 * @Description: 登录界面 
 * @author: 老九学堂·窖头   
 * @date:   2017年12月25日 下午2:40:07   
 * @version V1.0 
 * @Copyright: 2017 http://www.xuetang9.com Inc. All rights reserved.
 */
public class LoginFrame extends JFrame{
    private JPanel pnlTop = new TopPanel("images/sknin1.jpg");
    private JPanel pnlMiddle = new JPanel();
    private JPanel pnlBottom = new JPanel();
    private JPanel contentPane = null;
    private BorderLayout contentPaneLayout = null;  //内容面板的边框布局
    private Point mousePressedPoint;    //点击pnlTop面板时记录下的鼠标坐标

    public LoginFrame(){
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);   //关闭窗体时什么也不做
        setTitle("登录电脑管家"); //设置窗体标题
        setSize(Constants.Width_LoginFrame, Constants.Height_LoginFrame);       //这里的窗体大小可以参考图片素材的大小(界面素材需复制到images文件夹下)
        initComponents();       //调用自定义方法初始化窗体上的组件
        setLocationRelativeTo(null);    //设置窗体居中
        setUndecorated(true);           //设置窗体无装饰为真
    }

    private void initComponents() {
        contentPane = new JPanel();
        contentPaneLayout = new BorderLayout();
        contentPane.setLayout(contentPaneLayout);

        /**********************  start of 设置pnlTop相关控件  *************************/
        JLabel lblTitle = new JLabel("   登录电脑管家");
        lblTitle.setFont(new Font(Constants.SysFontName, Font.PLAIN, 14));
        lblTitle.setForeground(Color.WHITE);
        //网格包布局
        GridBagLayout pnlTopLayout = new GridBagLayout();
        pnlTop.setLayout(pnlTopLayout);
        pnlTop.add(lblTitle);
        JLabel lblClose = new JLabel();
        lblClose.setIcon(new ImageIcon("images/close.png"));
        pnlTop.add(lblClose);       
        //设置网格包布局的规则
        GridBagConstraints grConstraints = new GridBagConstraints();
        grConstraints.insets = new Insets(0, 0, 0,245);//设置四方向边距    
        pnlTopLayout.setConstraints(lblTitle, grConstraints);//为控件设置新规则
        //越过布局设置控件的宽高
        pnlTop.setPreferredSize(new Dimension(Constants.Width_LoginFrame, 30));
        contentPane.add(pnlTop, BorderLayout.NORTH);

        lblClose.addMouseListener(new MouseAdapter() {//关闭按钮图片替换
            ImageIcon icon = new ImageIcon("images/close.png");
            @Override
            public void mouseEntered(MouseEvent e) {
                lblClose.setIcon(null);
                lblClose.setForeground(Color.RED);
                lblClose.setText("X");  //没有其他图片素材,使用X字母模拟实现切换效果
                lblClose.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                lblClose.setIcon(icon);
            }
            @Override
            public void mouseClicked(MouseEvent e) {
                int result = JOptionPane.showConfirmDialog(null, "确认关闭吗?", "窗口关闭",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
                if(result == 0) System.exit(0);
            }
        });

        pnlTop.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                //鼠标点击时先记录鼠标的坐标
                mousePressedPoint = e.getPoint();
            }
        });
        pnlTop.addMouseMotionListener(new MouseAdapter() {          
            @Override
            public void mouseDragged(MouseEvent e) {
                //获得窗体当前的坐标
                Point p = getLocation();
                //设置窗体坐标:当前坐标+鼠标移动后的当前坐标-鼠标原坐标  == 当前坐标+鼠标移动距离
                setLocation((int)(p.getX() + e.getX() - mousePressedPoint.getX()), (int)(p.getY() + e.getY() - mousePressedPoint.getY()));              
            }
        });
        /**********************  end of 设置pnlTop相关控件  *************************/

        this.setContentPane(contentPane);
    }


    class TopPanel extends JPanel{//重写上部面板(实现了背景图片拉伸效果)
        private ImageIcon background;
        public TopPanel(String backImagePath) {
            if(null == backImagePath) return;
            background = new ImageIcon(backImagePath);

        }
        @Override
        protected void paintComponent(Graphics g) {//重绘组件
            if(background == null) return;
            //拉伸图片
            g.drawImage(background.getImage(), 0, 0, Constants.Width_LoginFrame, background.getIconHeight(), null);
        }
    }



    public static void main(String[] args) {
        new LoginFrame().setVisible(true);
    }
}

QQ电脑管家界面效果及素材下载

安装遇到问题,可加老九君个人QQ:614940318,请备注来自CSDN
老九学堂免费C、C++、Java课程地址: https://study.163.com/courses-search?keyword=老九学堂

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己写的,望多指教。import java.io.*; import java.net.*; public class link implements Runnable { public ServerSocket serversocket=null;//服务器套接字对象 public Socket socket=null;//客户端套接字 public PrintWriter out=null;//输出流对象 public BufferedReader in=null;//输入流对象 public DataOutputStream tsf=null;//输出流对象 public DataInputStream rcv=null;//输入流对象 public FileOutputStream fos=null;//文件输入流 public FileInputStream fis=null;//文件输出流 byte[] buf; public int patch,patch1; link(){} //创立连接 public void Screateconnect() { try { serversocket=new ServerSocket(9999);//ServerSocket对象监听端口 socket=serversocket.accept();//ServerSocket对象监听到端口并创建套接字 serversocket.close(); } catch(IOException e) { System.out.print("create fail!"); } } public void Ccreateconnect(String ip) { try { socket=new Socket(ip,9999);//创建套接字 } catch(Exception e) { System.out.print("创立连接失败!"); } } //管道通信 //通信内容 //1.文件接发请求以及相关数据 //2.登录、退出信息 //3.视屏聊天请求 public void Cduct() { } public void Sduct() { output("ftn"+String.valueOf(patch)); } //进行聊天 public void output(String str) { try { out=new PrintWriter(socket.getOutputStream(),true);//打开输出流 out.println(str);//发送信息 } catch(Exception e) { System.out.print("输出流创建失败!"); } } public void run() { while(true) { try { in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//打开接受输入流 String str = in.readLine();//输出接受信息 if("ftn".equals(substring(0,3))) { patch1=Integer.parseInt(str.substring(4)); recevie(); } else { System.out.println(in.readLine()); } } catch(Exception e) { System.out.print("输入流创建失败!"); } } } //接受各类文件 public void transmission() { try { tsf=new DataOutputStream(socket.getOutputStream());//打开输出流 fis = new FileInputStream("E:\\程序\\java\\1.AVI"); buf = new byte[1024]; long streamnum=fis.available(); int c=(int) Math.floor(streamnum/1024); patch=(int)streamnum24; Sduct();//发出尾数 if(c>0) { for(int i=0;i<c;i++) { fis.read(buf); tsf.write(buf);//发送信息 tsf.flush(); } } byte[] buff=new byte[c]; System.out.print(buff.length+"\n"); fis.read(buff); tsf.write(buff);//发送信息 tsf.flush(); tsf.close(); fis.close(); } catch(Exception e) { e.printStackTrace(); } } public void recevie() { try { rcv=new DataInputStream(socket.getInputStream());//打开接受输入流 fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\1.avi",true);System.out.print("12\n"); buf = new byte[1024]; int c=0; int j=0; if(rcv.available()>1024) { while((c=rcv.read(buf))!=-1) { j++; fos.write(buf); fos.flush(); if(rcv.available()<1024)break; } } buf = new byte[patch1]; while((c=rcv.read(buf))!=-1) { fos.write(buf); fos.flush(); } rcv.close(); fos.close(); } catch(Exception e) { System.out.print("输入流创建失败!2"); } } public void close() { try { in.close(); socket.close(); }catch(Exception ex) { System.out.print("关闭失败"); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值