📄 test.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
JDesktopPane desktopPane = new JDesktopPane();
public void init() {
Container contentPane = getContentPane();
contentPane.add(desktopPane, BorderLayout.CENTER);
desktopPane.setDesktopManager(new OutlineManager());
JInternalFrame jif = new JInternalFrame(
"Outline Drag and Resize", // title
true, // resizable
true, // closable
true, // maximizable
true); // iconifiable
jif.setBounds(10, 10, 250, 100);
desktopPane.add(jif);
}
}
class OutlineManager extends DefaultDesktopManager {
private Rectangle start, last;
private boolean first = true;
// dragging ...
public void beginDraggingFrame(JComponent frame) {
initializeOutline(frame);
}
public void dragFrame(JComponent frame, int x, int y) {
updateOutline(frame, x, y, start.width, start.height);
}
public void endDraggingFrame(JComponent frame) {
endOutline(frame);
}
// resizing ...
public void beginResizingFrame(JComponent frame, int dir) {
initializeOutline(frame);
}
public void resizeFrame(JComponent frame,
int x, int y, int w, int h) {
updateOutline(frame, x, y, w, h);
}
public void endResizingFrame(JComponent frame) {
endOutline(frame);
}
// outline ...
private void initializeOutline(final JComponent frame) {
// the call to setVisible() calls repaint, which
// places a paint event on the event queue.
// therefore, the effect of the setVisible() call is
// not apparent until after this method returns
frame.setVisible(false);
start = frame.getBounds();
last = new Rectangle(start);
first = true;
// the Runnable below paints the initial outline
// after the repaint event spawned by setVisible() is
// handled
SwingUtilities.invokeLater(new Runnable() {
public void run() {
updateOutline(frame,start.x,start.y,
start.width,start.height);
}
});
}
private void updateOutline(JComponent frame,
int x, int y, int w, int h) {
Container container = frame.getParent();
Graphics g = container.getGraphics();
try {
g.setXORMode(container.getBackground());
if( ! first) {
g.drawRect(last.x, last.y,
last.width-1, last.height-1);
}
g.drawRect(x, y, w-1, h-1);
first = false;
}
finally {
g.dispose();
last.setBounds(x,y,w,h);
}
}
private void endOutline(JComponent frame) {
frame.setVisible(true);
setBoundsForFrame(
frame, last.x, last.y, last.width, last.height);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -