可拖动的JPanel在编辑时总是跳回中心位置。

huangapple 未分类评论47阅读模式
英文:

dragable JPanel keeps jumping back to center when edited

问题

  1. 我有一个带有文本字段的JPaneltextPanel)。出于某些原因我将textPanel添加到另一个面板contentPanel),然后再添加到JFrame我已经为textPanel添加了一个MouseListener以便我可以拖动它新位置是通过方法调用setLocation...来设置的
  2. 每当我开始编辑文本字段时文本面板就会从其实际位置跳回到JFrame的中心- 为什么我需要更改什么
  3. public void run() {
  4. area1 = new JTextArea();
  5. area1.setText("Hello!");
  6. GridBagLayout layout = new GridBagLayout();
  7. GridBagConstraints cc = new GridBagConstraints();
  8. cc.fill = GridBagConstraints.NONE;
  9. textPanel = new JPanel(layout);
  10. cc.anchor = GridBagConstraints.PAGE_START;
  11. cc.gridx = 0;
  12. textPanel.add(area1, cc);
  13. JPanel contentPanel = new JPanel(new GridBagLayout());
  14. cc = new GridBagConstraints();
  15. cc.anchor = GridBagConstraints.FIRST_LINE_START;
  16. cc.gridx = 0;
  17. cc.gridy = 0;
  18. cc.weightx = 1;
  19. cc.weighty = 1;
  20. MyMouseAdapter mh = new MyMouseAdapter();
  21. textPanel.addMouseListener(mh);
  22. textPanel.addMouseMotionListener(mh);
  23. contentPanel.add(textPanel, cc);
  24. JFrame frame = new JFrame();
  25. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  26. frame.add(contentPanel);
  27. frame.setSize(800, 600);
  28. frame.setVisible(true);
  29. }
英文:

I have a JPanel with a textfield (textPanel). I added the textPanel for some reasons to another panel (contentPanel) and that to the JFrame. I have added a MouseListener to the textPanel, so that I can drag it around. The new Position is set by the methodcall setLocation (...)
Everytime I start editing the textfield, the textpanel jumps back from its actual location to the center of the JFrame. – Why? What do I have to change?

  1. public void run() {
  2. area1 = new JTextArea();
  3. area1.setText("Hello!");
  4. GridBagLayout layout = new GridBagLayout();
  5. GridBagConstraints cc = new GridBagConstraints();
  6. cc.fill = GridBagConstraints.NONE;
  7. textPanel = new JPanel(layout);
  8. cc.anchor = GridBagConstraints.PAGE_START;
  9. cc.gridx = 0;
  10. textPanel.add(area1, cc);
  11. JPanel contentPanel = new JPanel(new GridBagLayout());
  12. cc = new GridBagConstraints();
  13. cc.anchor = GridBagConstraints.FIRST_LINE_START;
  14. cc.gridx = 0;
  15. cc.gridy = 0;
  16. cc.weightx = 1;
  17. cc.weighty = 1;
  18. MyMouseAdapter mh = new MyMouseAdapter();
  19. textPanel.addMouseListener(mh);
  20. textPanel.addMouseMotionListener(mh);
  21. contentPanel.add(textPanel, cc);
  22. JFrame frame = new JFrame();
  23. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  24. frame.add(contentPanel);
  25. frame.setSize(800, 600);
  26. frame.setVisible(true);
  27. }

huangapple
  • 本文由 发表于 2020年4月6日 15:58:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/61055311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定