如何在JTextArea旁边使用JScrollPane?

时间:2014-06-07 09:52:58

标签: java swing user-interface jscrollpane jtextarea

我写了这段代码。在这里,我想让JScrollPane与JTextArea一起工作。但它根本不起作用。早些时候我几乎做了同样的事情。它曾经工作过。请提供解决方案。提前致谢。我已经发布了代码。

    protected void startServerProcess(int port) {
    serverFrame = new JFrame("SERVER NOTIFICATIONS PANEL | Labyrinth Developers");
    serverFrame.setSize(500, 500);
    serverFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    serverFrame.setLocationByPlatform(true);
    serverFrame.setLocationRelativeTo(null);
    serverFrame.setVisible(true);

    notificationsTA = new JTextArea();
    notificationsTA.setBounds(0,0,466,500);
    notificationsTA.setLineWrap(true);
    notificationsTA.setRows(1000);

    notificationsSP = new JScrollPane();
    notificationsSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    notificationsSP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    notificationsSP.setViewportView(notificationsTA);
    notificationsSP.setWheelScrollingEnabled(true);
    notificationsSP.setBounds(470, 0, 30, 500);

    serverFrame.add(notificationsTA);
    serverFrame.add(notificationsSP);
}

1 个答案:

答案 0 :(得分:2)

JTextArea已添加到JScrollPane中,因此无需再次在JFrame中添加它。删除以下行:

serverFrame.add(notificationsTA);

您可以使用其Constructor以及内部调用JScrollPane#setViewport()方法在滚动窗格的视口中添加该组件。

notificationsSP = new JScrollPane(notificationsTA);

一些要点: