首页 > 解决方案 > JFrame中的滚动窗格未显示

问题描述

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

这是我的代码:

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);

标签: javaswingjframejscrollpane

解决方案


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

 JScrollPane jScrollPane = new JScrollPane( noticePanel);

和:

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

而且您将它们都添加到弹出窗口中,并且还将通知面板添加到弹出窗口中,因此需要删除很多代码

                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);

推荐阅读