并排放置按钮

发布于 2024-08-07 13:22:46 字数 645 浏览 5 评论 0原文

我怎样才能让按钮并排放置。我使用 gridBagLayout 来设计布局。问题是按钮彼此距离太远。我尝试选择中心作为锚点,但这会使按钮重叠。如果我使用“WEST”和“EAST”,则按钮彼此之间的距离太远。

例如 SAVE ---------- NEW PATTERN 而不是 SAVE NEW_PATTERN。

JButton bSave = new JButton("SAVE");
JButton bPattern = new JButton("NEW_PATTERN");
con = new GridBagConstraints();
con.anchor=GridBagConstraints.WEST;
con.gridy = 3; con.gridx = 0;           
con.gridwidth = 1; con.gridheight = 1;      
con.insets= new Insets(2,5,2,2);        
m.setConstraints(bSave, con);
c.add(bSave);
con.weightx=1;
con.gridy=3; con.gridx=0;
con.anchor=GridBagConstraints.EAST;
m.setConstraints(bPattern,con);
c.add(bPattern);

how can I make a button place side by side. I used a gridBagLayout to design the layout.The problem is that the button place too far from each other. I have tried to choose the CENTER as anchor but this makes the button overlapping. If I used WEST and EAST, the button placed too far from each other.

e.g. SAVE ---------- NEW PATTERN instead of SAVE NEW_PATTERN.

JButton bSave = new JButton("SAVE");
JButton bPattern = new JButton("NEW_PATTERN");
con = new GridBagConstraints();
con.anchor=GridBagConstraints.WEST;
con.gridy = 3; con.gridx = 0;           
con.gridwidth = 1; con.gridheight = 1;      
con.insets= new Insets(2,5,2,2);        
m.setConstraints(bSave, con);
c.add(bSave);
con.weightx=1;
con.gridy=3; con.gridx=0;
con.anchor=GridBagConstraints.EAST;
m.setConstraints(bPattern,con);
c.add(bPattern);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

旧话新听 2024-08-14 13:22:46

谢谢akf,我已经通过将 flowLayout 放在 gridBagLayout 中解决了这个问题。

....
JButton bSave = new JButton("Save");
JButton bPattern = new JButton("New Pattern");
JPanel pContainer = new JPanel();
pContainer.setLayout(new FlowLayout(FlowLayout.CENTER));
pContainer.add(bSave); pContainer.add(bPattern); 
con = new GridBagConstraints();
con.anchor=GridBagConstraints.CENTER;
con.gridy = 3; con.gridx = 0;           
con.gridwidth = 1; con.gridheight = 1;      
m.setConstraints(pContainer, con);
c.add(pContainer);
....

Thanks akf, I have solved the problem by placing the flowLayout inside the gridBagLayout.

....
JButton bSave = new JButton("Save");
JButton bPattern = new JButton("New Pattern");
JPanel pContainer = new JPanel();
pContainer.setLayout(new FlowLayout(FlowLayout.CENTER));
pContainer.add(bSave); pContainer.add(bPattern); 
con = new GridBagConstraints();
con.anchor=GridBagConstraints.CENTER;
con.gridy = 3; con.gridx = 0;           
con.gridwidth = 1; con.gridheight = 1;      
m.setConstraints(pContainer, con);
c.add(pContainer);
....
甜尕妞 2024-08-14 13:22:46

GridBagLayout 是最复杂的布局。如果您只是对齐几个按钮,我建议使用 FlowLayout(默认)或 BoxLayout。但是,如果您想使用 GridBagLayout,请不要调整锚点,而是将第二个按钮的 gridx 调整为 1。另外,不知道为什么你的网格为 3 而不是 0 的网格(除非你省略了其他使用 0-2 网格的代码)。

GridBagLayout is the most complicated of layouts. If you're just aligning a couple of buttons, I would recommend using FlowLayout (the default) or BoxLayout. But, if you want to use GridBagLayout, instead of adjusting the anchor, adjust the gridx to be 1 for the second button. Also, not sure why you have a gridy of 3 instead of gridy of 0 (unless there is other code you have omitted that uses gridy of 0-2).

倥絔 2024-08-14 13:22:46

GridLayout 是一个简单的解决方案,而不是 GridBagLayout。设置很容易:

JButton bSave = new JButton("SAVE");
JButton bPattern = new JButton("NEW_PATTERN");
JPanel panel = new JPanel(new GridLayout(1,2); // 1 row, 2 cols
panel.add(bSave);
panel.add(bPattern);

编辑:

出于好奇,我在你的原始版本上闲逛,发现了一个仅允许使用 GridBagLayout 的组合。它与你原来的没有太大不同:

GridBagConstraints con = new GridBagConstraints();
con.anchor=GridBagConstraints.WEST;
con.gridy = 3;
con.gridx = 0;                   
con.gridwidth = 1; 
con.gridheight = 1;          
con.insets= new Insets(2,5,2,2);        
m.setConstraints(bSave, con);
c.add(bSave);
con.weightx=0;
con.gridy=3;
con.gridx=1;//this is the big diff!
con.anchor=GridBagConstraints.EAST;
m.setConstraints(bPattern,con);
c.add(bPattern);

Instead of GridBagLayout,GridLayout is a simple solution. It is easy to set up:

JButton bSave = new JButton("SAVE");
JButton bPattern = new JButton("NEW_PATTERN");
JPanel panel = new JPanel(new GridLayout(1,2); // 1 row, 2 cols
panel.add(bSave);
panel.add(bPattern);

EDIT:

Just out of curiosity, I was fooling around with your original and found a combination that allows for the use of only the GridBagLayout. It is not much different than your original:

GridBagConstraints con = new GridBagConstraints();
con.anchor=GridBagConstraints.WEST;
con.gridy = 3;
con.gridx = 0;                   
con.gridwidth = 1; 
con.gridheight = 1;          
con.insets= new Insets(2,5,2,2);        
m.setConstraints(bSave, con);
c.add(bSave);
con.weightx=0;
con.gridy=3;
con.gridx=1;//this is the big diff!
con.anchor=GridBagConstraints.EAST;
m.setConstraints(bPattern,con);
c.add(bPattern);
浪漫人生路 2024-08-14 13:22:46

为什么在发布新问题之前不先阅读旧帖子?

您在上一篇文章中给出了 Swing 教程的链接。因此,请阅读教程,尝试示例并使用适当的布局管理器或布局管理器组合来完成工作。

Why don't you read your old postings first before posting new questions?

You where given a link to the Swing tutorial in your last posting. So read the tutorial, try out the examples and use the appropriate layout manager or combination of layout managers for the job.

孤寂小茶 2024-08-14 13:22:46

此代码将按钮并排放置在屏幕中央。

关键是:

constraints.fill=GridBagConstraints.NONE;-->使按钮不展开

constraints.insets.right=0;-->使按钮并排放置

constraints.insets.left=0;-->使按钮并排站立

constraints.weightx=1 -->使按钮所在的单元格水平展开

constraints.anchor=GridBagConstraints.EAST;-->使左侧按钮位于单元格的

constraints.anchor=GridBagConstraints.WEST;-->使右侧按钮位于单元格的左侧

public static void main(String args[]){
        JFrame frame=new JFrame();
        Container cont=frame.getContentPane();
        cont.setLayout(new GridBagLayout());

        GridBagConstraints constraints=new GridBagConstraints();
        constraints.insets.top=2;
        constraints.insets.bottom=2;
        constraints.insets.left=0;// increment to separate buttons
        constraints.insets.right=0;// increment to separate buttons
        constraints.fill=GridBagConstraints.NONE;
        constraints.weightx=1;
        constraints.gridy=0;

        constraints.anchor=GridBagConstraints.EAST;
        constraints.gridx=0;
        cont.add(new JButton("Save"),constraints);

        constraints.anchor=GridBagConstraints.WEST;
        constraints.gridx=1;
        cont.add(new JButton("New Pattern"),constraints);

        frame.pack();
        frame.setVisible(true);
    }

This code situate the buttons side by side in the center of the screen.

The Key are :

constraints.fill=GridBagConstraints.NONE;--> make the butons not to expand

constraints.insets.right=0;--> makes the buttons stand side by side

constraints.insets.left=0;--> makes the buttons stand side by side

constraints.weightx=1 --> makes the cell in wich the buttons are expando horizontally

constraints.anchor=GridBagConstraints.EAST;--> makes the left button to stand at the of the cell

constraints.anchor=GridBagConstraints.WEST;--> makes the right button to stand at the left of the cell

public static void main(String args[]){
        JFrame frame=new JFrame();
        Container cont=frame.getContentPane();
        cont.setLayout(new GridBagLayout());

        GridBagConstraints constraints=new GridBagConstraints();
        constraints.insets.top=2;
        constraints.insets.bottom=2;
        constraints.insets.left=0;// increment to separate buttons
        constraints.insets.right=0;// increment to separate buttons
        constraints.fill=GridBagConstraints.NONE;
        constraints.weightx=1;
        constraints.gridy=0;

        constraints.anchor=GridBagConstraints.EAST;
        constraints.gridx=0;
        cont.add(new JButton("Save"),constraints);

        constraints.anchor=GridBagConstraints.WEST;
        constraints.gridx=1;
        cont.add(new JButton("New Pattern"),constraints);

        frame.pack();
        frame.setVisible(true);
    }
肥爪爪 2024-08-14 13:22:46

我建议您阅读 GridBagLayout 的教程,因为它是开箱即用的最先进的布局管理器,并且使其他布局管理器变得过时。由于这些原因,它非常值得学习。如果您将其视为网格,那么一切都会变得简单,而您所要做的就是正确设置 x 和 y 坐标,而这正是您在代码中遗漏的。你就快到了:)

混合布局管理器通常是一个非常糟糕的主意,因为在用额外空间填充容器时它们的工作方式都略有不同。

@camickr
根据我的评论,人们应该使用 GridBagLayout,因为它是最灵活的,可以在任何情况下使用。只有一个应该会导致简单且可维护的可能。我还发现它很容易阅读,因为它是逻辑映射的。仅当您对所有组件使用一个约束对象时,约束才会发生变化,这显然是一个坏主意。

I suggest you read the tutorial for GridBagLayout since it is out of the box the most advanced layout manager and makes the other ones obsolete. It is well worth learning for those reasons. It makes everything simple if you think of it as a grid and all you have to get right then is the x and y coordinates which is what you missed in your code. You were almost there :)

Mixing Layout managers is usually a very bad idea since they all work slightly different when it comes to filling out containers with extra space.

@camickr
Based on my comment people should use GridBagLayout since it is the most flexible one and can be used in any situation. Having only one should result in simple and maintainable could. I also find it very easy to read since it is logically mapped. The contstraints only change if you use one constraints object for all components, which is obviously a bad idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文