package cn.com.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter; import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder; public class testDrap {
static int xx , yy;
static Point p = new Point(0, 0);
static int w,h;
static boolean b = true;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setBorder(new LineBorder(Color.black));
panel.setPreferredSize(new Dimension(260,55)); frame.setBounds(100, 100, 450, 300);
final JLabel label = new JLabel(){
public void repaint() {
this.setLocation(p);
this.setSize(w,h);
super.repaint();
}
};
LineBorder border = new LineBorder(Color.black);
label.setBorder(border);
label.setForeground(Color.black);
label.setPreferredSize(new Dimension(50,30));
label.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent arg0) {
b = false;
}
public void mousePressed(MouseEvent mouseEvent) {
xx = mouseEvent.getX();
yy = mouseEvent.getY();
w = label.getSize().width;
h = label.getSize().height;
b = true;
}
public void mouseExited(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent e) {
if (label.getSize().width-8<=e.getX()) {
label.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));//右
} else if (8>=e.getX()) {
label.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));//左
} else {
label.setCursor(Cursor.getDefaultCursor());
}
}
public void mouseClicked(MouseEvent arg0) {
}
});
label.addMouseMotionListener(new MouseAdapter() { public void mouseMoved(MouseEvent e) {
Cursor cursor = label.getCursor();
if (label.getSize().width-5<=e.getX()) {
label.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));//右
} else if (5>=e.getX()) {
label.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));//左
} else {
label.setCursor(Cursor.getDefaultCursor());
}
if (b) {
label.setCursor(cursor);
}
};
public void mouseDragged(MouseEvent e) {
p = label.getLocation();
if (label.getCursor().getType() == Cursor.W_RESIZE_CURSOR) { Point point = label.getLocation();
p.x = e.getPoint().x + point.x - 0;
p.y = point.y;
label.setLocation(p); w = label.getSize().width - e.getPoint().x;
h = label.getSize().height;
label.setSize(w,h);
} else if (label.getCursor().getType() == Cursor.E_RESIZE_CURSOR) { w = e.getPoint().x;
h = label.getSize().height;
label.setSize(w,h); label.setLocation(p);
} else {
Point point = label.getLocation();
////////////随意移动
            //p.x = e.getPoint().x + point.x - xx;
            //p.y = e.getPoint().y + point.y - yy;
//label.setLocation(e.getPoint().x + point.x - xx,e.getPoint().y + point.y - yy);
            ////////////沿着X轴方向移动
p.x = e.getPoint().x + point.x - xx;
p.y = point.y;
label.setLocation(e.getPoint().x + point.x - xx,point.y); }
}
}
);
panel.add(label);
frame.add(panel);
JButton btn = new JButton("确认");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < panel.getComponentCount(); i++) {
Component c = panel.getComponent(i);
System.out.println(c.getLocation().x);
}
}
});
frame.add(btn);
frame.setVisible(true);
}
}

java实现控件的移动及使用鼠标改变控件大小的更多相关文章

  1. (转)C# WinForm中 获得当前鼠标所在控件 或 将窗体中鼠标所在控件名显示在窗体标题上

    原文地址:http://www.cnblogs.com/08shiyan/archive/2011/04/14/2015758.html /********************** * 课题:将窗 ...

  2. c# asp.net 鼠标改变控件坐标位置,更改控件坐标,注册表保存读取,打印,查找局域网内打印机等等收集

    界面虽然被我弄的很难看,但功能还可以 里边注册表的路径自己设置一下,或者加一个创建注册表的语句,不然会报错 前台: <%@ Page Language="C#" AutoEv ...

  3. WinForm开发-界面控件到实体,实体到界面控件自动绑定

    在WinForm开发中,我们是不是为绑定界面控件的数据而每个控件每个控件的赋值?在保存修改时是不是也是每个控件每个控件的赋值到实体中?字段一多,那简直就是噩梦.有没有像Web中那样方便的方法直接就自动 ...

  4. VC 使用OnCtlColor函数来改变控件颜色(引用)

    在MFC类库提供了CWnd::OnCtlColor函数,在工作框架的子窗口被重画时将调用该成员函数.因此可以重载WM_CTLCOLOR消息的响应函数.此函数的原型:afx_msg HBRUSH OnC ...

  5. WinForm------DockManager控件的使用方法(里面包含DockPanel控件)

    1.在"引用"中添加DevExpress.XtraBars和DexExpress.XtraNavBar程序集 2.往工具栏拖出DockManager控件,点击右上角的小三角,再点击 ...

  6. MFC中改变控件的大小和位置

    用CWnd类的函数MoveWindow()或SetWindowPos()可以改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...

  7. wxpython 中 用鼠标拖动控件 总结

    #encoding: utf-8 import wx import os import noname class Frame( noname.MyFrame1 ): def __init__(self ...

  8. Asp.Net 如何获取所有控件&如何获取指定类型的所有控件

    一. Asp.Net Page页面中访问所有控件的属性为: Page.Controls 控件的结构是树结构. 二.获取指定类型所有控件实例: 1.递归方法定义: private void GetCon ...

  9. 在WinForm中使用委托来在其他线程中改变控件的显示

    假设winform中有两个控件: 1.ListView用来显示进度的文本提示,ID:listView_progressInfo 2.ProgressBar用来显示进度,ID:progressBar1 ...

随机推荐

  1. Storm介绍及安装部署

    本节内容: Apache Storm是什么 Apache Storm核心概念 Storm原理架构 Storm集群安装部署 启动storm ui.Nimbus和Supervisor 一.Apache S ...

  2. 加载Assetbundle需要注意的地方

    WWW:异步实现,手机上不能用于同步代码,需要监测其完成状态.不用www.dispose. CreateFromFile:阻塞,但是移动平台上面的路径格式有点坑,没时间看,不用. 以下两个方式需要先使 ...

  3. 20172328 2018-2019《Java软件结构与数据结构》第五周学习总结

    20172328 2018-2019<Java软件结构与数据结构>第五周学习总结 概述 Generalization 本周学习了第九章:排序与查找,主要包括线性查找和二分查找算法和几种排序 ...

  4. django——模型层之单表

    1.ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...

  5. lArea.js 城市选择

    http://blog.csdn.net/libin_1/article/details/50689075 lArea.js

  6. centos7.4中安装docker

    #!/bin/sh # 安装docker # 在docker中安装mysql # 解决了docker容器中无法输入中文的问题 ##########################安装docker # ...

  7. file_get_contents 在本地测试可以, 但在服务器上报错403

    解决方法: Most likely if you don't get any content while accessing an webpage, probably it doesn't want ...

  8. crawlspider

    Scrapy中CrawSpider 回头看: 之前的代码中,我们有很大一部分时间在寻找下一页的url地址或者是内容的url地址或者是内容的url地址上面,这个过程能更简单一些么? 思路: 1. 从re ...

  9. javascript的数组之includes()

    includes()方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回true,否则返回false. var array1 = [1, 2, 3]; console.log(array ...

  10. @ConfigurationProperties注解取消location属性

    当我正在自学如何自定义properties配置文件,为了防止不必要的麻烦,重新创建了一个新的properties文件 fu.properties 然后在自定义的配置类中引入fu.properties/ ...