JFrame中的滚动窗格未显示

问题描述 投票:1回答:2

我尝试在框架上放置一个滚动条,但它不起作用,我没有看到滚动条。

这是我的代码:

Popup noticePopup = new Popup("Notice", 1500, 900);
JPanel noticePanel = new JPanel();
noticePanel.setPreferredSize(new Dimension(1500,1000));
List<List> ListNote = controller.Medicament.consultationNotice(medocSelectStr);
String noticeThis =null;
for(int n=0;n<ListNote.size();n++) {
    noticeThis = (String) ListNote.get(n).get(1);
}
JTextArea noticeArea = new JTextArea(noticeThis);
noticePopup.add(noticePanel);
noticePanel.add(noticeArea);
JScrollPane jScrollPane = new JScrollPane( noticePanel);
// only a configuration to the jScrollPane...
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

// Then, add the jScrollPane to your frame
noticePopup.getContentPane().add(jScrollPane);

JScrollPane scroll=new JScrollPane(noticePanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
noticePopup.getContentPane().add(scroll);
java swing jframe jscrollpane
2个回答
1
投票

您正在创建两个具有相同内容的滚动窗格:

 JScrollPane jScrollPane = new JScrollPane( noticePanel);

和:

JScrollPane scroll=new JScrollPane(noticePanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

并且你将它们都添加到弹出窗口中,并且你还将noticepanel添加到弹出窗口中,因此需要删除大量代码

                Popup noticePopup = new Popup("Notice", 1500, 900);
                JPanel noticePanel = new JPanel();
                noticePanel.setPreferredSize(new Dimension(1500,1000));
                List<List> ListNote = controller.Medicament.consultationNotice(medocSelectStr);
                String noticeThis =null;
                for(int n=0;n<ListNote.size();n++) {
                    noticeThis = (String) ListNote.get(n).get(1);
                }
                JTextArea noticeArea = new JTextArea(noticeThis);
                noticePanel.add(noticeArea);

                JScrollPane scroll=new JScrollPane(noticePanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                noticePopup.getContentPane().add(scroll);

1
投票

我看到很多潜在的问题:

Popup noticePopup = new Popup("Notice", 1500, 900);
JPanel noticePanel = new JPanel();
noticePanel.setPreferredSize(new Dimension(1500,1000));

不要尝试手动编码框架或面板的大小:

  1. 你不知道人们将使用什么分辨率
  2. 你提供的数字毫无意义。面板的宽度不能与框架相同,因为框架有边框。高度不能相差100,因为面板将展开以填充可用空间。

如果您希望框架填满屏幕,那么您只需使用:

noticePopup.setExtendedState(JFrame.MAXIMIZED_BOTH);

您希望以下代码做什么:

for (int n=0; n < ListNote.size(); n++) 
{
    noticeThis = (String) ListNote.get(n).get(1);
}

如果列表中有多个项目,则只需将“noticeThis”变量中的文本替换为列表中的最后一项。

相反,您应该将文本附加到文本区域:

JTextArea noticeArea = new JTextArea(5, 20);

for (int n = 0; n < ListNote.size(); n++) 
{
    if (n > 0 ) noticeArea.append("\n");

    noticeArea.append( (String)ListNote.get(n).get(1) );
}

为什么你有“noticePanel”:

noticePanel.add(noticeArea);
JScrollPane scroll=new JScrollPane(noticePanel, ...);
noticePopup.getContentPane().add(scroll);

默认情况下,JPanel使用FlowLayout,它将遵循添加到其中的任何组件的首选大小。我不知道文本区域的首选大小是什么,但只有当组件的首选大小大于滚动窗格的大小时,滚动条才会出现。

只需将文本区域直接添加到滚动窗格,滚动条将根据需要显示:

JScrollPane scroll=new JScrollPane(noticeArea, ...);
noticePopup.add(scroll);

请注意,自JDK1.4起,您不需要使用getContentPane()

© www.soinside.com 2019 - 2024. All rights reserved.