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. java 读取本地文件并转换为byte数组

    private byte[] InputStream2ByteArray(String filePath) throws IOException { InputStream in = new File ...

  2. Spring Boot Mybatis整合

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 特 ...

  3. 使用CompletionService结合ExecutorService批处理调用存储过程任务实例

    此实例为java多线程并发调用存储过程实例,只做代码记载,不做详细描述 1.线程池构造初始化类CommonExecutorService.java package com.pupeiyuan.go; ...

  4. linux 硬盘满了如何处理

    事件源于在服务器运行一个脚本程序… 好好的脚本突然报错,还以为脚本出现问题了.细看报错原因(具体报错信息已经忘记了),是没有可用空间.从没遇见过这个情况,怎么办呢? 一.确定是不是真的是磁盘空间不足 ...

  5. log4net可视化查询

    转自:https://www.cnblogs.com/huangxincheng/p/9120028.html 小步快跑的公司可以最简化操作直接通过log4net将日志写入ElasticSearch ...

  6. DPI与DFI技术分析

    DPI全称为“Deep Packet Inspection”,称为“深度包检测”.DPI技术在分析包头的基础上,增加了对应用层的分析,是一种基于应用层的流量检测和控制技术,当IP数据包.TCP或UDP ...

  7. win10,vs2017+mpi v10.0 安装与入门 (详细)

    一.安装visual studio 2017  下载 地址:https://visualstudio.microsoft.com/zh-hans/vs/ 安装 我们再选择自己想要安装的东西,确认好后点 ...

  8. 如何运行后台Service?

    Unless you specify otherwise, most of the operations you do in an app run in the foreground on a spe ...

  9. [CF542D]Superhero's Job

    [CF542D]Superhero's Job 题目大意: 定义函数 \[ J(x) = \sum_{\substack{1 \leq k \leq x \\ k \mid x \\ \gcd \le ...

  10. SDOI2018:原题识别

    题解: https://files.cnblogs.com/files/clrs97/old-solution.pdf Code: #include<cstdio> #include< ...