如所建議的通過MadProgrammer,一個JLayer不工作:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
public class FixedImageLayerUI extends LayerUI<JComponent>
{
@Override
public void paint(Graphics g, JComponent c)
{
super.paint(g, c);
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(Color.RED);
g2.fillOval(0, 0, 10, 10);
g2.dispose();
}
private static void createAndShowUI()
{
String[] data =
{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z"
};
JList<String> list = new JList<String>(data);
JScrollPane scrollPane = new JScrollPane(list);
LayerUI<JComponent> layerUI = new FixedImageLayerUI();
JLayer<JComponent> layer = new JLayer<JComponent>(scrollPane, layerUI);
JFrame frame = new JFrame("FixedImage");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(layer);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
而且,如由MadProgrammer注意到,覆蓋JScrollPane的的塗料方法不起作用。但是,如果你做的JList非不透明,它的工作:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
public class FixedImageScrollPane
{
private static void createAndShowUI()
{
String[] data =
{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z"
};
JList<String> list = new JList<String>(data);
list.setOpaque(false);
JScrollPane scrollPane = new JScrollPane(list)
{
@Override
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.RED);
g.fillOval(0, 0, 10, 10);
}
};
JFrame frame = new JFrame("FixedImage");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
[例如(http://stackoverflow.com/q/8197261/714968),使用'塗料()',使用'JViewPort.setScrollMode(JViewport.Xxx)',使用自己的'RepaintManager' ,但我會使用'GlassPane'或'JLayer' – mKorbel 2013-03-18 07:37:32