0

在显式设置组件的位置(使用 id.x 和 id.y 约束)并调整添加组件的容器大小时,我在 MigLayout 中遇到问题。这是一个SSCCE:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

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

import net.miginfocom.swing.*;

public class MigClass extends JFrame {

    MigClass(String title) {
        super(title);
        MigLayout layout = new MigLayout();
        setLayout(layout);
        final JPanel content = new JPanel(new MigLayout()); 

        final JLabel l1 = new JLabel("A label");
        content.add(l1, "push, center, top, id l1");
        l1.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                final JTextField editor = new JTextField(5);
                content.add(editor, "pos l1.x l1.y");
                editor.setVisible(true);
                l1.setVisible(false);
                editor.addKeyListener(new KeyListener() {
                    @Override
                    public void keyTyped(KeyEvent e) {
                    }

                    @Override
                    public void keyPressed(KeyEvent e) {
                        if(e.getKeyCode() == (KeyEvent.VK_ENTER)) {
                            l1.setText(editor.getText());
                            editor.setVisible(false);
                            l1.setVisible(true);
                        }
                    }

                    @Override
                    public void keyReleased(KeyEvent e) {
                    }                   
                });
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }

        });
        add(content, "grow, push, span");
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        MigClass gui = new MigClass(null);
    }
}

如果将约束添加到标签(仅在放大框架时发生JTextField),则编辑器的位置不正确(它应该与 l1 的位置重叠)。"push"我怎么解决这个问题?

4

1 回答 1

0

文档中没有明确说明,但您不能将相对定位与网格结构混合使用。要么使用相对定位,要么使用正常的网格结构。

无需MigLayout设置两个经理。面板可以简单地添加到 BorderLayout.

我想出了一个利用hidemode约束的解决方案。

package com.zetcode;

import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;


public class MigLayoutReplaceEx extends JFrame {

    public MigLayoutReplaceEx() {

        initUI();

        setSize(300, 250);
        setTitle("Replace component");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private void initUI() {

        JPanel pnl = new JPanel(new MigLayout());

        JLabel lbl = new JLabel("A label");
        pnl.add(lbl, "cell 0 0, hidemode 3");        

        JTextField field = new JTextField(10);
        pnl.add(field, "cell 0 0, hidemode 3");
        field.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == (KeyEvent.VK_ENTER)) {

                    lbl.setText(field.getText());
                    field.setVisible(false);
                    lbl.setVisible(true);
                }
            }
        });

        field.setVisible(false);

        lbl.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                lbl.setVisible(false);
                field.setVisible(true);
            }
        });

        add(pnl);
    }

    public static void main(String[] args) {

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

该示例在窗口上显示了一个标签。单击标签,我们隐藏标签并显示一个文本字段。当我们在字段中按下回车键时,文本字段被隐藏并显示标签,从文本字段中复制新文本。

hidemode 3不可见的组件不参与布局。因此,我们可以在另一个组件始终隐藏的同一单元格中动态显示组件。

于 2014-07-15T21:03:15.470 回答