简体   繁体   中英

Adjust the vertical position of FlowLayout components?

I tried to adjust the vertical position, I looked at all the settings, unfortunately I could not find it.

How can I set the y position of the 2nd button? I want the y position at the top.

private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setBackground(Color.BLACK);
    frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    JButton btnNewButton = new JButton("New button");
    btnNewButton.setPreferredSize(new Dimension(91, 95));
    frame.getContentPane().add(btnNewButton);

    JButton btnNewButton_1 = new JButton("New button");
    frame.getContentPane().add(btnNewButton_1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
}

I've been using something like this to set a buffer area to manually affect where my components position. you have to keep adjusting the dimension until you get it where you want though. There may be a better way, but I haven't found it yet.

frame.add(someComponent);
frame.add(Box.createRigidArea(new Dimension(100, 0)));
frame.add(otherComponent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM