在flowLayout java中组织jbuttons

时间:2014-04-21 15:24:21

标签: java layout

我正在努力制作一个好看的布局。

我想在左侧添加一个按钮,在右侧添加2个按钮。 我尝试了很多方法让它看起来很好看,因此不能很好地工作。任何帮助都会很棒。

JPanel btn1 = new JPanel();
btn1.setLayout(new FlowLayout(FlowLayout.RIGHT));
btn1.add(new JButton("btn1"));
JButton btn2 = new JButton("btn2");
btn1.add(btn2);
pane.add(btn1, BorderLayout.SOUTH);

由于我不允许发布图片:

-----------------------------------------
| ------------------------------------- |
| |                                   | |
| |                                   | |
| |                                   | |
| |                                   | |
| ------------------------------------- |
| [ enter ]                [btn1][btn2] |
-----------------------------------------

如何将输入按钮设为左侧。

感谢。

尝试3:

JPanel southPanel = new JPanel(new GridLayout(1,2));
    JPanel enter = new JPanel();
    JPanel btn1_btn2 = new JPanel();

    enter.add(new JButton("Enter"));
    enter.setLayout((new FlowLayout(FlowLayout.LEADING)));
    southPanel.add(enter);


    btn1_btn2.add(new JButton("btn1"));
    btn1_btn2.add(new JButton("btn2"));
    btn1_btn2.setLayout((new FlowLayout(FlowLayout.TRAILING)));

    southPanel.add(btn1_btn2);

3 个答案:

答案 0 :(得分:0)

  1. 使用BorderLayout创建面板。
  2. 将“Enter”按钮添加到WEST。
  3. 创建第二个面板,并将另外两个按钮添加到面板。然后使用BorderLayout
  4. 将该面板添加到面板的EAST中
  5. 将带有BorderLayout的面板添加到您的框架中。

答案 1 :(得分:0)

您可以嵌套具有不同FlowLayout方向(LEADING,TRAILING)的面板

enter image description here

答案 2 :(得分:0)

以下是您要求的示例:

虽然GridBagLayout可能相当复杂,但它非常灵活。一旦你熟悉了你的选择(体重,锚,ipadx等等),它真的不那么难了。

我强烈建议您阅读GridBagLayout并了解随之而来的功能。 与其他布局不同,GridBagLayout依赖于另一个对象(调用约束)来为组件设置属性。所有组件通常共享相同的GridBagConstraints以避免不必要的对象配置

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class App {

    private static JFrame frame;
    public static void main(String[] args) {
        initFrame();
    }

    private static void initFrame() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() { //all swing actions should be handled on EDT
                public void run() {
                    frame = new JFrame();
                    frame.setSize(500, 500);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close process on X

                    addComponents(frame.getContentPane()); //add components to frame

                    frame.setLocationRelativeTo(null); //set frame in center of screen
                    frame.setVisible(true);
                }
            });
        } catch (InvocationTargetException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void addComponents(Container pane) {
        pane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        JButton button = new JButton("button1");
        JButton button2 = new JButton("b2");
        JButton button3 = new JButton("b3");

        //we are anchroing our button to the bottom-left side of the screen
        gbc.anchor = GridBagConstraints.SOUTHWEST;

        //weightx and y specifiy that our comp isn't just mindlessly floating around
        //for weightx, if there is any extra space on the X axis, fill ass needed
        //same for weighty. if we didnt have these vars, our comp will float in the middle
        //basically, if you dont have these, your "anchor" wont be noticed
        gbc.weighty = .5;
        gbc.weightx = .5; 
        pane.add(button, gbc);

        //since we are now working with the other 2 buttons, make it south east
        //we must change our weightx back to 0, to ensure there will be no spacing between
        //our two buttons (even if there is extra space, dont use it)
        gbc.anchor = GridBagConstraints.SOUTHEAST;
        gbc.weightx = 0;
        pane.add(button2, gbc);
        pane.add(button3, gbc);
    }
}