滚动窗格不起作用

发布于 2024-10-09 02:33:55 字数 3349 浏览 4 评论 0原文

我对 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();
    }
}

I have a problem with ScrollPane and TextArea I put a scroll there and when I try to type in the TextArea it doesn't scroll it gets wider. Here you got the code for it:

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞旧 2024-10-16 02:33:55

问题是您将 chatOutput 添加到面板的中心,并将 chatScroll 添加到东部。我认为这不是你想要的。

您需要将 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);

Problem is you are adding chatOutput to the CENTER of the panel and chatScroll to the EAST. I don't think this is what you want.

You need to add chatScroll to the CENTER like this:

//centerInternal_2.add(chatOutput, BorderLayout.CENTER); //don't add chatOutput
centerInternal_2.add(chatScroll, BorderLayout.CENTER);
centerInternal_2.add(centerInternal_3, BorderLayout.SOUTH);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文