【问题标题】:Align to right with GridBagLayout与 GridBagLayout 右对齐
【发布时间】:2012-06-24 13:29:35
【问题描述】:

我正在使用 GridBagLayout 创建一个简单的 GUI,几乎一切正常,但有以下细节:有两列,左侧用于标签,右侧用于 JTextArea 等其他小部件,我希望第一列是右对齐。

所以我尝试将锚属性设置为 GridBagConstraints.NORTHEAST 或 .EAST,但它们都不起作用。

我的代码如下:

private void createDataPanel() {
        data = new JPanel();
        data.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        data.setBorder(new TitledBorder("NOTAM proposal parameters"));

        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(5, 10, 5, 10);
        c.fill = GridBagConstraints.HORIZONTAL;
        data.add(new JLabel("Start time (DD-MM-YYYY HH:MM:SS):"), c);

        c.gridx++;
        long today = System.currentTimeMillis();
        startTimePicker = new JXDatePicker(new Date(today));
        startTimePicker.setFormats(dateFormatter);
        data.add(startTimePicker);

        c.gridx = 0;
        c.gridy++;
        c.anchor = GridBagConstraints.EAST;
        data.add(new JLabel("End time (DD-MM-YYYY HH:MM:SS):"), c);

        c.gridx++;
        c.anchor = GridBagConstraints.CENTER;
        long tomorrow = today + 86400000;
        endTimePicker = new JXDatePicker(new Date(tomorrow));
        endTimePicker.setFormats(dateFormatter);
        data.add(endTimePicker, c);

        c.gridx = 0;
        c.gridy++;
        c.anchor = GridBagConstraints.NORTHEAST;
        JPanel itemEPanel = new JPanel();
        itemEPanel.setLayout(new BoxLayout(itemEPanel, BoxLayout.Y_AXIS)); 
        itemEPanel.add(new JLabel("Item E:"));
        remainingChars = new JLabel(Integer.toString(availableChars) + " characters remaining");
        itemEPanel.add(remainingChars);
        data.add(itemEPanel, c);

        c.gridx++;
        c.anchor = GridBagConstraints.CENTER;
        itemEText = new JTextArea(8, 1);
        JScrollPane itemEScroll = new JScrollPane(itemEText);
        itemEText.setLineWrap(true);
        itemEText.setWrapStyleWord(true);
        itemEText.setBackground(Color.WHITE);
        itemEText.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateRemainingChars(e.getDocument());
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateRemainingChars(e.getDocument());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateRemainingChars(e.getDocument());
            }

            public void updateRemainingChars(Document doc) {
                availableChars = maxItemECharacters - doc.getLength();
                if(availableChars>=0){
                    remainingChars.setText(Integer.toString(availableChars) + " characters remaining");
                    remainingChars.setForeground(Color.BLACK);
                    if(!okButton.isEnabled()) okButton.setEnabled(true);
                }
                else{
                    remainingChars.setText("Message too long: " + Integer.toString(availableChars));
                    remainingChars.setForeground(Color.RED);
                    if(okButton.isEnabled()) okButton.setEnabled(false);
                }
            }
        });
        data.add(itemEScroll, c);

        c.gridx = 0;
        c.gridy++;
        JPanel itemDPanel = new JPanel(new FlowLayout());
        applicable = new JCheckBox("Applicable");
        applicable.setSelected(true);
        applicable.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                itemDText.setEnabled(applicable.isSelected());
            }
        });
        itemDPanel.add(applicable);
        itemDPanel.add(new JLabel("Item D:"));
        data.add(itemDPanel, c);

        c.gridx++;
        itemDText = new JTextField();
        data.add(itemDText, c);
    }

尽管如此,它目前看起来是这样的:

那它怎么能右对齐呢?

【问题讨论】:

    标签: java swing alignment gridbaglayout


    【解决方案1】:

    设置 GridBagConstraints.NORTHEAST 或 .EAST 但使用 fill=NONE

    【讨论】:

      【解决方案2】:

      您的意思是您希望JLabel 的文本位于右侧。如果是,请尝试像这样初始化您的JLabels

      JLabel someLabel = new JLabel("My Text will align towards RIGHT", JLabel.RIGHT);
      

      【讨论】:

        【解决方案3】:
        JLabel.setHorizontalTextPosition(JLabel.RIGHT);
        

        【讨论】:

          【解决方案4】:

          问题是你使用了这个约束:

          c.fill = GridBagConstraints.HORIZONTAL;
          

          因此所有组件都被水平拉伸,将对齐方式留给它们而不是 GridBagLayout。

          【讨论】:

            【解决方案5】:

            在我的情况下,它是

            constraints.anchor = GridBagConstraints.NORTHEAST;
            

            而不是.fill属性

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-04-05
              • 2013-05-16
              • 2014-06-04
              • 2013-01-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多