JScrollPane不添加组件 [英] JScrollPane doesn't add components
问题描述
我扩展了JScrollPane并向其添加了几个组件。当我向JFrame添加滚动窗格时,不会显示任何组件。如果我的类扩展,例如,JPanel,然后我将它添加到独立的JScrollPane,一切正常。我无法理解这种行为。任何人都可以解释一下,为什么会发生这种情况?
I extend JScrollPane and add several components to it. When I add scroll pane to JFrame no components are displayed. In case when my class extends, for example, JPanel and then I add it to a standalone JScrollPane, everything works fine. I cannot understand this behaviour. Could anyone explain me, why it happens?
以下是两种变体(一种有效,另一种无效):
Here are both variants (the one that works and the one that doesn't):
此变体不起作用:
public class MainScrollPanel extends JScrollPane {
private JPanel verticalPanel;
public MainScrollPanel() throws IOException, ParseException {
initGUI();
readData();
}
private void initGUI() {
verticalPanel = new JPanel();
verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));
add(verticalPanel);
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
private void readData() throws IOException, ParseException {
//read data
//...
for(NewData message : messages) {
verticalPanel.add(new JLabel(message.getMessage()));
}
}
}
public class MainGUI extends JFrame {
private MainScrollPanel mainPanel;
public MainGUI() throws IOException, ParseException {
super("Scroll app");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainPanel = new MainScrollPanel();
getContentPane().add(mainPanel);
setSize(200, 200);
setVisible(true);
}
public static void main(String[] args) throws IOException, ParseException {
new MainGUI();
}
}
这个工作正常:
public class MainScrollPanel extends JPanel {
public MainScrollPanel() throws IOException, ParseException {
initGUI();
readData();
}
private void initGUI() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
}
private void readData() throws IOException, ParseException {
//The same as in previous example
}
}
public class MainGUI extends JFrame {
private MainScrollPanel mainPanel;
public MainGUI() throws IOException, ParseException {
//...
JScrollPane scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
getContentPane().add(scrollPane);
//...
}
}
推荐答案
您可能不需要或不想扩展JScrollPane,但无论如何,您几乎从不将组件直接添加到JScrollPane,而是添加到其Viewport。否则你会失去Viewport及其功能。
You probably don't need or want to extend JScrollPane, but regardless, you almost never add components directly to a JScrollPane but rather to its Viewport. Else you lose the Viewport and its functionality.
这可以通过JScrollPane方法完成: setViewportView(Component comp)
另一种方法是将组件传递给JScrollPane的构造函数(或者它的 super 构造函数),因为这会自动将组件传递给视口。这是一个小小的语法糖。
This can be done via the JScrollPane method: setViewportView(Component comp)
The other way is to pass the component into the JScrollPane's constructor (or here its super constructor) as this will automatically pass the component to the viewport. It's a little syntactic sugar is all.
例如,
private void initGUI() {
verticalPanel = new JPanel();
verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));
setViewportView(verticalPanel); // ********** changed *******
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
这篇关于JScrollPane不添加组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!