Rom*_*man 12 java user-interface swing
我有这样的代码:
JPanel myPanel = new JPanel();
myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
JButton button = new JButton("My Button");
JLabel label = new JLabel("My label!!!!!!!!!!!");
myPanel.add(button);
myPanel.add(label);
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我获得了它们之间没有距离的元素.我的意思是,"顶部"元素总是触及"底部"元素.我该怎么改变它?我想在我的元素之间有一些分离?
我想在我的元素之间添加一些"中间"JPanel(有一些大小).但我不认为这是一种获得理想效果的优雅方式.有人可以帮助我吗?
Kan*_*ath 16
JPanel myPanel = new JPanel();
myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
JButton button = new JButton("My Button");
JLabel label = new JLabel("My label!!!!!!!!!!!");
myPanel.add(button);
myPanel.add(Box.createVerticalStrut(20));
myPanel.add(label);
Run Code Online (Sandbox Code Playgroud)
将是一种方式.
如果您肯定打算使用它BoxLayout来布局面板,那么您应该查看如何使用BoxLayout Sun Learning Trail,特别是使用不可见组件作为填充程序部分.简而言之,BoxLayout您可以创建特殊的隐形组件,充当其他组件之间的间隔:
container.add(firstComponent);
container.add(Box.createRigidArea(new Dimension(5,0)));
container.add(secondComponent);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20706 次 |
| 最近记录: |