组合 JTextArea

发布于 2024-10-21 13:43:09 字数 1183 浏览 3 评论 0原文

基本上这就是我想要做的:

我在两个单独的 JScrollPanes 中有两个单独的 JTextArea,并且我想组合它们,因此当第一个 TextArea 的文本太长时,应该在第二个 TextArea 上继续。 但它也应该与滚动一起使用:如果我滚动第一个 TextArea,第二个 TextArea 应该随之滚动(反之亦然)。我尝试计算最大可能的可显示行数,以便我知道有多少行可见。我发现了这一点:

public void paint(Graphics g) {   
        super.paint(g);   
        final FontMetrics fontMetrics = g.getFontMetrics();   
        // get width of any char (font is monospaced)   
        final int charWidth = fontMetrics.charWidth('M');   
        final int charHeight = fontMetrics.getHeight();   
        final int prefWidth = this.getPreferredSize().width;   
        // get height of Parent (JScrollPane)   
        final int prefHeight = this.getParent().getHeight();   

        int maxChars = prefWidth/charWidth;   
        int maxLines = prefHeight/charHeight;   
    }  

但是 getParent().getHeight() 方法不起作用,因为我使用 LayoutManager (SpringLayout)设置了组件的大小,并且我只是定义了约束来设置组件的大小组件,我认为这是固定组件比例的最佳方法,即使在调整窗口大小之后,如果我错了,请纠正我,在尝试了几个小时的各种布局管理器(这些都有点糟糕和丑陋)之后,我没有任何动力进一步思考这个问题)。因此 getHeight() 方法始终返回 0 因为我从未设置过大小。

是否还有其他方法可以获取嵌套在 ScrollPane 中的 TextArea 的可见行?或者我在这里完全错了,应该采取其他方式吗?

感谢您的帮助。

basically this is what i want to do:

I have two seperate JTextAreas in two seperate JScrollPanes and i want to Combine them, so when the text is to long for the first TextArea, it should be continued on the second one.
But it also should work with scrolling: If i scroll the first TextArea, the second should scroll with it (and vice versa). I tried to calculate the maximum possible Lines showable, so that i know how many lines are visible. I found this:

public void paint(Graphics g) {   
        super.paint(g);   
        final FontMetrics fontMetrics = g.getFontMetrics();   
        // get width of any char (font is monospaced)   
        final int charWidth = fontMetrics.charWidth('M');   
        final int charHeight = fontMetrics.getHeight();   
        final int prefWidth = this.getPreferredSize().width;   
        // get height of Parent (JScrollPane)   
        final int prefHeight = this.getParent().getHeight();   

        int maxChars = prefWidth/charWidth;   
        int maxLines = prefHeight/charHeight;   
    }  

but the getParent().getHeight() method is not working, because i set the size of the components with my LayoutManager (SpringLayout, and i simply define constraints to set the size of the Components, which i think is the best method to have fixed proportions of the components, even after resizing the Window. Correct me if i'm wrong here, after hours of trying various LayoutManagers (which are all kind of bad and ugly), i dont have any Motivation to think about this any further). So the getHeight() Method returns 0 all the time because i never set the size.

Are there any other Methods to get the visible Rows of a TextArea nested in a ScrollPane? Or am i completely wrong here and should it some other way?

Thanks for the Help.

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

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

发布评论

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

评论(2

攒眉千度 2024-10-28 13:43:09

我从来没有做过这样的事情,但我认为看待问题的方法是重新定义问题。

您实际上希望对同一个文档有 2 个视图。视图 1 将显示达到某个最大长度的所有字符。视图 2 将显示其余所有视图。

我认为这可以通过创建自定义 文档 来完成 类共享底层数据(可能是一个主文档,这样您就可以获取文档的更新事件)并具有不同的偏移量。

每个 JTextArea 都有它自己的 Document 实例。

当第一个窗口已满时,您仍然必须手动跳转到第二个窗口(尽管您当然可以对 TextArea 进行编程,以在达到 MAX_LENGTH 时自动执行此操作)。

我没有看到连接滚动条的优势,因为它们显示不同的数据。为什么滚动到区域 1 的第 100 行意味着您想要滚动到区域 2 的第 1100 行?

我认为你不应该碰 paint 方法。您需要的是业务逻辑,而不是图形:)

I've never done anything like it, but I think the way to look at the problem is to redefine the question.

You actually want 2 views on the same document. View number 1 would show all chars up to some max length. View number 2 would show all the rest.

I think this could be accomplished by creating a custom Document class which share the underlying data (perhaps a master Document so you'd get update events for your document) and have a different offset.

Each of the JTextAreas would have it's own Document instance.

You'd still have to manually jump to the second window when the first is full (though you could of course program your TextArea to do that automatically when you'd get at MAX_LENGTH).

I don't see advantages of connecting the scrollers as they show different data. Why would scrolling to line 100 at area 1 mean that you'd want to scroll to line 1100 at area 2?

And I think you should not touch the paint method. It's business logic you need, not graphics :)

吃→可爱长大的 2024-10-28 13:43:09

你的要求对我来说毫无意义:

所以当第一个 TextArea 的文本太长时,应该在第二个 TextArea 上继续

好吧,让我们假设创建每个文本区域以显示 10 行。在第一个中,您输入 20 行,此时您溢出到秒,现在您有 1 行。

如果我滚动第一个 TextArea,第二个 TextArea 应该随之滚动(反之亦然)。

当文本区域只有 1 行时如何滚动?

Your requirement makes no sense to me:

so when the text is to long for the first TextArea, it should be continued on the second one

Ok, so lets say create each text area to display 10 lines. In the first you enter 20 lines at which time you overflow to the secon and you now have 1 line.

If i scroll the first TextArea, the second should scroll with it (and vice versa).

How do you scroll the text area when it only has 1 line?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文