java – 如何移动JPanel

codeday· 2019-11-11
本文来自 codeday ,作者 codeday
我有一个JDialog,其中包含JPanel和其他元素,如JTextField.我想在将JDialog加载到屏幕后将其从一个位置移动到另一个位置.当我尝试使用jdialog.setLocation()时,我无法移动JDialog,并且添加到其中的所有其他组件也变得不可见.

谁能告诉我我的方法可能有什么问题?

最佳答案
关于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);

   }
}

请注意,必须在调用setVisible(true)之前启动动画Swing Timer.也许这就是吉尔伯特所指的.