ScrollPane不起作用

时间:2010-12-22 14:40:54

标签: java swing jpanel scrollpane

我有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 个答案:

答案 0 :(得分:1)

问题是您要将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);
相关问题