java Box添加垂直方向上的框
时间: 2023-08-22 15:06:39 浏览: 51
您可以使用javax.swing.Box类的静态方法createVerticalBox()来创建一个垂直Box容器,然后使用javax.swing.border.BorderFactory类的静态方法createLineBorder()来创建一个带有线条的边框,最后将边框设置为Box容器的边框即可。以下是示例代码:
```
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class Example {
public static void main(String[] args) {
JFrame frame = new JFrame("Vertical Box Example");
JPanel panel = new JPanel();
// 创建垂直Box容器
Box verticalBox = Box.createVerticalBox();
// 创建边框
Border border = BorderFactory.createLineBorder(Color.BLACK);
// 添加组件到Box容器中
verticalBox.add(new JLabel("Component 1"));
verticalBox.add(Box.createVerticalStrut(10)); // 添加垂直间距
verticalBox.add(new JLabel("Component 2"));
verticalBox.add(Box.createVerticalStrut(10)); // 添加垂直间距
verticalBox.add(new JLabel("Component 3"));
// 设置边框
verticalBox.setBorder(border);
panel.add(verticalBox);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
```
运行代码后,您将看到一个带有垂直方向上的边框的Box容器,其中包含三个标签组件。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)