最近要通过java的swing组件实现一个功能:添加一张背景图片,之后在上面随意添加不同的可拖动的小图片标签,刚开始不太懂,绕了很大一个弯路,想着用JLayeredPane的分层去实现,但最终没能如愿,从头开始之后,思路一下清晰了起来,想到了一种非常简单的实现方式,这里分享一下代码:
只有一个类,复制粘贴之后,找几张图片,和类放在同一个目录下,在修改一下图片名称,直接运行就能看到效果了:
15362347_201401201738210265.png
package testSwing;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
public class ModuleAdd extends JFrame{
//背景图片
JPanel jpanel;
ImageIcon image;
ImageIcon icon;
Image img;
//
int x, y;//鼠标双击时的坐标
ImageIcon markerIcon; //标签图标
//更改标签图片
private Point pressPoint; //press标签时的鼠标位
final int PREVIEW_SIZE = 100;
JFrame jf = new JFrame("更改标签图片");
JButton button = new JButton("");
//以当前路径创建文件选择器
JFileChooser chooser = new JFileChooser(".");
JLabel accessory = new JLabel();
public void changeMarker(){
chooser.setAccessory(accessory);//为文件选择器指定一个预览图片的附件组件
//设置预览图片组件的大小和边框
accessory.setPreferredSize(new Dimension(PREVIEW_SIZE, PREVIEW_SIZE));
accessory.setBorder(BorderFactory.createEtchedBorder());
//用于检测被选择文件的改变事件
chooser.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent event)
{
//JFileChooser的被选文件已经发生了改变
if (event.getPropertyName() == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)
{
File f = (File) event.getNewValue(); //获取用户选择的新文件
if (f == null)
{
accessory.setIcon(null);
return;
}
ImageIcon icon = new ImageIcon(f.getPath()); //将所文件读入ImageIcon对象中
//如果图像太大,则缩小它
if(icon.getIconWidth() > PREVIEW_SIZE)
{
icon = new ImageIcon(icon.getImage()
.getScaledInstance(PREVIEW_SIZE, -1, Image.SCALE_DEFAULT));
}
//改变accessory Label的图标
accessory.setIcon(icon);
}
}
});
button.setToolTipText("选择标签");
button.setIcon(new ImageIcon(JFrameBackground.class.getResource("/testSwing/Browse-64.png")));
//为buttom增加事件
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int result = chooser.showDialog(jf , "打开"); //显示文件对话框
//如果用户选择了APPROVE(赞同)按钮,即打开,保存及其等效按钮
if(result == JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getPath();
markerIcon=new ImageIcon(name); //显示指定图片
}
}
});
}
public void addMarker() {
final JLabel lablel = new JLabel("");
lablel.setHorizontalAlignment(SwingConstants.CENTER);
lablel.setIcon(markerIcon);
lablel.setBounds(x, y, 16, 16);
jpanel.add(lablel);
repaint();
//为添加的标签增加拖动事件
lablel.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
pressPoint = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
pressPoint = null;
}
});
lablel.addMouseMotionListener(new MouseAdapter(){
@Override
public void mouseDragged(MouseEvent e) {
Point nowPoint = e.getPoint(); //当前鼠标拖拽到的鼠标位置
int diffX = nowPoint.x - pressPoint.x;
int diffY = nowPoint.y - pressPoint.y;
/**移动**/
lablel.setLocation(lablel.getX()+diffX,lablel.getY()+diffY);
System.out.println(lablel.getX()+" "+lablel.getY());
}
});
}
public void ini() {
jpanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent m) {
if (m.getClickCount() == 2) {
x = m.getPoint().x;
y = m.getPoint().y;
System.out.println("x:"+x+" y:"+y);
addMarker();
}
}
});
}
public ModuleAdd(){
setIconImage(Toolkit.getDefaultToolkit().getImage(ModuleAdd.class.getResource("/testSwing/swingTest.jpg")));
icon= new ImageIcon(SwingImgAdd.class.getResource("/testSwing/swingTest.jpg"));
img = icon.getImage();
jpanel = new JPanel(){
protected void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
}
};
jpanel.setPreferredSize(new Dimension(icon.getIconWidth(),
icon.getIconHeight()));
ini();
changeMarker();
setTitle("增加标签");
button.setBounds(0,20,98,41);
getContentPane().add(button);
getContentPane().add(new JScrollPane( jpanel));
jpanel.setLayout(null);
this.setSize(780,550);
System.out.println(this.getSize());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new ModuleAdd();
}
}
阅读(1513) | 评论(0) | 转发(0) |