繁体   English   中英

ScrollPane不起作用

[英]ScrollPane doesn't work

我在使用ScrollPane和TextArea时遇到问题,我在其中放置了一个滚动条,当我尝试在TextArea中键入内容时,它不会滚动,它会变宽。 在这里,您得到了代码:

package interface_Components;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class chatComponent extends JFrame {

    private JTextField chatInput;
    private JTextArea chatOutput;
    private JScrollPane chatScroll;
    private JButton sendButton;
    private JButton newRoomButton;
    private JButton joinRoomButton;
    private JButton inviteButton;
    private JList roomsList;
    private JList usersList;

    public chatComponent() {
        JFrame loggedInWindow = new JFrame("Yikes!");
        chatInput = new JTextField("Type here");
        chatOutput = new JTextArea("Type here and press enter many times scroll doesnt work I dont know why");
        chatScroll = new JScrollPane(chatOutput);
        sendButton = new JButton("Send");
        newRoomButton = new JButton("New Room");
        joinRoomButton = new JButton("Join Room");
        inviteButton = new JButton("Invite");
        roomsList = new JList();
        usersList = new JList();

        chatInput.selectAll();
        chatScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        chatScroll.setAutoscrolls(true);

        chatOutput.setRows(6);
        chatOutput.setLineWrap(true);
        chatOutput.setAutoscrolls(true);

        loggedInWindow.setLayout(new BorderLayout(5, 5));

        JPanel centerPanel = new JPanel(new BorderLayout(5, 5));
        JPanel eastPanel = new JPanel(new BorderLayout(5, 5));

        JPanel centerInternal_1 = new JPanel(new BorderLayout(5, 5));
        JPanel centerInternal_2 = new JPanel(new BorderLayout(5, 5));
        JPanel centerInternal_3 = new JPanel(new BorderLayout(5, 5));

        JPanel eastInternal_1 = new JPanel(new BorderLayout(5, 5));
        JPanel eastInternal_2 = new JPanel(new GridLayout(3, 0, 5, 5));

        centerInternal_3.add(chatInput, BorderLayout.CENTER);
        centerInternal_3.add(sendButton, BorderLayout.EAST);

        centerInternal_2.add(chatOutput, BorderLayout.CENTER);
        centerInternal_2.add(chatScroll, BorderLayout.EAST);
        centerInternal_2.add(centerInternal_3, BorderLayout.SOUTH);

        centerInternal_1.add(centerInternal_2, BorderLayout.SOUTH);
        centerInternal_1.add(roomsList, BorderLayout.CENTER);

        centerPanel.add(centerInternal_1, BorderLayout.CENTER);

        eastInternal_1.add(usersList, BorderLayout.CENTER);

        eastInternal_2.add(newRoomButton);
        eastInternal_2.add(joinRoomButton);
        eastInternal_2.add(inviteButton);

        eastPanel.add(eastInternal_1, BorderLayout.CENTER);
        eastPanel.add(eastInternal_2, BorderLayout.SOUTH);

        loggedInWindow.add(eastPanel, BorderLayout.EAST);
        loggedInWindow.add(centerPanel, BorderLayout.CENTER);

        loggedInWindow.setVisible(true);
        loggedInWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loggedInWindow.setSize(800, 600);
    }

    public static void main(String[] args) {
        chatComponent cc = new chatComponent();
    }
}

问题是您chatOutput添加到面板的CENTER,并将chatScroll到EAST。 我认为这不是您想要的。

您需要像这样将chatScroll添加到CENTER:

//centerInternal_2.add(chatOutput, BorderLayout.CENTER); //don't add chatOutput
centerInternal_2.add(chatScroll, BorderLayout.CENTER);
centerInternal_2.add(centerInternal_3, BorderLayout.SOUTH);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM