gpt4 book ai didi

java - 基于 jpanel 内容的滚动条

转载 作者:行者123 更新时间:2023-12-02 09:50:27 25 4
gpt4 key购买 nike

我有一个 500x500 的 jpanel 并充当 Canvas (在其上绘制所有内容)。滚动条在面板上以 510x510 的分辨率启动(根据需要)(视觉上永远无法达到,但根据内容会超过),500x500 表示为 1:1(比例)

下面是放大的线的表示,可以看出, View 窗口保持不变并且没有弹出滚动条,将滚动条设置为始终会导致禁用滚动条。

JScrollPane scrollPanel = new JScrollPane(JPanel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

initial line

我该如何让滚动条读取此面板的内容并变得可滚动?我找不到与此相关的任何内容

附加面板变大,但滚动条未读取此更改。是否有某种我需要调用但我没有的更新(想一想,我没有明确地调用更新的任何内容?但也找不到与此相关的任何内容)

最佳答案

您需要为面板实现可滚动界面,该界面放置在 JScrollPane 中并覆盖方法 getPreferredSize()。这是示例

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;
import javax.swing.WindowConstants;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

public class ZoomPanel extends JPanel implements Scrollable {

private static final int DEFAULT_SIZE = 500;

private float zoom = 1f;

@Override
public Dimension getPreferredSize() {
int size = Math.round(zoom * DEFAULT_SIZE);
return new Dimension(size, size);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int size = Math.round(zoom * DEFAULT_SIZE);
g.drawOval(0, 0, size, size);
}

private static class ZoomRenderer extends BasicComboBoxRenderer {

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Object val = value;
if (value instanceof Number) {
val = Math.round(((Number) value).doubleValue() * 100) + "%";
}
return super.getListCellRendererComponent(list, val, index, isSelected, cellHasFocus);
}
}

public static void main(String[] args) {
JFrame frm = new JFrame("Zoom panel");
ZoomPanel z = new ZoomPanel();
JScrollPane scroller = new JScrollPane(z);
Float[] zooms = {.5f, .75f, 1f, 1.5f, 2f, 4f};
JComboBox<Float> combo = new JComboBox<>(zooms);
combo.setSelectedIndex(2);
combo.setRenderer(new ZoomRenderer());
combo.addActionListener(e -> {
Float v = (Float) combo.getSelectedItem();
if (v != null) {
z.zoom = v;
scroller.revalidate();
scroller.repaint();
}
});
JPanel comboPanel = new JPanel();
comboPanel.add(combo);
frm.add(comboPanel, BorderLayout.NORTH);
frm.add(scroller);
frm.pack();
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}

@Override
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}

@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 5;
}

@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 20;
}

@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}

@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}

关于java - 基于 jpanel 内容的滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56353306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com