修改
这是你的代码,其中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中的文本都不在框架内,滚动条也不会出现。
有什么建议吗?
答案 0 :(得分:1)
您的问题不是问题,而是一个功能 - JScrollPane是一个智能组件,除非再次需要,否则不会显示滚动条或滚动条选项卡,这只会在JScrollPane的视口小于它所持有和显示的组件。例如,如果您人为地限制包含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();
});
}
}