有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在JTextArea旁边使用JScrollPane?

我已经写了这段代码。在这里,我想让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) 个答案

  1. # 1 楼答案

    JTextArea已经添加到了JScrollPane中,因此也不需要在JFrame中再次添加它。删除以下行:

    serverFrame.add(notificationsTA);
    

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

    notificationsSP = new JScrollPane(notificationsTA);
    

    一些要点: