摇摆外行分页 [英] Swing layman pagination

查看:76
本文介绍了摇摆外行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开始之前,存在类似的问题(我的),我希望删除该问题,因为我在那儿没有很好地解释我的观点,但不是这一点.谢谢.

Before I start, similar question (of mine) exists, I'd like that one to be deleted since I didn't explain my point well there, but not this one. Thank you.

首先,我没有代码,这只是我不知道的概念.但这很有趣(至少对我而言).

First of all, I have no code, this is only a concept that I can't figure out. But it's interesting (at least to me).

你们都知道MSWord是如何工作的.你写东西.然后,当您填充页面时,它将创建一个新页面并开始在该页面上书写.如果将更多文本粘贴到首页,则所有内容都会被下推.如果您删除页面上的大量文本,则会吸收上一页的一些文本.如果您正在处理例如图片,然后在页面顶部放置一个图片,如果没有足够的空间容纳图片的缩小版,请缩小图片尺寸,直到上一页为止.

You all know how MSWord works. You write stuff. Then, when you fill a page it will create a new page and start writing on that page. If you paste some more text to the first page, everything will be pushed down. If you delete a large chunk of text on a page, it will suck up some of the text on the previous page. If you're dealing with e.g. pictures and you have one on top of a page, reducing it's size my cause it to get sucked up to the previous page, if there is enough room for the reduced version of the picture.

现在您正在考虑这种方式,我想将该概念转换为Java Swing.页面是JPanels,图片和文本块(或行)是安装在页面JPanel上的JPanels.

Now that you're thinking this way, I want to transfer that concept into Java Swing. Pages are JPanels, and pictures and chunks (or lines) of text are JPanels fitted onto the page JPanel.

我已经提出了使用Filler的方法的方法(好的,我撒谎了,我有一些代码,但是一团糟,无论如何它都行不通),但在所有情况下都行不通.如果您想知道为什么,请在两行之间阅读,否则就跳过它.

I have come up (okay, I lied, I have some code, but it's a mess and it doesn't work anyway) with a method using a Filler, which doesn't work in all cases. If you want to know why, read between the two lines, otherwise just skip it.

因此,结构本身很容易复制,但是保持它却是脖子上的痛苦.您会看到,可能发生两种主要类型的事件:

So, the structure itself is easy to replicate, but maintaining it is a pain in the neck. You see, there are two main types of events that can occur:

a) height of the page content has increased
b) height of the page content has decreased

通过使用Filler作为页面的最后一个组件,并在其上附加componentAdapter(componentResized),您可以监视这些更改.

By using a Filler as the last component of a page, and having a componentAdapter (componentResized) attached to it, you can monitor those changes.

这些更改可以进一步分为:

These changes can further be dividet into:

a) element is added/removed to/from page
b) height of the element has increased/decreased 

考虑到这些事件,可能会发生许多事情.跳过简单的情况,请看以下示例:

Taking those events into consideration many things can happen. Skipping the simple cases, look at this example:

Page 1:
{element 1
blabla
blabla}
{element 2
blabla}
{element 3}
{element 4
blabla
blabla
blabla
blabla}
{free space
---
---
---}

/

Page 2:
{element 1
blabla
blabla
blabla
blabla}
{element 2
blabla
blabla
blabla
blabla}
{element 3}
{element 4
blabla
blabla
blabla}

/

Page 3:
{element 1}
{element 2}
{element 3}
{element 4}
{element 5}
{free space
---
---
---
---
---
---
---
---
---}

每页高度为15行.现在看一下,如果将第二页元素1的高度减少一行,会发生什么.它将变为4行高,使其适合上一页,被吸住了.这将在第二页上创建5(价值1(已删除的行+ 4被吸引的行))行的可用空间.这将吸收第三页上的所有五个元素,而将第三页留空(现在应将其删除).

Each page has a height of 15 rows. Look now what happens if you reduce the height of the element 1 of the second page by one row. It will become 4 rows high, making it fit to the previous page, being sucked up. That will create 5(1 deleted row + 4 sucked up rows) rows worth of free space on the second page. That will suck up all the five elements on the third page and leave the third page blank (which should now be deleted).

之所以不起作用,是因为在删除后,第二页触发了一个侦听器,它既要向上推动顶部元素,又要吸收上一页中的元素.由于所有操作都是在侦听器中完成的,因此我必须等待其执行才能在程序中注册可视化更改.由于必须在页面上更改两件事,因此会引起某种听者混乱.页面高度减少了两次,但只注册了一次,最后我只能完全移动其顶部或底部,或每侧一个组件.这并不是一个很好的解释,但是,如果您了解秋千的工作原理,那么您应该可以自己连接圆点.

The reason this wouldn't work is because upon deletion, a listener is triggered for the second page and it has to both push the top element up, and suck up the elements from the previous page. Since it's all done in a listener, I have to wait for it to execute in order to register visual change in my program. Since it has to change two thing on a page, it comes to somekind of listener chaos. Page height is reduced twice but is registered only once and in the end I can fully move only thr top part or the bottom part, or a single component on each side. This isn't really a good explenation, but if you understand how swing works, you should be able to connect the dots yourself.

正如我之前提到的,我已经为此编写了代码,但是它很长且很难遵循,如果有人愿意看到它,我可以在这里发布.我说的是SSCCE本身.确实不能将其缩短为几十行的代码.

As I mentioned before, I have written the code for that, but it's long and hard to follow, and I can post it here, if anyone shows the desire to see it. And I'm talking about SSCCE itself. It really can't be shortened into couple of 10s of rows of code.

我要跳过编写维持文档"结构并移动所有元素的算法,因为这是一件非常复杂的事情,需要处理很多情况.

What I would want is to skip writting an algorithm that would maintain the structure of the "document" and move all the elements around, since it's a really complicated thing to do, taking all the numerous cases.

我想要的是替代方法,我问您是否有任何想法.我想到的一件事是具有类似于JPanel的组件.它将具有可以与其他组件一起填充的固定高度部分,并且在它们之间具有不可填充(?)或实心"的固定高度部分.

What I want is an alternative and I'm asking you if you have any ideas. One thing that came to my mind is having a component similar to JPanel. It would have fixed height-parts that can be populated with other components, and between them fixed height-parts that are unpopulable(?) or "solid".

可行的方法是,每次将某些内容添加到可填充(?)部件时,它们都会自动重新排列.如果某些内容不适合当前的可填充部分,则将其移至下一个(类似于Verticall Box布局的工作原理,将一个内容添加到某个位置会将所有其他内容向下推),但跳过固定的部分.

Way it would work would be that everytime you add something to populable(?) parts, they would be automatically rearanged. If something doesn't fit to the current populable part, it's just moved to the next one (similar to how verticall box layout works, adding one thing to a spot pushes all the others down), but skiping the solid part.

由于我还必须能够确定某个组件位于哪个可填充部分中,所以我不知道在Java swing中是否可以创建这样的结构.

Since I would also have to be able to tell in which populable part a certain component is, I don't know if creating a such structure is possible in Java swing.

好吧,欢迎您提出任何建议,包括外部库.

Well, any advice is welcome, external libaries included.

请记住,整个文档是带有页面的文档,这些页面将在JScrollPane的视口中一个接一个地放置,这是对其外观的唯一限制.

Just keep in mind that this whole document is document with pages that would be placed one after another in a JScrollPane's viewport, and that is the only limit to what it should look like.

推荐答案

让布局完成工作:JPaneladd()实例,每个基于内容都有其自己的首选大小,而Box的实例具有一个垂直布局.将Box放在JScrollPane中,可以选择实现Scrolable.根据需要使用滚动窗格的行标题和列标题. JTable是一个示例.您可以根据需要从Boxrevalidate()repaint()remove()面板.

Let the layout do the work: add() instances of JPanel, each having its own preferred size based on content, to a Box having a vertical layout. Put the Box in a JScrollPane, optionally implementing Scrolable. Use the scroll pane's row and column headers as needed; JTable is an example. You can remove() a panel from the Box, revalidate() and repaint() as required.

附录:最初的答案仅解决了问题的视角方面.它可能有助于分离模型并更严格地进行查看,因为文本组件做;从模型中删除内容,并向视图发送信号以进行相应的更新.为此,在此处中提到了几种用于实现观察者模式的常见方法.

Addendum: The initial answer addressed only the view aspect of the problem. It may help to separate the model and view more rigorously, as the text components do; remove content from the model and signal the view to update itself accordingly. To achieve this, several common approaches to implementing the observer pattern are mentioned here.

这篇关于摇摆外行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆