GridBagLayout 多个按钮 +边界
我正在尝试使用 BoxLayout
在 JPanel
内使用 10 (3x3 + 1) JButtons
的 GridBagLayout
。
但是我对glueboxes 或类似的GridBagLayout
JPanel
所做的一切都会占用BoxLayout
中的所有额外空间。我可能错过了一些东西或者这是不可能做到的?
我使用的一种解决方案是使用 gridbaglayout 内的扩展元素向上推动按钮。这会将按钮放在正确的位置,但边框看起来很大。
下面是我的示例代码:
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GridBagLayoutTest extends JFrame {
public GridBagLayoutTest(){
super();
this.setTitle("JVectorView");
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = this.getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(new JLabel("Hello!"));
content.add(new Controls());
content.add(Box.createGlue());
this.setVisible(true);
}
private class Controls extends JPanel{
private static final int WIDTH = 3, HEIGHT = 3;
public Controls(){
GridBagConstraints constraints = new GridBagConstraints();
//this.setBorder(BorderFactory.createLineBorder(Color.red));
this.setBorder(BorderFactory.createTitledBorder("Some stuff"));
constraints.fill = GridBagConstraints.NONE;
this.setLayout(new GridBagLayout());
for(int row = 0; row < HEIGHT; row++){
for(int col = 0; col < WIDTH; col++){
constraints.gridx = col;
constraints.gridy = row;
this.add(new JButton("B"+(col+row*WIDTH)), constraints);
}
}
constraints.gridx = 1;
constraints.gridy = 3;
this.add(new JButton("B"+(10)), constraints);
}
}
public static void main(String[] args) {
new GridBagLayoutTest();
}
}
我希望按钮周围的边框紧密。是否有可能让 gridbaglayout 折叠在其内容上,或者它总是强制填充面板?
I'm trying to use GridBagLayout
for 10 (3x3 + 1) JButtons
inside a JPanel
using BoxLayout
.
But what ever I do with glueboxes or similarly the GridBagLayout
JPanel
takes up all the extra space in the BoxLayout
. I'm probably missing something or is this not possible to do?
One solution I have used is to push the buttons up with a expanding element inside the gridbaglayout. This puts the buttons in the right place but the border box appear to big.
Here follows my example code:
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GridBagLayoutTest extends JFrame {
public GridBagLayoutTest(){
super();
this.setTitle("JVectorView");
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = this.getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(new JLabel("Hello!"));
content.add(new Controls());
content.add(Box.createGlue());
this.setVisible(true);
}
private class Controls extends JPanel{
private static final int WIDTH = 3, HEIGHT = 3;
public Controls(){
GridBagConstraints constraints = new GridBagConstraints();
//this.setBorder(BorderFactory.createLineBorder(Color.red));
this.setBorder(BorderFactory.createTitledBorder("Some stuff"));
constraints.fill = GridBagConstraints.NONE;
this.setLayout(new GridBagLayout());
for(int row = 0; row < HEIGHT; row++){
for(int col = 0; col < WIDTH; col++){
constraints.gridx = col;
constraints.gridy = row;
this.add(new JButton("B"+(col+row*WIDTH)), constraints);
}
}
constraints.gridx = 1;
constraints.gridy = 3;
this.add(new JButton("B"+(10)), constraints);
}
}
public static void main(String[] args) {
new GridBagLayoutTest();
}
}
I would like the border to be tight around the buttons. Is it at all possible to get gridbaglayout to collapse in on it's content or does it always force fill the panels?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)