0

I asked a question before, on how to make a JTextField insert its text from beneath. Now I got some problem with the code. I edited a little bit, just for testing. Here is the code I'm using right now:

public class BaseTextAreaDemo {

private static JTextArea textArea;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            final JFrame frame = new JFrame("Base JTextArea App");
            final JPanel panel = new JPanel();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            panel.setLayout(new BorderLayout());
            JPanel textAreaPanel = getBaseTextArea();
            JScrollPane scrollPane = new JScrollPane(textAreaPanel)  {
                public Dimension getPreferredSize() {
                    return new Dimension(300, 230);
                }
            };

            panel.add(scrollPane, BorderLayout.SOUTH);
            JTextField textField = new JTextField();
            textField.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    textArea.append(arg0.getActionCommand() + "\n");

                }

            });
            frame.add(panel, BorderLayout.CENTER);
            frame.add(textField, BorderLayout.SOUTH);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }

        private JPanel getBaseTextArea() {
            textArea = new JTextArea();
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            textArea.append("bla bla bla\n");
            textArea.append("new text here");
            JPanel panel = new JPanel(new BorderLayout());
            panel.setBackground(textArea.getBackground());
            panel.setBorder(textArea.getBorder());
            textArea.setBorder(null);
            panel.add(textArea, BorderLayout.SOUTH);

            return panel;
        }

    });

}

I just added a textField, because I also need one in the program I need this in, and it's useful to add more lines this way.

Now the problem is, when I run this program and I add lines until a vertical scrollbar appears, a horizontal comes out too. I already figured that you can just turn that off, but then some text falls out of the screen. Also, when you make the frame wider, and than return it to it's normal size, the horizontal scrollbar stays at the size from the widened window.

Some pictures of the problem

Another problem is that the speed of scrolling is very low in the JTextField.

4

2 回答 2

0

尝试将 textArea 的 PreferedSize 设置为与您的 JScrollPane 相同:

textArea.setPreferedSize(Dimension(300, 230));

您也可以禁用水平滚动条:

scrollPane.setHorizonalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

您可以像这样设置 Scrollspeed:

scrollPane.getVerticalScrollBar().setUnitIncrement(16);

我可以解决这个问题:它是你添加到滚动窗格的面板。将 JTextArea 直接添加到 ScrollPane 中,仅在需要时才会出现水平栏:

textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
于 2013-07-04T16:11:58.137 回答
0

如果您想完全删除水平滚动条,那么我建议您在您的 : 上调用这行代码JScrollPane

scrollPane.setHorizonalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

于 2013-07-04T16:13:41.943 回答