2013-09-27 13:58:59Morris
[JAVA] 實作拖曳功能
如影片所述,寫一個拖曳物件功能。
事實上就是撰寫一個 listener 去聆聽滑鼠的指令,並且這個 class 要能運用在相對位置上的處理。
這樣不會侷限於單一程式使用。
首先,先來貼核心的 Listener。
-> 詳細說明請參照
http://javapub.iteye.com/blog/739146
http://hi.baidu.com/wwwpeng584/item/668f6ef7289020db6225d24a。
e.getSource() 可以抓到誰將使用這個 listener。
SwingUtilities 功能很強大,特別要注意,視窗排版都是按照相對位置去計算的。
因此處理上要將其找到適合的相對位置於移動。
//最近要組一個大的程式,慢慢找到想要的功能,以及盡可能減少相依性的寫法。
package package1;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;
/**
* more explain by Chinese language : http://javapub.iteye.com/blog/739146 &
* http://hi.baidu.com/wwwpeng584/item/668f6ef7289020db6225d24a
*
* @author owner
*
*/
public class DragObjectListener implements MouseInputListener {
Point p = new Point(0, 0); // 坐标点
public DragObjectListener() {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
// 如果是左键则做一下操作
if (SwingUtilities.isLeftMouseButton(e)) {
Component jl = (Component) e.getSource();
// 坐标转换
Point newP = SwingUtilities.convertPoint(jl, e.getPoint(),
jl.getParent()); // 转换坐标系统
jl.setLocation(jl.getX() + (newP.x - p.x), jl.getY()
+ (newP.y - p.y)); // 设置标签的新位置
p = newP; // 更改坐标点
// jl.getParent().repaint();
} else if (SwingUtilities.isRightMouseButton(e)) {// 右键操作
} else if (SwingUtilities.isMiddleMouseButton(e)) {// 中键操作,一般鼠标没有
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
Component source = (Component) e.getSource();
((MovePanel)source).setSelected(false);
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (e.getClickCount() == 2) {
Component source = (Component) e.getSource();
((MovePanel)source).setSelected(true);
}
}
}
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
Component jl = (Component) e.getSource();
p = SwingUtilities.convertPoint(jl, e.getPoint(), jl.getParent()); // 得到当前坐标点
// jl.getParent().repaint();
} else if (SwingUtilities.isRightMouseButton(e)) {// 右键操作
} else if (SwingUtilities.isMiddleMouseButton(e)) {// 中键操作,一般鼠标没有
}
}
}
package package1;
import java.awt.*;
import javax.swing.*;
public class MainFrame extends JFrame {
private static int WIDTH = 500, HEIGHT = 300;
MainFrame() {
System.out.println("START");
this.setTitle("MoveObject");
this.setSize(WIDTH, HEIGHT);
this.setResizable(false);
JPanel left, right;
left = new JPanel();
right = new JPanel();
left.setLayout(new BorderLayout());
left.add(new JButton("Button"), BorderLayout.NORTH);
left.add(new MovePanel(), BorderLayout.SOUTH);
right.add(new JButton("Foo"));
this.setLayout(new BorderLayout());
this.add(left, BorderLayout.CENTER);
this.add(right, BorderLayout.WEST);
//this.setLayout(null);
//this.add(new MovePanel());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}
}
package package1;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
public class MovePanel extends JPanel {
private boolean selected;
private int positionX = 0, positionY = 0;
private Point prevPressedPoint;
MovePanel() {
this.setBounds(50, 50, 100, 100);
LineBorder blackline = new LineBorder(Color.BLACK);
this.setBorder(blackline);
JTextArea textarea = new JTextArea("TextArea");
textarea.setEditable(false);
this.add(textarea);
DragObjectListener listener = new DragObjectListener();
this.addMouseListener(listener);
this.addMouseMotionListener(listener);
}
public void setSelected(boolean flag) {
selected = flag;
System.out.println(flag);
}
}