gpt4 book ai didi

java - Japanel 中带有 jList 的滚动 Pane 不起作用

转载 作者:行者123 更新时间:2023-11-30 07:34:24 27 4
gpt4 key购买 nike

编辑

这是您的代码,其中projectList 的字符串太长,这应该会使滚动条出现。但事实并非如此。相反,它扩大了框架尺寸

import java.awt.BorderLayout;
import javax.swing.*;

public class FooFrame extends JFrame {
private JList jList;
private String[] projectList = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nineeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"};
private DefaultListModel listItems;
private JScrollPane scrollpane;

public FooFrame() {
JLabel lblHeading = new JLabel(
" Demo text");
JPanel mainLayout = new JPanel();
mainLayout.setLayout(new BorderLayout());
mainLayout.add(lblHeading, BorderLayout.NORTH);
jList = new JList(projectList);

// listItems = new DefaultListModel(); // wasted line since you don't use this
jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollpane = new JScrollPane(jList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
mainLayout.add(scrollpane);
add(mainLayout);
}

private static void createAndShowGui() {
FooFrame frame = new FooFrame();
frame.setSize(10,10);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
createAndShowGui();
}

}

当我尝试在 Japanel 的 Jscrollpane 中添加 JList 时,我注意到了一件奇怪的事情。如果我尝试直接在框架中的 JscrollPane 中添加 JList,则很容易完成。好像我试图添加相同的内容并将其放入面板中(带有边框布局),它对我不起作用。

这里是示例代码

JLabel lblHeading = new JLabel(
" Demo text");
JPanel mainLayout = new JPanel();
mainLayout.setLayout(new BorderLayout());
mainLayout.add(lblHeading, BorderLayout.NORTH);
jList = new JList(projectList);

listItems = new DefaultListModel();
jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollpane = new JScrollPane(jList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
mainLayout.add(scrollpane);
add(mainLayout);

无论我做什么,JList 中的文本都会超出框架,并且滚动条不会出现。

有什么建议吗?

最佳答案

您的问题不是问题,而是一个功能 - JScrollPane 是一个智能组件,除非需要,否则不会显示滚动条或滚动条选项卡,并且只有当 JScrollPane 的视口(viewport)小于它持有和显示的组件。例如,如果您人为地限制包含 JPanel 的大小(此处通过覆盖 getPreferredSize() 来完成),则会显示滚动条选项卡:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;

@SuppressWarnings("serial")
public class FooPanel extends JPanel {
private static final int PREF_W = 200;
private static final int PREF_H = 200;
private static final String[] PROJECT_LIST = { "One", "Two", "Three",
"FOURRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR" };
private static final String TITLE_TEXT = "Demo Text";
private JList<String> jList = new JList<>(PROJECT_LIST);

public FooPanel() {
JScrollPane scrollPane = new JScrollPane(jList);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

setLayout(new BorderLayout());
add(new JLabel(TITLE_TEXT, SwingConstants.CENTER), BorderLayout.PAGE_START);
add(scrollPane);
}

@Override
public Dimension getPreferredSize() {
Dimension superSz = super.getPreferredSize();
if (isPreferredSizeSet()) {
return superSz;
}
int prefW = Math.min(superSz.width, PREF_W);
int prefH = Math.max(superSz.height, PREF_H);
return new Dimension(prefW, prefH);
}

private static void createAndShowGui() {
FooPanel mainPanel = new FooPanel();

JFrame frame = new JFrame("Foo Panel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}

关于java - Japanel 中带有 jList 的滚动 Pane 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35613600/

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