问题描述
我有一个JTextArea,它正在JScrollPane上。无论如何,我知道我可以使用 getViewPort()
方法来设置视口的不透明...但我似乎无法找到任何关于如何做到这一点的迹象......任何地方。 :S
I have a JTextArea and it's riding ontop of a JScrollPane. Anyways, I know I can use the getViewPort()
method to set the opaque of of the view port... but I cannot seem to find any sign of how to do that... anywhere. :S
这是我到目前为止所拥有的:
Here is what I have so far:
if (e.getKeyCode() == KeyEvent.VK_F)
{
if (sp.isVisible())
{
sp.setVisible(false);
}
else
{
sp.setVisible(true);
}
}
推荐答案
您的与@Serplat的讨论表明你可能会混淆不透明度和透明度。
Your colloquy with @Serplat suggests that you may be confounding opacity and transparency.
是用于优化绘图的Swing组件的布尔属性:
Opacity is a boolean property of Swing components used to optimize drawing:
-
true
:组件同意绘制其中包含的所有位矩形边界。 -
false
:该组件不保证在其矩形边界内绘制所有位。
true
: The component agrees to paint all of the bits contained within its rectangular bounds.false
: The component makes no guarantees about painting all the bits within its rectangular bounds.
是一种合成数字图像的方法,如所示。
考虑到区别可能有助于cla提出您的问题或集中搜索更多信息。
Considering the distinction may help to clarify your question or focus your search for more information.
附录:基于@ camickr的,下面的示例显示了一个粘贴到视口的蓝色方块,而灰色棋盘可以在其上滚动。
Addendum: Based on @camickr's example, the example below shows a blue square that "sticks" to the viewport, while the gray checkerboard may be scrolled over it.
import java.awt.*;
import javax.swing.*;
/** @see https://stackoverflow.com/questions/2846497 */
public class ScrollPanePaint extends JFrame {
private static final int TILE = 64;
public ScrollPanePaint() {
JViewport viewport = new MyViewport();
viewport.setView(new MyPanel());
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewport(viewport);
this.add(scrollPane);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private static class MyViewport extends JViewport {
public MyViewport() {
this.setOpaque(false);
this.setPreferredSize(new Dimension(6 * TILE, 6 * TILE));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.blue);
g.fillRect(TILE, TILE, 3 * TILE, 3 * TILE);
}
}
private static class MyPanel extends JPanel {
public MyPanel() {
this.setOpaque(false);
this.setPreferredSize(new Dimension(9 * TILE, 9 * TILE));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.lightGray);
int w = this.getWidth() / TILE + 1;
int h = this.getHeight() / TILE + 1;
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
if ((row + col) % 2 == 0) {
g.fillRect(col * TILE, row * TILE, TILE, TILE);
}
}
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ScrollPanePaint();
}
});
}
}
这篇关于Java - 透明的JScrollPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!