【问题标题】:Setting alignment of different groups of JComponent objects设置不同组 JComponent 对象的对齐方式
【发布时间】:2014-06-15 02:43:54
【问题描述】:

我有一个关于如何在我的 GUI 上对齐组件的问题。

参考上面的屏幕截图,我希望复选框相互对齐,但在这里我得到了一个不符合要求的复选框(LC 代理),因为在同一行。

任何人都可以建议我应该进行必要的更改以实现我想要的吗? (也欢迎其他关于如何改进代码/显示的建议!)

(P/S 抱歉,如果标题具有误导性;不知道我还能如何命名)

这是我的 GUI 的代码:

/**
 * Builds the elements for the GUI
 */
private void build() {
    // define properties
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocation(100, 100);
    this.setSize(360, 250);
    this.setResizable(false);

    // build elements
    movFileField = new JTextField(30);
    movFileField.setEditable(false);
    JLabel textFieldLabel = new JLabel("MOV file: ");
    textFieldLabel.setLabelFor(movFileField);

    JPanel filenamePane = new JPanel();
    filenamePane.setLayout(new FlowLayout());
    filenamePane.add(textFieldLabel);
    filenamePane.add(movFileField);

    JCheckBox checkIPV = new JCheckBox("IPV Proxy");
    checkIPV.setSelected(configMap.get(EpoxyConfigField.IPV_PROXY.name()).isEnabled());
    toggleMap.put(EpoxyConfigField.IPV_PROXY, checkIPV);
    flickComps.add(checkIPV);

    JCheckBox checkITX = new JCheckBox("ITX Proxy");
    checkITX.setSelected(configMap.get(EpoxyConfigField.ITX_PROXY.name()).isEnabled());
    toggleMap.put(EpoxyConfigField.ITX_PROXY, checkITX);
    flickComps.add(checkITX);

    JCheckBox checkLC = new JCheckBox("LC Proxy");
    checkLC.setSelected(configMap.get(EpoxyConfigField.LC_PROXY.name()).isEnabled());
    toggleMap.put(EpoxyConfigField.LC_PROXY, checkLC);
    flickComps.add(checkLC);

    JComboBox<String> comboLCProfile = new JComboBox<String>("A;C;D;Dxd;E".split(";"));

    JCheckBox checkArchive = new JCheckBox("Archive Asset");
    checkArchive.setSelected(configMap.get(EpoxyConfigField.ARCHIVE.name()).isEnabled());
    toggleMap.put(EpoxyConfigField.ARCHIVE, checkArchive);
    flickComps.add(checkArchive);

    JPanel checkboxPane = new JPanel();
    checkboxPane.setLayout(new BoxLayout(checkboxPane, BoxLayout.PAGE_AXIS));
    checkboxPane.add(checkIPV);
    checkboxPane.add(checkITX);

// This is the part I'm having problems with
    JPanel lcproxyPane = new JPanel();
    lcproxyPane.add(checkLC);
    lcproxyPane.add(comboLCProfile);
    checkboxPane.add(lcproxyPane);

    checkboxPane.add(checkArchive);
    checkboxPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

    JButton processButton = new JButton("Process");
    processButton.setActionCommand(COMMAND_PROCESS);
    processButton.addActionListener(this);
    flickComps.add(processButton);

    JButton selectButton = new JButton("Select");
    selectButton.setActionCommand(COMMAND_SELECT);
    selectButton.addActionListener(this);
    flickComps.add(selectButton);

    statusField = new JTextArea(10,20);
    statusField.setEditable(false);
    statusField.setLineWrap(true);

    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.add(Box.createRigidArea(new Dimension(50, 0)));
    buttonPane.add(processButton);
    buttonPane.add(Box.createHorizontalGlue());
    buttonPane.add(selectButton);
    buttonPane.add(Box.createRigidArea(new Dimension(50, 0)));
    buttonPane.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));

    JPanel bottomPane = new JPanel();
    bottomPane.setLayout(new BoxLayout(bottomPane, BoxLayout.PAGE_AXIS));
    bottomPane.add(buttonPane);
    bottomPane.add(new JScrollPane(statusField));

    Container contentPane = this.getContentPane();
    contentPane.add(filenamePane, BorderLayout.PAGE_START);
    contentPane.add(checkboxPane, BorderLayout.CENTER);
    contentPane.add(bottomPane, BorderLayout.PAGE_END);

    this.pack();
}

【问题讨论】:

    标签: java swing user-interface alignment


    【解决方案1】:

    您无法对齐组件的原因是您使用的布局很难对齐这些组件。 我建议你使用GridBagLayout 而不是 FlowLayout。

    例子:

        JPanel checkboxPane = new JPanel();
    
        GridBagLayout gbl_checkboxPane = new GridBagLayout();
        gbl_checkboxPane.columnWidths = new int[]{430, 0, 0};
        gbl_checkboxPane.rowHeights = new int[]{27, 27, 27, 27};
        gbl_checkboxPane.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
        gbl_checkboxPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        checkboxPane.setLayout(gbl_checkboxPane);
    
        JCheckBox checkIPV = new JCheckBox("IPV Proxy");
    
        GridBagConstraints gbc_checkIPV = new GridBagConstraints();
        gbc_checkIPV.anchor = GridBagConstraints.WEST;
        gbc_checkIPV.fill = GridBagConstraints.VERTICAL;
        gbc_checkIPV.insets = new Insets(0, 0, 5, 5);
        gbc_checkIPV.gridx = 0;
        gbc_checkIPV.gridy = 0;
        checkboxPane.add(checkIPV, gbc_checkIPV);
    

    【讨论】:

    • 感谢您提供此示例。问题是我一直在尝试不同的布局,但由于我对不同的经理缺乏了解,我没有得到太多的结果。我会试一试,看看会发生什么!
    • 使用 Swing 布局并不是一件容易的事。我真正建议您做的是使用名为 Windows Builder 的插件安装 eclipse。使用此工具,您可以实时查看您正在构建的内容。祝你好运
    猜你喜欢
    • 2015-10-10
    • 1970-01-01
    • 2013-04-18
    • 2012-04-28
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多