在显式设置组件的位置(使用 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"
我怎么解决这个问题?