简体   繁体   English

是否可以只在顶部有一个java swing边框?

[英]Is it possible to have a java swing border only on the top side?

我知道如何使用BorderFactory创建边框,但我没有看到任何可以指定我想要边框的边:S

You can use the MatteBorder to specify the dimensions of the border in each side. 您可以使用MatteBorder指定每侧边框的尺寸。 The constructor of MatteBorder is: MatteBorder的构造函数是:

public MatteBorder(int top,
                   int left,
                   int bottom,
                   int right,
                   Color matteColor)

So if you want to have a border only on the bottom and right sides of your JPanel , you could write something like this: 因此,如果您只想在JPanel的底部和右侧设置边框,您可以编写如下内容:

JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 1, Color.BLACK));

From Sun tutorial : 来自Sun教程

The next picture shows some matte borders. 下图显示了一些遮罩边框。 When creating a matte border, you specify how many pixels it occupies at the top, left, bottom, and right of a component. 创建遮罩边框时,可以指定它在组件的顶部,左侧,底部和右侧占据的像素数。

( Java docs ) Java文档

Matte and empty border allow you to specify the sizes on each side, which may be zero. 遮罩和空边框允许您指定每侧的尺寸,可以为零。

The Border interface itself is quite easy to implement yourself if you want a custom look. 如果您想要自定义外观, Border界面本身很容易实现。 I guess there may be third party libraries available containing styles not included within the Java library. 我想可能有第三方库可用,包含Java库中未包含的样式。

text_field.setBorder( new MatteBorder(2, 0, 0, 0, Color.black));

值可以相应地改变。

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

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