下面列出了怎么用javax.swing.plaf.basic.BasicInternalFrameUI的API类实例代码及写法,或者点击链接到github查看源代码。
private static void addFrame(JDesktopPane desktop, int idx) {
String titleAlignment = idx == 0 ? "CENTER" : "LEADING";
JInternalFrame frame = new JInternalFrame("title: " + titleAlignment, true, true, true, true);
BasicInternalFrameUI ui = (BasicInternalFrameUI) frame.getUI();
JComponent titleBar = ui.getNorthPane();
UIDefaults d = new UIDefaults();
d.put("InternalFrame:InternalFrameTitlePane.titleAlignment", titleAlignment);
titleBar.putClientProperty("Nimbus.Overrides", d);
frame.add(makePanel());
frame.setSize(240, 100);
frame.setVisible(true);
frame.setLocation(10 + 60 * idx, 10 + 120 * idx);
desktop.add(frame);
desktop.getDesktopManager().activateFrame(frame);
}
private MainPanel() {
super(new BorderLayout());
// title, resizable, closable, maximizable, iconifiable
JInternalFrame immovableFrame = new JInternalFrame("immovable", false, false, true, true);
Component north = ((BasicInternalFrameUI) immovableFrame.getUI()).getNorthPane();
MouseMotionListener[] actions = north.getListeners(MouseMotionListener.class);
for (MouseMotionListener l: actions) {
north.removeMouseMotionListener(l);
}
// immovableFrame.setLocation(0, 0);
immovableFrame.setSize(160, 0);
desktop.add(immovableFrame);
immovableFrame.setVisible(true);
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
desktop.addComponentListener(new ComponentAdapter() {
@Override public void componentResized(ComponentEvent e) {
immovableFrame.setSize(immovableFrame.getSize().width, e.getComponent().getSize().height);
}
});
add(desktop);
add(createMenuBar(), BorderLayout.NORTH);
setPreferredSize(new Dimension(320, 240));
}
protected static void removeSystemMenuListener(JInternalFrame modal) {
BasicInternalFrameUI ui = (BasicInternalFrameUI) modal.getUI();
Container titleBar = ui.getNorthPane();
Stream.of(titleBar.getComponents())
.filter(c -> c instanceof JLabel || "InternalFrameTitlePane.menuButton".equals(c.getName()))
.forEach(MainPanel::removeComponentMouseListener);
// for (Component c: titleBar.getComponents()) {
// if (c instanceof JLabel || "InternalFrameTitlePane.menuButton".equals(c.getName())) {
// removeComponentMouseListener(c);
// }
// }
}
@Override public void updateUI() {
super.updateUI();
BasicInternalFrameUI ui = (BasicInternalFrameUI) getUI();
Component titleBar = ui.getNorthPane();
for (MouseMotionListener l: titleBar.getListeners(MouseMotionListener.class)) {
titleBar.removeMouseMotionListener(l);
}
DragWindowListener dwl = new DragWindowListener();
titleBar.addMouseListener(dwl);
titleBar.addMouseMotionListener(dwl);
}
private MainPanel() {
super(new BorderLayout());
JInternalFrame f0 = new JInternalFrame("metal(default)", true, true, true, true);
// TEST: f0.setUI(new BasicInternalFrameUI(f0));
f0.setSize(240, 100);
f0.setLocation(20, 10);
f0.setVisible(true);
JInternalFrame f1 = new JInternalFrame("basic", true, true, true, true) {
@Override public void updateUI() {
super.updateUI();
setUI(new BasicInternalFrameUI(this) {
@Override protected JComponent createNorthPane(JInternalFrame w) {
return new BumpsFreeInternalFrameTitlePane(w);
}
});
}
};
f1.setSize(240, 100);
f1.setLocation(40, 120);
f1.setVisible(true);
JDesktopPane desktop = new JDesktopPane();
desktop.add(f0);
desktop.add(f1);
add(desktop);
setPreferredSize(new Dimension(320, 240));
}
/**
* Updates the snapshot of the specified internal frame.
*
* @param frame
* Internal frame.
*/
private void updateSnapshot(JInternalFrame frame) {
if (!frame.isShowing())
return;
// Draw the current state of the internal frame to a
// temp image (w/o border and decorations). It would be nice
// to use Robot, but this frame may be partially obscured,
// so we take our chances that the frame will be properly
// drawn by the user code.
int frameWidth = frame.getWidth();
int frameHeight = frame.getHeight();
int dx = 0;
int dy = 0;
// Now we need to remove the border and the title pane :)
Border internalFrameBorder = UIManager.getBorder("InternalFrame.border");
Insets borderInsets = internalFrameBorder.getBorderInsets(frame);
dx += borderInsets.left;
dy += borderInsets.top;
frameWidth -= (borderInsets.left + borderInsets.right);
frameHeight -= (borderInsets.top + borderInsets.bottom);
BasicInternalFrameUI frameUI = (BasicInternalFrameUI) frame.getUI();
JComponent frameTitlePane = frameUI.getNorthPane();
if (frameTitlePane != null) {
dy += frameTitlePane.getHeight();
frameHeight -= frameTitlePane.getHeight();
}
// fix for defect 112 - checking frame height and width
if ((frameWidth > 0) && (frameHeight > 0)) {
// draw frame (note the canvas translation)
BufferedImage tempCanvas = new BufferedImage(frameWidth, frameHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics tempCanvasGraphics = tempCanvas.getGraphics();
tempCanvasGraphics.translate(-dx, -dy);
Map<Component, Boolean> dbSnapshot = new HashMap<>();
WidgetUtilities.makePreviewable(frame, dbSnapshot);
frame.paint(tempCanvasGraphics);
WidgetUtilities.restorePreviewable(frame, dbSnapshot);
int maxWidth = UIManager.getInt("DesktopIcon.width");
int maxHeight = maxWidth;
// check if need to scale down
double coef = Math.min((double) maxWidth / (double) frameWidth,
(double) maxHeight / (double) frameHeight);
if (coef < 1.0) {
int sdWidth = (int) (coef * frameWidth);
BufferedImage scaledDown = NeonCortex.createThumbnail(tempCanvas, sdWidth);
snapshot = scaledDown;
} else {
snapshot = tempCanvas;
}
}
}
/**
* Adds a component on this Canvas inside a frame.
*
* @param comp The component to add to the canvas.
* @param toolBox Should be set to true if the resulting frame is
* used as a toolbox (that is: it should not be counted as a
* frame).
* @param popupPosition A preferred {@code PopupPosition}.
* @param resizable Whether this component can be resized.
* @return The {@code JInternalFrame} that was created and added.
*/
private JInternalFrame addAsFrame(JComponent comp, boolean toolBox,
PopupPosition popupPosition,
boolean resizable) {
final int FRAME_EMPTY_SPACE = 60;
final JInternalFrame f = (toolBox) ? new ToolBoxFrame()
: new JInternalFrame();
Container con = f.getContentPane();
if (con instanceof JComponent) {
JComponent c = (JComponent)con;
c.setOpaque(false);
c.setBorder(null);
}
if (comp.getBorder() != null) {
if (comp.getBorder() instanceof EmptyBorder) {
f.setBorder(Utility.blankBorder(10, 10, 10, 10));
} else {
f.setBorder(comp.getBorder());
comp.setBorder(Utility.blankBorder(5, 5, 5, 5));
}
} else {
f.setBorder(null);
}
final FrameMotionListener fml = new FrameMotionListener(f);
comp.addMouseMotionListener(fml);
comp.addMouseListener(fml);
if (f.getUI() instanceof BasicInternalFrameUI) {
BasicInternalFrameUI biu = (BasicInternalFrameUI) f.getUI();
biu.setNorthPane(null);
biu.setSouthPane(null);
biu.setWestPane(null);
biu.setEastPane(null);
}
f.getContentPane().add(comp);
f.setOpaque(false);
f.pack();
int width = f.getWidth();
int height = f.getHeight();
if (width > getWidth() - FRAME_EMPTY_SPACE) {
width = Math.min(width, getWidth());
}
if (height > getHeight() - FRAME_EMPTY_SPACE) {
height = Math.min(height, getHeight());
}
f.setSize(width, height);
Point p = chooseLocation(comp, width, height, popupPosition);
f.setLocation(p);
this.addToCanvas(f, MODAL_LAYER);
f.setName(comp.getClass().getSimpleName());
f.setFrameIcon(null);
f.setVisible(true);
f.setResizable(resizable);
try {
f.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
return f;
}
private double getTitleHeight() {
return (((BasicInternalFrameUI) this.getUI()).getNorthPane()).getPreferredSize().getHeight();
}
private static JInternalFrame makeInternalFrame() {
JInternalFrame internal = new JInternalFrame("@[email protected]");
BasicInternalFrameUI ui = (BasicInternalFrameUI) internal.getUI();
Component title = ui.getNorthPane();
for (MouseMotionListener l: title.getListeners(MouseMotionListener.class)) {
title.removeMouseMotionListener(l);
}
DragWindowListener dwl = new DragWindowListener();
title.addMouseListener(dwl);
title.addMouseMotionListener(dwl);
KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
focusManager.addPropertyChangeListener(e -> {
String prop = e.getPropertyName();
// System.out.println(prop);
if ("activeWindow".equals(prop)) {
try {
internal.setSelected(Objects.nonNull(e.getNewValue()));
} catch (PropertyVetoException ex) {
throw new IllegalStateException(ex);
}
// System.out.println("---------------------");
}
});
// frame.addWindowListener(new WindowAdapter() {
// @Override public void windowLostFocus(FocusEvent e) {
// System.out.println("222222222");
// try {
// internal.setSelected(false);
// } catch (PropertyVetoException ex) {
// throw new IllegalStateException(ex);
// }
// }
// @Override public void windowGainedFocus(FocusEvent e) {
// System.out.println("111111111");
// try {
// internal.setSelected(true);
// } catch (PropertyVetoException ex) {
// throw new IllegalStateException(ex);
// }
// }
// });
// EventQueue.invokeLater(() -> {
// try {
// internal.setSelected(true);
// } catch (PropertyVetoException ex) {
// throw new IllegalStateException(ex);
// }
// // internal.requestFocusInWindow();
// });
return internal;
}