我对 Java 很陌生,所以请慢慢来。经过大量研究后,我相信我明白 JScrollPane
选项默认显示滚动条。我正在尝试改变一些现有代码中的这种行为。我相信只需使用 VERTICAL_SCROLLBAR_NEVER 更改默认值即可。但是,我很难确定它在代码中的位置。有人可以帮我看看这个属性的设置位置吗(或者我是否走在正确的轨道上)?
这是我认为与这个问题相关的代码段。分割 Pane 窗口的右侧有一个滚动条。我想永久隐藏它。
public class LauncherFrame extends JFrame {
private final Launcher launcher;
@Getter
private final InstanceTable instancesTable = new InstanceTable();
private final InstanceTableModel instancesModel;
@Getter
private final JScrollPane instanceScroll = new JScrollPane(instancesTable);
private WebpagePanel webView;
private JSplitPane splitPane;
private final JButton launchButton = new JButton(SharedLocale.tr("launcher.launch"));
private final JButton refreshButton = new JButton(SharedLocale.tr("launcher.checkForUpdates"));
private final JButton optionsButton = new JButton(SharedLocale.tr("launcher.options"));
private final JButton selfUpdateButton = new JButton(SharedLocale.tr("launcher.updateLauncher"));
private final JCheckBox updateCheck = new JCheckBox(SharedLocale.tr("launcher.downloadUpdates"));
public LauncherFrame(@NonNull Launcher launcher) {
super(tr("launcher.title", launcher.getVersion()));
this.launcher = launcher;
instancesModel = new InstanceTableModel(launcher.getInstances());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(new Dimension(615, 322));
setResizable(false);
initComponents();
setLocationRelativeTo(null);
SwingHelper.setFrameIcon(this, Launcher.class, "icon.png");
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
loadInstances();
}
});
}
private void initComponents() {
JPanel container = createContainerPanel();
container.setLayout(new MigLayout("fill, insets dialog", "[][]push[][]", "[grow][]"));
webView = createNewsPanel();
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, instanceScroll, webView);
selfUpdateButton.setVisible(launcher.getUpdateManager().getPendingUpdate());
launcher.getUpdateManager().addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("pendingUpdate")) {
selfUpdateButton.setVisible((Boolean) evt.getNewValue());
}
}
});
updateCheck.setSelected(true);
instancesTable.setModel(instancesModel);
launchButton.setFont(launchButton.getFont().deriveFont(Font.BOLD));
splitPane.setDividerLocation(100);
splitPane.setDividerSize(4);
splitPane.setOpaque(false);
container.add(splitPane, "grow, wrap, span 5, gapbottom unrel, w null:680, h null:350");
SwingHelper.flattenJSplitPane(splitPane);
container.add(refreshButton);
container.add(updateCheck);
container.add(selfUpdateButton);
container.add(optionsButton);
container.add(launchButton);
add(container, BorderLayout.CENTER);
}
* 更新 *
尝试了下面的建议,但仍然看到滚动条。我将窗口更改为可调整大小 true,以便缩小尺寸时您可以更轻松地看到它。
最佳答案
您可以将滚动条策略传递到 the constructor当您创建新的滚动 Pane 时。
private final JScrollPane instanceScroll = new JScrollPane(instancesTable,
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
或者您可以保留构造函数调用不变,并在 initComponents()
方法中设置策略(例如)。
instanceScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
关于java - 在哪里设置 VERTICAL_SCROLLBAR_NEVER?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53180324/