ScrollPane не работает - PullRequest
       25

ScrollPane не работает

1 голос
/ 22 декабря 2010

У меня проблема с 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();
    }
}

1 Ответ

1 голос
/ 22 декабря 2010

Проблема в том, что вы добавляете chatOutput в ЦЕНТР панели и chatScroll в ВОСТОК.Я не думаю, что это то, что вы хотите.

Вам нужно добавить chatScroll в центр, как это:

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