0
當在顯式設置組件的位置(使用id.x和id.y約束)並調整添加組件的容器大小時,我在MigLayout中遇到問題。這裏是一個SSCCE:明確設置位置時發生MigLayout問題
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"
被添加到標籤(出現這種情況僅當幀被放大)。我怎麼解決這個問題?
嗯...可能是一個錯誤。好奇:爲什麼不在列約束上使用增長(從而避免推動組件約束)? – kleopatra