【问题标题】:Want to alignment of components from the start of the panel in java swing想要从java swing中的面板开始对齐组件
【发布时间】:2014-09-30 10:57:26
【问题描述】:

我有一个面板,并在其顶部连续添加JLabelJTextField

一切正常。但是我的面板很大,我想从面板的左上角添加这些标签和文本字段。

您能否就此提出任何建议..

List<CPWARackExpData> expList = ...;
for (int i = 0; i < expList.size(); i++) {
    JLabel expLabel = expList.get(i).getExpUIComp().getExpLabel();
    String sExpName = expList.get(i).getExpName();
    if ((sExpName.startsWith(sSubString1) == false))
        continue;
    sConfigLabelName = sExpName.substring(sSubString1.length(),
        sExpName.length());
    expLabel.setText(sConfigLabelName);
    add(expLabel, getConstraint(new int[] {1, i, 1, 1}));
    JTextField expTextField = expList.get(i).getExpUIComp()
            .getExpTextField();
    expTextField.setPreferredSize(new Dimension(50, 17));
    expTextField.setText(expList.get(i).getExpValue());
    add(expTextField, getConstraint(new int[] {2, i, 1, 1}));
}

// Create the constraints
private GridBagConstraints getConstraint(int[] c) {
    GridBagConstraints g = new GridBagConstraints();
    g.gridx = c[0];
    g.gridy = c[1];
    g.gridwidth = c[2];
    g.gridheight = c[3];
    g.anchor = GridBagConstraints.FIRST_LINE_START;
    g.ipadx = 30;
    g.ipady = 15;
    return g;
}

【问题讨论】:

  • g.anchor = GridBagConstraints.NORTH_WEST;?
  • 为了尽快获得更好的帮助,请发布MCVE(最小、完整、可验证的示例)。

标签: java swing alignment layout-manager gridbaglayout


【解决方案1】:

组件不要使用setPreferedSize(...),所有尺寸必须由LayoutManager计算。

你可以使用 dummy JLabel,它在面板下抢空间,例如:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class TestFrame extends JFrame {

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        setLayout(new GridBagLayout());
        for (int i = 0; i < 5; i++) {
            JLabel expLabel = new JLabel(""+i);
            String sExpName = "text"+i;

            add(expLabel, getConstraint(new int[] {1, i, 1, 1}));
            JTextField expTextField = new JTextField();
            expTextField.setText(sExpName);
            add(expTextField, getConstraint(new int[] {2, i, 1, 1}));
        }
        GridBagConstraints c = getConstraint(new int[] {1,6,2,1});
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        add(new JLabel(" "),c);
    }

    private GridBagConstraints getConstraint(int[] c) {
        GridBagConstraints g = new GridBagConstraints();
        g.gridx = c[0];
        g.gridy = c[1];
        g.gridwidth = c[2];
        g.gridheight = c[3];
        g.anchor = GridBagConstraints.FIRST_LINE_START;
        g.ipadx = 30;
        g.ipady = 15;
        return g;
    }


    public static void main(String args[]) {
        new TestFrame();
    }

}

或在面板中使用 LayoutManager 的组合,更改 init 方法如下:

private void init() {
    JPanel p = new JPanel();
    p.setLayout(new GridBagLayout());
    for (int i = 0; i < 5; i++) {
        JLabel expLabel = new JLabel(""+i);
        String sExpName = "text"+i;

        p.add(expLabel, getConstraint(new int[] {1, i, 1, 1}));
        JTextField expTextField = new JTextField();
        expTextField.setText(sExpName);
        p.add(expTextField, getConstraint(new int[] {2, i, 1, 1}));
    }
    JPanel dummyPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    dummyPanel.add(p);
    add(dummyPanel,BorderLayout.PAGE_START);
}

【讨论】:

  • 感谢您建议的第二种方法对我有用。
猜你喜欢
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
  • 2014-12-01
  • 2011-11-16
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
相关资源
最近更新 更多