1)swing是个什么呢?(翅膀?)2)if (e.getSource() instanceof Component) {的instanceof是什么语法?
按理,以下两句,是不是就是确保e是Component的后代?
if (e.getSource() instanceof Component) {
com = (Component)e.getSource();
3)e = SwingUtilities.convertMouseEvent(com, e, this);是不是在此切换坐标系呢?import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.SwingUtilities;class Win extends Frame
implements MouseListener, MouseMotionListener {
private static final long serialVersionUID = 1L;
Button button;
TextField text;
int x, y;
boolean move = false;
Win() {
button = new Button("用鼠标拖动我");
text = new TextField("用鼠标拖动我");
button.addMouseListener(this);
button.addMouseMotionListener(this);
text.addMouseListener(this);
text.addMouseMotionListener(this);
addMouseMotionListener(this);
setLayout(new FlowLayout());
add(button);
add(text);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setBounds(10, 10, 350, 300);
setVisible(true);
validate();
} @Override
public void mouseDragged(MouseEvent e) {
Component com = null;
if (e.getSource() instanceof Component) {
com = (Component)e.getSource();
if (com != this) { //假如不是在窗口上拖动鼠标
move = true;
}
move = true;
e = SwingUtilities.convertMouseEvent(com, e, this); //拖动鼠标事件到窗口
if (move) {
x = e.getX();
y = e.getY();
int w = com.getSize().width;
int h = com.getSize().height;
com.setLocation(x-w/2, y-h/2);
}
}
} @Override
public void mouseMoved(MouseEvent e) {
} @Override
public void mouseClicked(MouseEvent e) {
} @Override
public void mousePressed(MouseEvent e) {
} @Override
public void mouseReleased(MouseEvent e) {
move = false;
} @Override
public void mouseEntered(MouseEvent e) {
} @Override
public void mouseExited(MouseEvent e) {
}
}public class Example7_u {
public static void main(String args[]) {
new Win();
}
}
按理,以下两句,是不是就是确保e是Component的后代?
if (e.getSource() instanceof Component) {
com = (Component)e.getSource();
3)e = SwingUtilities.convertMouseEvent(com, e, this);是不是在此切换坐标系呢?import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.SwingUtilities;class Win extends Frame
implements MouseListener, MouseMotionListener {
private static final long serialVersionUID = 1L;
Button button;
TextField text;
int x, y;
boolean move = false;
Win() {
button = new Button("用鼠标拖动我");
text = new TextField("用鼠标拖动我");
button.addMouseListener(this);
button.addMouseMotionListener(this);
text.addMouseListener(this);
text.addMouseMotionListener(this);
addMouseMotionListener(this);
setLayout(new FlowLayout());
add(button);
add(text);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setBounds(10, 10, 350, 300);
setVisible(true);
validate();
} @Override
public void mouseDragged(MouseEvent e) {
Component com = null;
if (e.getSource() instanceof Component) {
com = (Component)e.getSource();
if (com != this) { //假如不是在窗口上拖动鼠标
move = true;
}
move = true;
e = SwingUtilities.convertMouseEvent(com, e, this); //拖动鼠标事件到窗口
if (move) {
x = e.getX();
y = e.getY();
int w = com.getSize().width;
int h = com.getSize().height;
com.setLocation(x-w/2, y-h/2);
}
}
} @Override
public void mouseMoved(MouseEvent e) {
} @Override
public void mouseClicked(MouseEvent e) {
} @Override
public void mousePressed(MouseEvent e) {
} @Override
public void mouseReleased(MouseEvent e) {
move = false;
} @Override
public void mouseEntered(MouseEvent e) {
} @Override
public void mouseExited(MouseEvent e) {
}
}public class Example7_u {
public static void main(String args[]) {
new Win();
}
}
Swing是基于Graphics的一个界面控件包,
其中绝大部分都是轻量级控件,
(即都是用Graphics自己绘制的,所有操作系统上的外观都是一致的)
只有JFrame,JDialog等很少数的是重量级
(即取自操作系统的控件,外观决定于不同的操作系统)
不像以前的awt都是重量级控件2)
A instanceof B
就是判断A对象是否为B类(接口)的一个实例
如果A为B类(或B的子类,或实现了B接口的类)的一个实例,则返回true
否则返回false3)
convertMouseEvent(A控件, e, B控件);
没用过,不过从API说明上理解
就是把鼠标事件e的x,y坐标,从相对于A控件的坐标,转换成相对于B控件的坐标
然后返回一个新的鼠标事件
比如当前事件点相对于A控件为(10,10)的位置
现在转成相对于B控件,可能坐标就成了(20,20)
button = new Button("用鼠标拖动我");
text = new TextField("用鼠标拖动我");
button.addMouseListener(this);
button.addMouseMotionListener(this);
text.addMouseListener(this);
text.addMouseMotionListener(this);
setLayout(new FlowLayout());
add(text);
add(button);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setBounds(10, 10, 350, 300);
setVisible(true);
validate();
}@Override
public void mouseDragged(MouseEvent e) {
Object obj = e.getSource();
if (obj instanceof Component) {
Component com = (Component)obj;
a = com.getBounds().x;
b = com.getBounds().y;
x = e.getX();
y = e.getY();
a += x;
b += y;
com.setLocation(a-x0, b-y0);
}
}@Override
public void mousePressed(MouseEvent e) {
Component com = null;
com = (Component)e.getSource();
a = com.getBounds().x;
b = com.getBounds().y;
x0 = e.getX();
y0 = e.getY();
}