区域可供拖曳 Frame。
清单 4. Frame 修饰的完整示例
import java.awt.*;
import java.awt.event.*;
public class FrameTest {
static Point origin = new Point();
public static void main (String args[]) {
final Frame frame = new Frame();
frame.setUndecorated(true);
frame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
origin.x = e.getX();
origin.y = e.getY();
}
});
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point p = frame.getLocation();
frame.setLocation(
p.x + e.getX() - origin.x,
p.y + e.getY() - origin.y);
}
});
frame.setSize(300, 300);
Button b1 = new Button("Maximize");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
}
});
Button b2 = new Button("Iconify");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Preserve maximizing
frame.setExtendedState(Frame.ICONIFIED
| frame.getExtendedState());
}
});
Button b3 = new Button("Normal");
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setExtendedState(Frame.NORMAL);
}
});
Button b4 = new Button("Close");
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
frame.setLayout(new GridLayout(5,1));
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.add(b4);
frame.show();
}
}
|