我正在使用一个自定义类,基于这个答案中的代码,来绘制形状像对话气泡的背景。每当我调整窗口大小使得组件顶部或底部露出时,该组件的轮廓就会绘制在
JScrollPane
之外并覆盖其他组件,此时是JPanel
。
在左侧图像中,由于组件仍然可见,因此绘制了JScrollPane
底部的组件边框;而在右侧图像中,该组件不再可见,一切看起来都很正常。
我认为这与我使用JScrollPane
来容纳组件有关,从而允许组件滑动到JPanel
下方。如何防止这种情况发生?
主要:
public class Main {
public static void main(String[] args) {
JPanel panel = new JPanel(), panelbar = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panelbar.setLayout(new FlowLayout());
JScrollPane scroll = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JFrame frame = new JFrame("");
frame.setLayout(new BorderLayout());
frame.setSize(200, 223);
for (int i = 0; i < 6; i++) {
JLabel label = new JLabel("JLabel");
label.setBorder(new CustomBorder());
label.setOpaque(true);
label.setBackground(Color.ORANGE);
panel.add(label);
}
panelbar.add(new JLabel("JPanel"));
frame.add(scroll, BorderLayout.CENTER);
frame.add(panelbar, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
自定义类:
public class CustomBorder extends AbstractBorder {
private static final long serialVersionUID = 1L;
Insets i;
CustomBorder() {
i = new Insets(10, 20, 10, 20);
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
super.paintBorder(c, g, x, y, width, height);
Polygon bubble = new Polygon();
bubble.addPoint(x + 10, y + 5);
bubble.addPoint(x + width - 10, y + 5);
bubble.addPoint(x + width - 10, y + height / 3);
bubble.addPoint(x + width, y + height / 2);
bubble.addPoint(x + width - 10, y + height * 2 / 3);
bubble.addPoint(x + width - 10, y - 5 + height);
bubble.addPoint(x + 10, y - 5 + height);
Graphics2D g2d = (Graphics2D) g;
Area rect = new Area(new Rectangle(x, y, width, height));
rect.subtract(new Area(bubble));
g2d.setClip(rect);
g2d.setColor(c.getParent().getBackground());
g2d.fillRect(0, 0, width, height);
g2d.setClip(null);
g2d.setColor(Color.BLACK);
g2d.draw(bubble);
}
@Override
public Insets getBorderInsets(Component c) {
return i;
}
@Override
public Insets getBorderInsets(Component c, Insets insets) {
return i;
}
}
g2d.setClip(rect);
会引起问题,因为您已更改了原始的Graphics
上下文的剪辑,现在允许您在不应该绘制的位置绘画,这就是为什么我不玩剪辑的原因。相反,制作一个与您试图生成的形状匹配的Shape
并draw
/fill
它。 - MadProgrammerpaintComponent
后,Border
被绘制,这意味着它们覆盖内容...这意味着如果您填充边框,则会覆盖内容... - MadProgrammerdraw/fill
会在文本上方绘制,而不是在文本后面,使 JLabel 内部的文本无法阅读。 - Spitz