📄 baseline.java
字号:
textRect,
0);
int baseline = textRect.y + metrics.getAscent();
switch(tp.getTabPlacement()) {
case JTabbedPane.TOP:
baseline += insets.top + tabAreaInsets.top;
if (isWindows()) {
if (tp.getTabCount() > 1) {
baseline += 1;
}
else {
baseline -= 1;
}
}
return baseline;
case JTabbedPane.BOTTOM:
baseline = tp.getHeight() - insets.bottom -
tabAreaInsets.bottom - maxHeight + baseline;
if (isWindows()) {
if (tp.getTabCount() > 1) {
baseline += -1;
}
else {
baseline += 1;
}
}
return baseline;
case JTabbedPane.LEFT:
case JTabbedPane.RIGHT:
if (isAqua()) {
// Aqua rotates left/right text, so that there isn't a good
// baseline.
return -1;
}
baseline += insets.top + tabAreaInsets.top;
if (isWindows()) {
baseline += (maxHeight % 2);
}
return baseline;
}
}
return -1;
}
private static int getAquaTabbedPaneBaseline(JTabbedPane tp, int height) {
Font font = tp.getFont();
FontMetrics metrics = tp.getFontMetrics(font);
int ascent = metrics.getAscent();
int offset;
switch(tp.getTabPlacement()) {
case JTabbedPane.TOP:
offset = 5;
if (tp.getFont().getSize() > 12) {
offset = 6;
}
int yOffset = 20 - metrics.getHeight();
yOffset /= 2;
return offset + yOffset + ascent - 1;
case JTabbedPane.BOTTOM:
if (tp.getFont().getSize() > 12) {
offset = 6;
} else {
offset = 4;
}
return height - (20 -
((20 - metrics.getHeight()) / 2 + ascent)) - offset;
case JTabbedPane.LEFT:
case JTabbedPane.RIGHT:
// Aqua rotates left/right text, so that there isn't a good
// baseline.
return -1;
}
return -1;
}
private static int getSliderBaseline(JSlider slider, int height) {
// We don't handle GTK as too much is hidden to be able to calculate it
if (slider.getPaintLabels() && !isGTK()) {
boolean isAqua = isAqua();
FontMetrics metrics = slider.getFontMetrics(slider.getFont());
Insets insets = slider.getInsets();
Insets focusInsets = (Insets)UIManager.get("Slider.focusInsets");
if (slider.getOrientation() == JSlider.HORIZONTAL) {
int tickLength = 8;
int contentHeight = height - insets.top - insets.bottom -
focusInsets.top - focusInsets.bottom;
int thumbHeight = 20;
if (isMetal()) {
tickLength = ((Integer)UIManager.get(
"Slider.majorTickLength")).intValue() + 5;
thumbHeight = UIManager.getIcon(
"Slider.horizontalThumbIcon" ).getIconHeight();
}
else if (isWindows() && isXP()) {
// NOTE: this is not correct, this should come from
// the skin (in >= 1.5), but short of reflection
// hacks we don't have access to the real value.
thumbHeight++;
}
int centerSpacing = thumbHeight;
if (isAqua || slider.getPaintTicks()) {
// centerSpacing += getTickLength();
centerSpacing += tickLength;
}
// Assume uniform labels.
centerSpacing += metrics.getAscent() + metrics.getDescent();
int trackY = insets.top + focusInsets.top +
(contentHeight - centerSpacing - 1) / 2;
if (isAqua) {
if (slider.getPaintTicks()) {
int prefHeight = slider.getUI().getPreferredSize(slider).
height;
int prefDelta = height - prefHeight;
if (prefDelta > 0) {
trackY -= Math.min(1, prefDelta);
}
} else {
trackY--;
}
}
int trackHeight = thumbHeight;
int tickY = trackY + trackHeight;
int tickHeight = tickLength;
if (!isAqua && !slider.getPaintTicks()) {
tickHeight = 0;
}
int labelY = tickY + tickHeight;
return labelY + metrics.getAscent();
}
else { // vertical
boolean inverted = slider.getInverted();
Integer value = inverted ? getMinSliderValue(slider) :
getMaxSliderValue(slider);
if (value != null) {
int thumbHeight = 11;
if (isMetal()) {
thumbHeight = UIManager.getIcon(
"Slider.verticalThumbIcon").getIconHeight();
}
int trackBuffer = Math.max(metrics.getHeight() / 2,
thumbHeight / 2);
int contentY = focusInsets.top + insets.top;
int trackY = contentY + trackBuffer;
int trackHeight = height - focusInsets.top -
focusInsets.bottom - insets.top - insets.bottom -
trackBuffer - trackBuffer;
int maxValue = getMaxSliderValue(slider).intValue();
int min = slider.getMinimum();
int max = slider.getMaximum();
double valueRange = (double)max - (double)min;
double pixelsPerValue = (double)trackHeight /
(double)valueRange;
int trackBottom = trackY + (trackHeight - 1);
if (isAqua) {
trackY -= 3;
trackBottom += 6;
}
int yPosition = trackY;
double offset;
if (!inverted) {
offset = pixelsPerValue *
((double)max - value.intValue());
}
else {
offset = pixelsPerValue *
((double)value.intValue() - min);
}
if (isAqua) {
yPosition += Math.floor(offset);
} else {
yPosition += Math.round(offset);
}
yPosition = Math.max(trackY, yPosition);
yPosition = Math.min(trackBottom, yPosition);
if (isAqua) {
return yPosition + metrics.getAscent();
}
return yPosition - metrics.getHeight() / 2 +
metrics.getAscent();
}
}
}
return -1;
}
private static Integer getMaxSliderValue(JSlider slider) {
Dictionary dictionary = slider.getLabelTable();
if (dictionary != null) {
Enumeration keys = dictionary.keys();
int max = slider.getMinimum() - 1;
while (keys.hasMoreElements()) {
max = Math.max(max, ((Integer)keys.nextElement()).intValue());
}
if (max == slider.getMinimum() - 1) {
return null;
}
return new Integer(max);
}
return null;
}
private static Integer getMinSliderValue(JSlider slider) {
Dictionary dictionary = slider.getLabelTable();
if (dictionary != null) {
Enumeration keys = dictionary.keys();
int min = slider.getMaximum() + 1;
while (keys.hasMoreElements()) {
min = Math.min(min, ((Integer)keys.nextElement()).intValue());
}
if (min == slider.getMaximum() + 1) {
return null;
}
return new Integer(min);
}
return null;
}
private static int getProgressBarBaseline(JProgressBar pb, int height) {
if (pb.isStringPainted() &&
pb.getOrientation() == JProgressBar.HORIZONTAL) {
FontMetrics metrics = pb.getFontMetrics(pb.getFont());
Insets insets = pb.getInsets();
int y = insets.top;
if (isWindows() && isXP()) {
if (pb.isIndeterminate()) {
y = -1;
height--;
}
else {
y = 0;
height -= 3;
}
}
else if (isGTK()) {
return (height - metrics.getAscent() -
metrics.getDescent()) / 2 + metrics.getAscent();
}
else if (isAqua()) {
if (pb.isIndeterminate()) {
// Aqua doesn't appear to support text on indeterminate
// progress bars.
return -1;
}
y -= 1;
height -= (insets.top + insets.bottom);
}
else {
height -= insets.top + insets.bottom;
}
return y + (height + metrics.getAscent() -
metrics.getLeading() -
metrics.getDescent()) / 2;
}
return -1;
}
private static int getTreeBaseline(JTree tree, int height) {
int rowHeight = tree.getRowHeight();
if (TREE_LABEL == null) {
TREE_LABEL = new JLabel("X");
TREE_LABEL.setIcon(UIManager.getIcon("Tree.closedIcon"));
}
JLabel label = TREE_LABEL;
label.setFont(tree.getFont());
if (rowHeight <= 0) {
rowHeight = label.getPreferredSize().height;
}
return getLabelBaseline(label, rowHeight) + tree.getInsets().top;
}
private static int getTableBaseline(JTable table, int height) {
if (TABLE_LABEL == null) {
TABLE_LABEL = new JLabel("");
TABLE_LABEL.setBorder(new EmptyBorder(1, 1, 1, 1));
}
JLabel label = TABLE_LABEL;
label.setFont(table.getFont());
int rowMargin = table.getRowMargin();
int baseline = getLabelBaseline(label, table.getRowHeight() -
rowMargin);
return baseline += rowMargin / 2;
}
private static int getTextAreaBaseline(JTextArea text, int height) {
Insets insets = text.getInsets();
FontMetrics fm = text.getFontMetrics(text.getFont());
return insets.top + fm.getAscent();
}
private static int getListBaseline(JList list, int height) {
int rowHeight = list.getFixedCellHeight();
if (LIST_LABEL == null) {
LIST_LABEL = new JLabel("X");
LIST_LABEL.setBorder(new EmptyBorder(1, 1, 1, 1));
}
JLabel label = LIST_LABEL;
label.setFont(list.getFont());
// JList actually has much more complex behavior here.
// If rowHeight != -1 the rowHeight is either the max of all cell
// heights (layout orientation != VERTICAL), or is variable depending
// upon the cell. We assume a default size.
// We could theoretically query the real renderer, but that would
// not work for an empty model and the results may vary with
// the content.
if (rowHeight == -1) {
rowHeight = label.getPreferredSize().height;
}
return getLabelBaseline(label, rowHeight) + list.getInsets().top;
}
private static int getScrollPaneBaseline(JScrollPane sp, int height) {
Component view = sp.getViewport().getView();
if (view instanceof JComponent) {
int baseline = getBaseline((JComponent)view);
if (baseline > 0) {
return baseline + sp.getViewport().getY();
}
}
return -1;
}
private static int getPanelBaseline(JPanel panel, int height) {
Border border = panel.getBorder();
if (border instanceof TitledBorder) {
TitledBorder titledBorder = (TitledBorder)border;
if (titledBorder.getTitle() != null &&
!"".equals(titledBorder.getTitle())) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -