简体   繁体   English

如何移动JPanel

[英]How to move a JPanel

I have a JDialog which contains JPanel and other elements like JTextField . 我有一个JDialog ,其中包含JPanel和其他元素,如JTextField I want to move JDialog from one location to another after it is loaded on screen. 我想在将JDialog加载到屏幕后将其从一个位置移动到另一个位置。 When I try to use jdialog.setLocation() , I am not able to move JDialog and also all other components added to it becomes invisible. 当我尝试使用jdialog.setLocation() ,我无法移动JDialog并且添加到其中的所有其他组件也变得不可见。

Can anyone tell me what might be wrong with my approach? 谁能告诉我我的方法可能有什么问题?

Regarding Gilbert's assertion that a dialog can't be moved after being set visible, please run this: 关于Gilbert关于在设置可见后无法移动对话框的断言,请运行以下命令:

import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MovingDialog {
   private static void createAndShowGui() {
      JPanel panel = new JPanel();
      panel.add(new JButton(new ShowMovingDialogAction()));
      JFrame frame = new JFrame("MovingDialog");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class ShowMovingDialogAction extends AbstractAction {
   private JPanel panel = new JPanel();

   public ShowMovingDialogAction() {
      super("Show Moving Dialog");
      panel.add(new JLabel("label"));
      panel.add(new JTextField("TextField", 10));
      panel.add(new JButton("Button"));
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      JFrame owner = (JFrame) SwingUtilities.getWindowAncestor((Component) e
            .getSource());
      final JDialog dialog = new JDialog(owner, "Dialog",
            ModalityType.APPLICATION_MODAL);
      dialog.getContentPane().add(panel);
      dialog.pack();
      dialog.setLocation(0, 0);

      int delay = 20;
      new Timer(delay , new ActionListener() {
         int x = 0;
         int y = 0;
         Dimension scrn = Toolkit.getDefaultToolkit().getScreenSize();

         @Override
         public void actionPerformed(ActionEvent e) {
            int maxX = scrn.width - dialog.getWidth();
            int maxY = scrn.height - dialog.getHeight();
            if (x < maxX  && y < maxY) {
               x++;
               y++;
               dialog.setLocation(x, y);
            } else {
               ((Timer)e.getSource()).stop();
            }
         }
      }).start();

      dialog.setVisible(true);

   }
}

Note that the animation Swing Timer must be started before calling setVisible(true). 请注意, 必须在调用setVisible(true)之前启动动画Swing Timer。 Perhaps that is what Gilbert was referring to. 也许这就是吉尔伯特所指的。

If you try to do animation then you will have to initialize and start a new thread and do it there. 如果你尝试做动画,那么你将不得不初始化并开始一个新的线程并在那里做。 The code inside the run() method of the thread should check if the dialog is visible and call dialog.setLocation() with modified values on each iteration. 线程的run()方法内的代码应检查对话框是否可见,并在每次迭代时调用带有修改值的dialog.setLocation()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM