在jframe中加竖线
时间: 2024-02-23 17:02:56 浏览: 83
要在JFrame中添加一条竖线,你可以使用JPanel来实现。首先,你需要创建一个JPanel,并使用GridLayout来将其分成两列。然后在第一列添加你的组件,例如按钮、标签等。接下来,在第二列添加一条垂直的分隔线,你可以使用JSeparator组件来实现。最后,将这个JPanel添加到你的JFrame中。
以下是一个示例代码:
```
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
public class VerticalLineExample extends JFrame {
public VerticalLineExample() {
// 创建一个JPanel,并将其分成两列
JPanel panel = new JPanel(new GridLayout(1, 2));
// 在第一列添加组件
panel.add(new JButton("Button 1"));
panel.add(new JLabel("Label 1"));
// 在第二列添加垂直分隔线
panel.add(new JSeparator(JSeparator.VERTICAL));
// 添加JPanel到JFrame中
add(panel);
// 设置JFrame属性
setSize(300, 200);
setTitle("Vertical Line Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new VerticalLineExample();
}
}
```
运行这个代码,你将会看到一个包含一条竖线的JFrame窗口。
阅读全文
相关推荐
















