创建一个类似于 Microsoft Word 中的页边距编辑器组件
我有一个 Java Swing 应用程序,我想在其中创建一个很好的组件,就像 Microsoft Word 中的组件一样。在 Microsoft Word 中,您可以更改文档的页边距,如下所示:
这里的技巧是,如果将顶部边距更改为(比方说)1.5",则预览图像将更改以显示此内容,因此线条将在图像中向下移动一点以显示边距的变化,以便用户可以感觉到他的文档将受到此更改的影响,例如,如果我将左边距更改为。 (4.0") 图像将如下所示:
我所做的是创建 2 个图像和一个空白页面图像+ 另一张仅包含线条的图像(线条图像),例如这 2 个图像:
我将 JLabel 中的每个图像插入到彼此上方,现在当我更改 JSpinner 上边距值时,我保持“空白页”图像固定,但更改“线条图像”的边框以将其向下移动一点。该技巧对于上边距效果很好,但如果我更改下/右/左边距,则行为会完全错误。
这是我在更改任何 JSpinner 值时应用的代码:
private void marginSpinnerStateChanged() {
//1. Get the approximate values of all margins :
int topMargin = (int)( Float.valueOf( topSpinner.getValue().toString() ) * 8 );
int bottomMargin = (int)( Float.valueOf( bottomSpinner.getValue().toString() ) * 8 );
int leftMargin = (int)( Float.valueOf( leftSpinner.getValue().toString() ) * 8 );
int rightMargin = (int)( Float.valueOf( rightSpinner.getValue().toString() ) * 8 );
//2. Apply all specified margins to the lines label :
linesLabel.setBorder( new EmptyBorder( topMargin, leftMargin, bottomMargin, rightMargin ) );
}
你能帮助我继续正常工作吗?
I have a Java Swing application that i want to create a nice component in it like a component in Microsoft word. In Microsoft word you can change the margins of your document like in here :
The trick here is that if you change the Top margins to (Let's say) 1.5" then the Preview image will change to show this, so the lines will move down a bit in the image to show that change in the margins so the user can feel how much his document will be affected by this change. So for example if i change the left margin to (4.0") the image will look like this :
What i did is create 2 images a blank page image + another image that contains lines only(Lines image), like these 2 images :
I inserted each image in a JLabel above each other, and now when i change the JSpinner top margin value, i keep the "blank page" image fixed, but i change the border of the "lines image" to move it down a bit. The trick worked fine for the top margin, but the behavior goes totally wrong if i change the bottom/right/left margins.
Here is my code that i apply when changing any JSpinner value :
private void marginSpinnerStateChanged() {
//1. Get the approximate values of all margins :
int topMargin = (int)( Float.valueOf( topSpinner.getValue().toString() ) * 8 );
int bottomMargin = (int)( Float.valueOf( bottomSpinner.getValue().toString() ) * 8 );
int leftMargin = (int)( Float.valueOf( leftSpinner.getValue().toString() ) * 8 );
int rightMargin = (int)( Float.valueOf( rightSpinner.getValue().toString() ) * 8 );
//2. Apply all specified margins to the lines label :
linesLabel.setBorder( new EmptyBorder( topMargin, leftMargin, bottomMargin, rightMargin ) );
}
Can you help me continue this to work right ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以只在纸上绘制图像并随时缩放图像。因此,您可以重写 JComponent 的 PaintComponent() 方法来执行以下操作:
x - 将是左边距
y - 将是上边距
宽度 - 将为 (maxWidth - leftMargin - rightMargin)
height - 将为 (maxHeight - topMargin - BottomMargin)
如果您不喜欢缩放图像,您可以始终使用 BufferedImage,然后使用 getSubImage(...) 方法来获取要绘制的所需尺寸的图像。
You could just draw the image on top of the paper and scale the image as you go. So you would override the paintComponent() method of a JComponent to do something like:
x - would be the left margin
y - would be the top margin
width - would be (maxWidth - leftMargin - rightMargin)
height - would be (maxHeight - topMargin - bottomMargin)
If you don't like scaling the image you can always use a BufferedImage and then use the getSubImage(...) method to get an image the desired size to be painted.
如果你注意到的话,他们不会改变文本图像。相反,他们只显示了一半。这是简单的图像处理。有关一个很好的示例,请参阅 这个。
If you notice, they don't shift the textual image. Instead, they only show half of it. This is simple image manipulation. For a good example, see this.