Java实现的可选择及拖拽图片的面板功能【基于swing组件】

2025-05-27 0 102

本文实例讲述了java实现的可选择及拖拽图片的面板功能。分享给大家供大家参考,具体如下:

今天在论坛上看到帖子希望能在 swing 中实现像拖地图一样拖拽图片。这里是一个最简单的实现,提供了一个基本思路。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124
import javax.swing.*;

import javax.swing.filechooser.filenameextensionfilter;

import java.awt.*;

import java.awt.event.mouseevent;

import java.awt.event.mouselistener;

import java.awt.event.mousemotionlistener;

import java.io.file;

/**

* 在窗体上拖拽图片。使用方法:双击窗体空白处将会弹出打开图片对话框。打开图片后可以在窗体上拖拽图片。

*/

@suppresswarnings("serial")

public class dragingframe extends jframe {

/**

* 构造函数

*

* @throws headlessexception ???

*/

public dragingframe() throws headlessexception {

this.setdefaultcloseoperation(exit_on_close);

getcontentpane().setlayout(new borderlayout());

getcontentpane().add(new imagepanel(), borderlayout.center);

}

// 程序入口

public static void main(string[] args) throws exception {

uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());

dragingframe frame = new dragingframe();

frame.setsize(400, 300);

frame.setlocation(300, 300);

frame.setresizable(false);

frame.settitle("www.zzvips.com 双击打开图片,然后拖拽");

frame.setvisible(true);

}

}

/**

* 能够拖拽图片的面板

*/

@suppresswarnings("serial")

class imagepanel extends jpanel {

private dragstatus status = dragstatus.ready; // 拖拽状态

private image image; // 要显示的图片

private point imageposition = new point(0, 0), // 图片的当前位置

imagestartposition = new point(0, 0), // 每次拖拽开始时图片的位置(也就是上次拖拽后的位置)

mousestartposition; // 每次拖拽开始时鼠标的位置

imagepanel() {

addmouselistener(new mouselistener() {

// 双击鼠标时打开图片

public void mouseclicked(mouseevent e) {

if (e.getclickcount() == 2) {

openimage();

}

}

// 按下鼠标时,更改状态,并且记录拖拽起始位置。

public void mousepressed(mouseevent e) {

if (status == dragstatus.ready) {

status = dragstatus.dragging;

mousestartposition = e.getpoint();

imagestartposition.setlocation(imageposition.getlocation());

}

}

// 松开鼠标时更改状态

public void mousereleased(mouseevent e) {

if (status == dragstatus.dragging) {

status = dragstatus.ready;

}

}

public void mouseentered(mouseevent e) {

}

public void mouseexited(mouseevent e) {

}

});

addmousemotionlistener(new mousemotionlistener() {

// java 有拖拽事件,在这个事件中移动图片位置

public void mousedragged(mouseevent e) {

if (status == dragstatus.dragging) {

moveimage(e.getpoint());

}

}

public void mousemoved(mouseevent e) {

}

});

}

/**

* 移动图片。实际上画图工作在 paintcomponent() 中进行,这里只是计算图片位置,然后调用该方法。

*

* @param point 当前的鼠标位置

*/

private void moveimage(point point) {

// 图片的当前位置等于图片的起始位置加上鼠标位置的偏移量。

imageposition.setlocation(

imagestartposition.getx() + (point.getx() - mousestartposition.getx()),

imagestartposition.gety() + (point.gety() - mousestartposition.gety())

);

repaint();

}

// 打开图片

private void openimage() {

system.out.println("opening image...");

file file = createfilechooser().getselectedfile();

if (file != null) {

image = toolkit.getdefaulttoolkit().getimage(file.getabsolutepath());

if (image != null) {

this.repaint();

}

}

}

// 创建打开文件对话框

private jfilechooser createfilechooser() {

jfilechooser chooser = new jfilechooser();

chooser.setdialogtitle("请选择图片文件...");

chooser.addchoosablefilefilter(new filenameextensionfilter("常用图片格式", "jpg", "jpeg", "gif", "png"));

chooser.showopendialog(this);

return chooser;

}

@override

protected void paintcomponent(graphics g) {

super.paintcomponent(g);

if (image != null) {

g.drawimage(image, (int) imageposition.getx(), (int) imageposition.gety(), this);

}

}

private enum dragstatus {

ready, dragging

}

}

运行效果:

Java实现的可选择及拖拽图片的面板功能【基于swing组件】

希望本文所述对大家java程序设计有所帮助。

原文链接:http://blog.csdn.net/yidinghe/article/details/3890410

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java实现的可选择及拖拽图片的面板功能【基于swing组件】 https://www.kuaiidc.com/76212.html

相关文章

发表评论
暂无评论