Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何制作JTextArea autoscroll_Java_Swing_Scroll_Jtextarea - Fatal编程技术网

Java 如何制作JTextArea autoscroll

Java 如何制作JTextArea autoscroll,java,swing,scroll,jtextarea,Java,Swing,Scroll,Jtextarea,我的JTextArea i java有问题。当我在文本区域打印输出时,它不会自动滚动到底部。当它到达文本区域的底部时,我不能用滚动面板滚动它。以下是我的GUI代码: public void initializeWindow() { JPanel pan; JPanel colorBox; JPanel consolePanel; JLabel panText; JFrame frame = new JFram

我的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;
    }
下面是图像在GUI中的外观链接:

它适合我

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();
    }
}
添加文本时,如果光标位于文档末尾,窗口将滚动到末尾


也许您可以从这样简短的内容开始演示您的问题?

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

consoleOutput.setPreferredSize(new Dimension( 200,300));
不幸的是,它使
JTextArea
无法滚动,因为您将静态大小设置为该元素


注意:远离Swing-Java中有更好的选项

感谢您的回复,我会检查一下,然后很快写下我的反馈:).Hmm,所以您将滚动窗格直接添加到框架中。我把它包在另一个面板里了,也许这就是问题所在?ScrollPane是否需要松散地添加到框架中才能正常工作?@XYZEK0我不知道“松散添加”是什么意思。JScrollPane在哪个组件中并不重要,如果它们设置正确,它应该可以正常工作。我认为您应该切换
setCaretPosition
append
调用的顺序。如果光标位于文档的末尾,它将停留在那里。Swing组件应该在EDT上更新。您的意思是类似组件的东西一次只能有一个父组件。删除控制台面板。添加(控制台输出)完整;这一行从JScrollPane中删除您的JTextArea,并将consolePanel指定为JTextArea的新父级。嗯,我删除了consolePanel.add(consoleOutput);正如VGR所建议的,但是我的文本仍然没有移动。我怀疑文本甚至并没有被添加到一个又一个充满文本的文本区域。我真的不知道EDT上的更新意味着什么,你能解释一下吗?EDT是“事件调度线程”,每当你修改swing组件时,你都应该使用这种方式在EDT上进行更改。谢谢。这解决了我的问题。还记得吃2公升的音乐吗。
consoleOutput.setPreferredSize(new Dimension( 200,300));