如何使 JTextArea 自动滚动

时间:2021-05-26 19:27:00

标签: java swing scroll jtextarea

我的 JTextArea i java 有问题。当我在文本区域打印输出时,它不会自动滚动到底部。当它到达文本区域的底部时,我无法使用滚动面板滚动它。这是我的 GUI 代码:

public void initializeWindow()
    {
        JPanel pan;
        JPanel colorBox;
        JPanel consolePanel;
        JLabel panText;
        JFrame frame = new JFrame();
        JScrollPane scroll;


        gridPanels = new JPanel[sizeX][sizeY];
        boardPanel = new JPanel();
        legend = new JPanel();
        consolePanel = new JPanel();
        consoleOutput = new JTextArea(25,20);


        consoleOutput.setEditable(false);
        consoleOutput.setPreferredSize(new Dimension( 200,300));
        consoleOutput.setAutoscrolls(true);

        scroll = new JScrollPane(this.consoleOutput, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        consolePanel.setBorder(BorderFactory.createLineBorder(Color.black));
        consolePanel.add(consoleOutput);
        consolePanel.add(scroll);


        boardPanel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
        boardPanel.setLayout(new GridLayout(sizeX,sizeY));

        legend.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
        legend.setPreferredSize( new Dimension(300,boardPanel.getHeight()));


        PrintStream printStream = new PrintStream(new CustomOutputStream(consoleOutput));


        for (Organizm org: legendOrgs)
        {
            pan = new JPanel();
            colorBox = new JPanel();
            panText = new JLabel();

            pan.setMaximumSize(new Dimension(100,70));
            pan.setAlignmentX(Component.LEFT_ALIGNMENT);
            pan.setLayout(new FlowLayout(FlowLayout.LEADING));

            colorBox.setBackground(org.getOrgColor());
            colorBox.setAlignmentX(Component.LEFT_ALIGNMENT);
            colorBox.setPreferredSize(new Dimension(30,30));
            colorBox.setMaximumSize(new Dimension(30,30));

            panText.setPreferredSize(new Dimension(100,15));
            panText.setText(" - " + org.getName());
            panText.setAlignmentX(Component.RIGHT_ALIGNMENT);

            pan.add(colorBox);
            pan.add(panText);

            legend.add(pan);
        }
        legend.add(consolePanel);

        for(int i=0; i<sizeY; i++)
        {
            for(int j=0; j<sizeX; j++)
            {
                gridPanels[i][j] = new JPanel();
                if(organizmy[i][j]!=null)
                    gridPanels[i][j].setBackground(organizmy[i][j].getOrgColor());
                else gridPanels[i][j].setBackground(Color.white);
                boardPanel.add(gridPanels[i][j]);
            }
        }

        System.setOut(printStream);
        System.setErr(printStream);
        frame.add(boardPanel);
        frame.add(legend,BorderLayout.EAST);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Wirtualny świat");
        frame.pack();
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);
        worldFrame = frame;
    }

这是我的自定义输出流类,用于将我通过 System.out.println 打印的所有内容打印到我的文本区域:

public class CustomOutputStream extends OutputStream
{
    private final JTextArea textArea;

    public CustomOutputStream(JTextArea textArea)
    {
        this.textArea = textArea;
    }

    @Override
    public void write(int b)
    {
        textArea.append(String.valueOf((char)b));
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }




}

这是图形界面中的图像链接:

2 个答案:

答案 0 :(得分:1)

您需要从代码中删除这一行:

consoleOutput.setPreferredSize(new Dimension( 200,300));

不幸的是,它会阻止您的 JTextArea 滚动,因为您为该元素设置了静态大小。

附言远离 Swing - Java 中有更好的选择

答案 1 :(得分:0)

它对我有用。

import javax.swing.*;
public class Scrollin{

    public static void main(String[] args){
        JFrame frame = new JFrame("scrolling");
        JTextArea area = new JTextArea(20, 20);
        
        frame.add(new JScrollPane(area));
        frame.setVisible(true);
        frame.pack();
        Timer t = new Timer( 150, evt->{
            area.setCaretPosition( area.getDocument().getLength() );
            area.append("word is born\n");
        });
        t.start();
    }
}

添加文本时,如果光标位于文档末尾,窗口将滚动到末尾。

也许您可以从这样简短的内容开始来证明您的问题?

相关问题