我制作了一个 JPanel,其中包含一个围绕 JTextArea 的 JScrollPane。我想在鼠标退出 JScrollPane 时隐藏滚动条。
如果我在 JScrollPane 上设置 MouseListener,则什么也不会发生。如果我在 JTextArea 上设置它,那么滚动条会正确隐藏/取消隐藏,但您无法单击它们,因为它们超出了 JTextArea 的范围。\
当您尝试单击滚动条时,如何防止滚动条消失?
代码示例 1. 隐藏和取消隐藏 + 更改透明度有效。
问题:您无法单击滚动条,它们会因为您退出 JTextArea 而消失。
public class TransparentTextArea extends JTextArea {
private static final Color LIGHT_TRANSPARENT = new Color(0, 0, 0, 150);
private static final Color HEAVY_TRANSPARENT = new Color(0, 0, 0, 50);
public TransparentTextArea(final GameModel model) {
setOpaque(false);
setForeground(Color.WHITE);
setBackground(HEAVY_TRANSPARENT);
setEditable(false);
addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
model.setLogActive(false);
setBackground(HEAVY_TRANSPARENT);
}
@Override
public void mouseEntered(MouseEvent e) {
model.setLogActive(true);
setBackground(LIGHT_TRANSPARENT);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
}
public class Logger extends JScrollPane implements PanelObserver,
ChangeListener {
private GameModel model;
public Logger(final GameModel model) {
super(new TransparentTextArea(model));
this.model = model;
setOpaque(false);
getViewport().setOpaque(false);
setBorder(BorderFactory.createEmptyBorder());
setViewportBorder(BorderFactory.createEmptyBorder());
model.addPanelObserver(this);
model.addChangeListener(this);
}
@Override
public void panelResized() {
float x = model.getPanelWidth() * 0.01f;
float y = model.getPanelHeight() * 0.70f;
float width = model.getPanelWidth() * 0.30f;
float height = model.getPanelHeight() * 0.28f;
setBounds((int) x, (int) y, (int) width, (int) height);
}
@Override
public void stateChanged(ChangeEvent e) {
if (model.isLogActive()) {
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
} else {
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
}
}
代码示例 2。
问题我试图通过在它周围创建一个面板并使用该 MouseExited 来解决这个问题,但它不起作用,因为孩子们使用它。
public class Logger extends JPanel implements PanelObserver, MouseListener {
private static final Color LIGHT_TRANSPARENT = new Color(0, 0, 0, 150);
private static final Color HEAVY_TRANSPARENT = new Color(0, 0, 0, 50);
private GameModel model;
private JTextArea textArea;
private JScrollPane scrollPane;
public Logger(GameModel model) {
super(null);
this.model = model;
setOpaque(false);
textArea = new JTextArea();
textArea.setOpaque(false);
textArea.setForeground(Color.WHITE);
textArea.setBackground(HEAVY_TRANSPARENT);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
scrollPane.setBorder(BorderFactory.createEmptyBorder());
scrollPane.setViewportBorder(BorderFactory.createEmptyBorder());
add(scrollPane);
model.addPanelObserver(this);
addMouseListener(this);
}
@Override
public void panelResized() {
float x = model.getPanelWidth() * 0.01f;
float y = model.getPanelHeight() * 0.70f;
float width = model.getPanelWidth() * 0.30f;
float height = model.getPanelHeight() * 0.28f;
setBounds((int) x, (int) y, (int) width, (int) height);
scrollPane.setBounds(0, 0, (int) width, (int) height);
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
textArea.setBackground(LIGHT_TRANSPARENT);
}
@Override
public void mouseExited(MouseEvent e) {
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
textArea.setBackground(HEAVY_TRANSPARENT);
}
}