📄 baseline.java
字号:
private static int getSingleLineTextBaselineResizeBehavior(JTextField tf) {
return BRB_CENTER_OFFSET;
}
private static int getTextAreaBaselineResizeBehavior(JTextArea ta) {
return BRB_CONSTANT_ASCENT;
}
private static int getTableBaselineResizeBehavior(JTable table) {
return BRB_CONSTANT_ASCENT;
}
private static int getTabbedPaneBaselineResizeBehavior(JTabbedPane tp) {
switch(tp.getTabPlacement()) {
case JTabbedPane.LEFT:
case JTabbedPane.RIGHT:
case JTabbedPane.TOP:
return BRB_CONSTANT_ASCENT;
case JTabbedPane.BOTTOM:
return BRB_CONSTANT_DESCENT;
}
return BRB_OTHER;
}
private static int getSpinnerBaselineResizeBehavior(JSpinner spinner) {
return getBaselineResizeBehavior(spinner.getEditor());
}
private static int getSliderBaselineResizeBehavior(JSlider slider) {
return BRB_OTHER;
}
private static int getScrollPaneBaselineBaselineResizeBehavior(JScrollPane sp) {
return BRB_CONSTANT_ASCENT;
}
private static int getProgressBarBaselineResizeBehavior(JProgressBar pb) {
if (pb.isStringPainted() &&
pb.getOrientation() == JProgressBar.HORIZONTAL) {
return BRB_CENTER_OFFSET;
}
return BRB_OTHER;
}
private static int getPanelBaselineResizeBehavior(JPanel panel) {
Border b = panel.getBorder();
if (b instanceof TitledBorder) {
switch(((TitledBorder)b).getTitlePosition()) {
case TitledBorder.ABOVE_TOP:
case TitledBorder.TOP:
case TitledBorder.DEFAULT_POSITION:
case TitledBorder.BELOW_TOP:
return BRB_CONSTANT_ASCENT;
case TitledBorder.ABOVE_BOTTOM:
case TitledBorder.BOTTOM:
case TitledBorder.BELOW_BOTTOM:
return BRB_CONSTANT_DESCENT;
}
}
return BRB_OTHER;
}
private static int getListBaselineResizeBehavior(JList list) {
return BRB_CONSTANT_ASCENT;
}
private static int getLabelBaselineResizeBehavior(JLabel label) {
if (label.getClientProperty("html") != null) {
return BRB_OTHER;
}
switch(label.getVerticalAlignment()) {
case JLabel.TOP:
return BRB_CONSTANT_ASCENT;
case JLabel.BOTTOM:
return BRB_CONSTANT_DESCENT;
case JLabel.CENTER:
return BRB_CENTER_OFFSET;
}
return BRB_OTHER;
}
private static int getButtonBaselineResizeBehavior(AbstractButton button) {
if (button.getClientProperty("html") != null) {
return BRB_OTHER;
}
switch(button.getVerticalAlignment()) {
case AbstractButton.TOP:
return BRB_CONSTANT_ASCENT;
case AbstractButton.BOTTOM:
return BRB_CONSTANT_DESCENT;
case AbstractButton.CENTER:
return BRB_CENTER_OFFSET;
}
return BRB_OTHER;
}
private static int getComboBoxBaselineResizeBehavior(JComboBox cb) {
if (cb.isEditable()) {
return getBaselineResizeBehavior(cb.getEditor().getEditorComponent());
}
ListCellRenderer renderer = cb.getRenderer();
if (renderer == null) {
if (brbListCellRenderer == null) {
brbListCellRenderer = new DefaultListCellRenderer();
}
renderer = brbListCellRenderer;
}
Object value = null;
Object prototypeValue = cb.getPrototypeDisplayValue();
if (prototypeValue != null) {
value = prototypeValue;
} else if (cb.getModel().getSize() > 0) {
value = cb.getModel().getElementAt(0);
}
if (value != null) {
if (brbList == null) {
brbList = new JList();
}
Component component = renderer.
getListCellRendererComponent(brbList, value, -1,
false, false);
return getBaselineResizeBehavior(component);
}
return BRB_OTHER;
}
/**
* Returns the baseline for the specified component, or -1 if the
* baseline can not be determined. The baseline is measured from
* the top of the component. This method returns the baseline based
* on the preferred size.
*
* @param component JComponent to calculate baseline for
* @return baseline for the specified component
*/
public static int getBaseline(JComponent component) {
Dimension pref = component.getPreferredSize();
return getBaseline(component, pref.width, pref.height);
}
private static Method getBaselineMethod(JComponent component) {
if (COMPONENT_BASELINE_METHOD != null) {
return COMPONENT_BASELINE_METHOD;
}
Class klass = component.getClass();
while (klass != null) {
if (BASELINE_MAP.containsKey(klass)) {
Method method = (Method)BASELINE_MAP.get(klass);
return method;
}
klass = klass.getSuperclass();
}
klass = component.getClass();
Method[] methods = klass.getMethods();
for (int i = methods.length - 1; i >= 0; i--) {
Method method = methods[i];
if ("getBaseline".equals(method.getName())) {
Class[] params = method.getParameterTypes();
if (params.length == 2 && params[0] == int.class &&
params[1] == int.class) {
BASELINE_MAP.put(klass, method);
return method;
}
}
}
BASELINE_MAP.put(klass, null);
return null;
}
private static int invokeBaseline(Method method, JComponent c, int width,
int height) {
int baseline = -1;
try {
baseline = ((Integer)method.invoke(c,
new Object[] { new Integer(width),
new Integer(height) })).intValue();
} catch (IllegalAccessException iae) {
} catch (IllegalArgumentException iae2) {
} catch (InvocationTargetException ite2) {
}
return baseline;
}
private static boolean isKnownLookAndFeel() {
LookAndFeel laf = UIManager.getLookAndFeel();
String lookAndFeelID = laf.getID();
return (lookAndFeelID == "GTK" || lookAndFeelID == "Aqua" ||
isMetal(laf) || isWindows(laf));
}
/**
* Returns the baseline for the specified component, or a value less
* than 0 if the baseline can not be determined. The baseline is measured
* from the top of the component.
*
* @param component JComponent to calculate baseline for
* @param width Width of the component to determine baseline for.
* @param height Height of the component to determine baseline for.
* @return baseline for the specified component
*/
public static int getBaseline(JComponent component, int width, int height) {
Method baselineMethod = getBaselineMethod(component);
if (baselineMethod != null) {
return invokeBaseline(baselineMethod, component, width, height);
}
Object baselineImpl = UIManager.get("Baseline.instance");
if (baselineImpl != null && (baselineImpl instanceof Baseline)) {
return ((Baseline)baselineImpl).getComponentBaseline(
component, width, height);
}
if (!isKnownLookAndFeel()) {
return -1;
}
String uid = component.getUIClassID();
int baseline = -1;
if (uid == "ButtonUI" || uid == "CheckBoxUI" ||
uid == "RadioButtonUI" || uid == "ToggleButtonUI") {
baseline = getButtonBaseline((AbstractButton)component,
height);
}
else if (uid == "ComboBoxUI") {
return getComboBoxBaseline((JComboBox)component,
height);
}
else if (uid == "TextAreaUI") {
return getTextAreaBaseline((JTextArea)component, height);
}
else if (uid == "FormattedTextFieldUI" ||
uid == "PasswordFieldUI" ||
uid == "TextFieldUI") {
baseline = getSingleLineTextBaseline((JTextComponent)component,
height);
}
else if (uid == "LabelUI") {
baseline = getLabelBaseline((JLabel)component, height);
}
else if (uid == "ListUI") {
baseline = getListBaseline((JList)component, height);
}
else if (uid == "PanelUI") {
baseline = getPanelBaseline((JPanel)component, height);
}
else if (uid == "ProgressBarUI") {
baseline = getProgressBarBaseline((JProgressBar)component, height);
}
else if (uid == "SliderUI") {
baseline = getSliderBaseline((JSlider)component, height);
}
else if (uid == "SpinnerUI") {
baseline = getSpinnerBaseline((JSpinner)component, height);
}
else if (uid == "ScrollPaneUI") {
baseline = getScrollPaneBaseline((JScrollPane)component, height);
}
else if (uid == "TabbedPaneUI") {
baseline = getTabbedPaneBaseline((JTabbedPane)component, height);
}
else if (uid == "TableUI") {
baseline = getTableBaseline((JTable)component, height);
}
else if (uid == "TreeUI") {
baseline = getTreeBaseline((JTree)component, height);
}
return Math.max(baseline, -1);
}
private static Insets rotateInsets(Insets topInsets, int targetPlacement) {
switch(targetPlacement) {
case JTabbedPane.LEFT:
return new Insets(topInsets.left, topInsets.top,
topInsets.right, topInsets.bottom);
case JTabbedPane.BOTTOM:
return new Insets(topInsets.bottom, topInsets.left,
topInsets.top, topInsets.right);
case JTabbedPane.RIGHT:
return new Insets(topInsets.left, topInsets.bottom,
topInsets.right, topInsets.top);
default:
return new Insets(topInsets.top, topInsets.left,
topInsets.bottom, topInsets.right);
}
}
private static int getMaxTabHeight(JTabbedPane tp) {
int fontHeight = tp.getFontMetrics(tp.getFont()).getHeight();
int height = fontHeight;
boolean tallerIcons = false;
for (int counter = tp.getTabCount() - 1; counter >= 0; counter--) {
Icon icon = tp.getIconAt(counter);
if (icon != null) {
int iconHeight = icon.getIconHeight();
height = Math.max(height, iconHeight);
if (iconHeight > fontHeight) {
tallerIcons = true;
}
}
}
Insets tabInsets = UIManager.getInsets("TabbedPane.tabInsets");
height += 2;
if (!isMetal() || !tallerIcons) {
height += tabInsets.top + tabInsets.bottom;
}
return height;
}
private static int getTabbedPaneBaseline(JTabbedPane tp, int height) {
if (tp.getTabCount() > 0) {
if (isAqua()) {
return getAquaTabbedPaneBaseline(tp, height);
}
Insets insets = tp.getInsets();
Insets contentBorderInsets = UIManager.getInsets(
"TabbedPane.contentBorderInsets");
Insets tabAreaInsets = rotateInsets(UIManager.getInsets(
"TabbedPane.tabAreaInsets"),
tp.getTabPlacement());
FontMetrics metrics = tp.getFontMetrics(tp.getFont());
int maxHeight = getMaxTabHeight(tp);
iconRect.setBounds(0, 0, 0, 0);
textRect.setBounds(0, 0, 0, 0);
viewRect.setBounds(0, 0, Short.MAX_VALUE, maxHeight);
SwingUtilities.layoutCompoundLabel(tp, metrics, "A", null,
SwingUtilities.CENTER,
SwingUtilities.CENTER,
SwingUtilities.CENTER,
SwingUtilities.TRAILING,
viewRect,
iconRect,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -