1

我正在尝试使用 GroupLayout 使“>>”按钮垂直居中。

但正如你所看到的,它把它放在顶部附近。我试过弄乱布局,但似乎无法让它工作。请帮忙!=)

horizontal_group = layout.createParallelGroup GroupLayout::Alignment::CENTER
horizontal_group.addComponent tags_label
available_across_hor = layout.createSequentialGroup
available_down_hor = layout.createParallelGroup GroupLayout::Alignment::CENTER
available_down_hor.addComponent available_label
available_down_hor.addComponent available_pane
available_down_hor.addComponent new_tag_button
selected_down_hor = layout.createParallelGroup GroupLayout::Alignment::CENTER
selected_down_hor.addComponent selected_label
selected_down_hor.addComponent selected_pane
selected_down_hor.addComponent remove_tag_button
available_across_hor.addGroup available_down_hor
available_across_hor.addComponent move_button
available_across_hor.addGroup selected_down_hor
horizontal_group.addGroup available_across_hor

vertical_group = layout.createSequentialGroup
vertical_group.addComponent tags_label
available_across_ver = layout.createParallelGroup
available_down_ver = layout.createSequentialGroup
available_down_ver.addComponent available_label
available_down_ver.addComponent available_pane
available_down_ver.addComponent new_tag_button
selected_down_ver = layout.createSequentialGroup
selected_down_ver.addComponent selected_label
selected_down_ver.addComponent selected_pane
selected_down_ver.addComponent remove_tag_button
available_across_ver.addGroup available_down_ver
available_across_ver.addComponent move_button
available_across_ver.addGroup selected_down_ver
vertical_group.addGroup available_across_ver

layout.setHorizontalGroup horizontal_group
layout.setVerticalGroup vertical_group

用户界面

4

2 回答 2

1

这是解决方案:

package com.zetcode;

import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.CENTER;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;


public class GroupLayoutTags extends JFrame {

    public GroupLayoutTags() {

        initUI();

        setTitle("Tags");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          
    }

    private void initUI() {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl); 

        JLabel avLbl = new JLabel("Available");
        JLabel tagsLbl = new JLabel("Tags");
        JLabel selLbl = new JLabel("Selected");

        JButton newBtn = new JButton("New");
        JButton moveBtn = new JButton(">>");
        JButton remBtn = new JButton("Remove");

        JList leftList = new JList();
        JScrollPane spleft = new JScrollPane(leftList);
        JList rightList = new JList();
        JScrollPane spright = new JScrollPane(rightList);

        gl.setAutoCreateGaps(true);
        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createParallelGroup(CENTER)
                .addComponent(tagsLbl)
                .addGroup(gl.createSequentialGroup()
                        .addGroup(gl.createParallelGroup(CENTER)
                                .addComponent(avLbl)
                                .addComponent(spleft, 100, 200, Short.MAX_VALUE)
                                .addComponent(newBtn))
                        .addComponent(moveBtn)
                        .addGroup(gl.createParallelGroup(CENTER)
                                .addComponent(selLbl)
                                .addComponent(spright, 100, 200, Short.MAX_VALUE)
                                .addComponent(remBtn)))
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(tagsLbl)
                .addGroup(gl.createParallelGroup(CENTER)
                        .addGroup(gl.createSequentialGroup()
                                .addComponent(avLbl)
                                .addComponent(spleft, 100, 250, Short.MAX_VALUE)
                                .addComponent(newBtn))
                        .addComponent(moveBtn)
                        .addGroup(gl.createSequentialGroup()
                                .addComponent(selLbl)
                                .addComponent(spright, 100, 250, Short.MAX_VALUE)
                                .addComponent(remBtn)))
        );

        pack();        

    }


    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GroupLayoutTags ex = new GroupLayoutTags();
                ex.setVisible(true);
            }
        });
    }
}

如果 find MigLayoutmanager 比GroupLayout. 不过通过一些练习,GroupLayout也是可行的。

GroupLayout 标签

于 2014-08-06T21:15:43.387 回答
0

尝试创建填充组件:

    Component topFiller = Box.createVerticalGlue();
    Component bottomFiller = Box.createVerticalGlue();

然后在 >> 按钮之前添加 topFiller ,并在其后添加 bottomFiller 用于水平和垂直组。

于 2021-12-10T09:05:30.080 回答