简体   繁体   English

Java如何在文本组件的右侧显示行号

[英]Java how to display line number on the right side of text components

I want to display line numbers in a text component. 我想在文本组件中显示行号。 I found this link https://tips4java.wordpress.com/2009/05/23/text-component-line-number/ And it worked. 我找到了此链接https://tips4java.wordpress.com/2009/05/23/text-component-line-number/并且它起作用了。 But I want to display line number on the right side of textarea. 但是我想在文本区域的右侧显示行号。 How can I do it. 我该怎么做。 Thank! 谢谢!

TextLineNumber is a Swing component. TextLineNumber是一个Swing组件。 How do you display multiple components in a scroll pane? 如何在滚动窗格中显示多个组件? You add the components to a panel and then add the panel to the viewport of the scroll pane. 您将组件添加到面板中,然后将面板添加到滚动窗格的视口中。 One way might be to use a panel with a BorderLayout: 一种方法是使用带有BorderLayout的面板:

JPanel panel = new JPanel( new BorderLayout() );
panel.add(textArea, BorderLayout.CENTER);
TextLineNumber lineNumber = new TextLineNumber(textArea, 3);
panel.add(lineNumber, BorderLayout.EAST);
JScrollPane scrollPane = new JScrollPane( panel );

Or you could use the existing code and change the orientation of the scroll pane: 或者,您可以使用现有代码并更改滚动窗格的方向:

scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

The line number will be on the right, but the scrollbar will now be on the left. 行号将在右侧,但滚动条现在将在左侧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM