久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java:如何將控件而不是其數據拖放到新位置?

Java: How do I drag and drop a control to a new location instead of its data?(Java:如何將控件而不是其數據拖放到新位置?)
本文介紹了Java:如何將控件而不是其數據拖放到新位置?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在 Java 中,當被拖動的項目是源代碼控制本身時,執行拖放的最佳方式是什么?我知道控件也不過是數據,但區別確實會對 UI 產生影響.

In Java, what is the best way to perform drag and drop when the item being dragged is the source control itself? I know a control is nothing but data too, but the difference does have UI impacts.

我正在創建一個紙牌風格的游戲,其中我有從 JLabel 派生的 Card 類的卡片對象.我想將該卡拖放到一個尚未命名的目標控件上,從而將其拖到另一個位置.在拖動過程中,我希望卡片在視覺上隨鼠標移動,并且在放下時我希望它移動到此目標對象或返回到其先前的位置.

I'm creating a solitaire-style game where I have card objects of class Card derived from JLabel. I want to drag that card to another location by dropping it onto a yet-to-be named Destination control. During the drag, I want the card to visually move with the mouse and when dropped I want it to either move to this destination object or return to its previous location.

我進行了各種 D-n-D 測試,但沒有發現任何符合 Java 的 D-D 規則的東西.

I've done various D-n-D tests and haven't found anything that works under the proper rules of Java's D-D.

例如,如果我使用真正的 D-n-D 拖動卡片對象,我只能創建卡片的幻影圖像而不是實體圖像.此外,光標發生了變化,我寧愿它沒有(我想我可以解決這個問題),并且源代碼控制仍然可見(盡管在拖動過程中應該很容易使其透明)

For example, if I drag the Card object using true D-n-D I can only create a ghosted image of the card and not a solid image. Also, the cursor changes and I'd rather it did not (I think I can fix that), and the source control remains visible (though it should be easy to make it transparent during the drag)

另一方面,我可以通過監聽 MouseMotionListener.mouseDragged() 事件并手動將卡片移動到新位置來漂亮地拖動卡片.這很好用,但它沒有遵循正確的 D-n-D,因為這不會通知其他控件的拖動.我想我可以創建自己的系統來通知其他控件,但這不會使用 Java 真正的 D-n-D.此外,如果我將真正的 Java d-n-d 內容與這種在 mouseDragged 期間從字面上移動卡片的方法混合在一起,那么我認為真正的 D-n-D 內容將永遠不會起作用,因為從技術上講,鼠標永遠不會直接位于任何其他控件上,而不是被拖動的卡片.這個方向看起來就像一個粗略的黑客攻擊.

On the other hand, I can drag the Card beautifully by listening for MouseMotionListener.mouseDragged() events and manually moving the Card to the new location. This works great, but it is not following proper D-n-D because this will not inform other controls of the drag. I figured I could either create my own system to notify the other controls, but that would not be using Java's real D-n-D. Also, if I mix the real Java d-n-d stuff with this method of literally moving the Card during mouseDragged then I assume the real D-n-D stuff will never work because the mouse will never technically be directly over any other control than the card being dragged. This direction just seems like a crude hack.

我希望這是有道理的.我一直在跟蹤樣本時遇到問題,因為它們看起來都非常不同,而我花費大量時間研究的樣本似乎在 D-n-D 在 1.4 版中進行大修之前幾年就過時了.

I hope this makes sense. I've been having problems following samples because they all seem very different, and one that I spent a great deal of time studying looks to be dated a couple years before D-n-D had its major overhaul in version 1.4.

推薦答案

在單個應用程序周圍而不是在應用程序之間拖動組件的一種方法是使用 JLayeredPane.例如,請在此處查看我的代碼:在屏幕上拖動 jlabel

One way to drag a component around a single application and not between applications is to use a JLayeredPane. For example please see my code here: dragging a jlabel around the screen

撲克牌示例可能如下所示(只要撲克牌圖像保持有效!):

An example with playing cards could look like this (as long as the playing card image remains valid!):

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.*;

public class PlayingCardTest {


   public static void main(String[] args) {
      String pathToDeck = "http://www.jfitz.com/cards/classic-playing-cards.png";
      try {
         final List<ImageIcon> cardImgList = CreateCards.createCardIconList(pathToDeck);
         SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               JFrame frame = new JFrame("Moving Cards");
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.add(new CardGameTable(cardImgList, frame));
               frame.pack();
               frame.setLocationRelativeTo(null);
               frame.setVisible(true);
            }
         });
      } catch (MalformedURLException e) {
         e.printStackTrace();
         System.exit(-1);
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }
   }
}

@SuppressWarnings("serial")
class CardGameTable extends JLayeredPane {

   private static final int PREF_W = 600;
   private static final int PREF_H = 400;
   private static final Color BASE_COLOR = new Color(0, 80, 0);
   private static final int CARD_COUNT = 20;
   private static final int WIDTH_SHOWING = 20;

   private JPanel basePane = new JPanel(null);

   public CardGameTable(List<ImageIcon> cardImgList, final JFrame frame) {
      basePane.setSize(getPreferredSize());
      basePane.setBackground(BASE_COLOR);
      add(basePane, JLayeredPane.DEFAULT_LAYER);

      final MyMouseAdapter myMouseAdapter = new MyMouseAdapter(this, basePane);
      addMouseListener(myMouseAdapter);
      addMouseMotionListener(myMouseAdapter);

      for (int i = 0; i < CARD_COUNT; i++) {
         JLabel card = new JLabel(cardImgList.remove(0));
         card.setSize(card.getPreferredSize());
         int x = (PREF_W / 2) + WIDTH_SHOWING * (CARD_COUNT - 2 * i) / 2 - 
               card.getPreferredSize().width / 2;
         int y = PREF_H - card.getPreferredSize().height - WIDTH_SHOWING * 2;
         card.setLocation(x, y);
         basePane.add(card);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

}

class MyMouseAdapter extends MouseAdapter {
   private JLabel selectedCard = null;
   private JLayeredPane cardGameTable = null;
   private JPanel basePane = null;
   private int deltaX = 0;
   private int deltaY = 0;

   public MyMouseAdapter(JLayeredPane gameTable, JPanel basePane) {
      this.cardGameTable = gameTable;
      this.basePane = basePane;
   }

   @Override
   public void mousePressed(MouseEvent mEvt) {
      Component comp = basePane.getComponentAt(mEvt.getPoint());
      if (comp != null && comp instanceof JLabel) {
         selectedCard = (JLabel) comp;
         basePane.remove(selectedCard);
         basePane.revalidate();
         basePane.repaint();

         cardGameTable.add(selectedCard, JLayeredPane.DRAG_LAYER);
         cardGameTable.revalidate();
         cardGameTable.repaint();
         deltaX = mEvt.getX() - selectedCard.getX();
         deltaY = mEvt.getY() - selectedCard.getY();
      }
   }

   @Override
   public void mouseReleased(MouseEvent mEvt) {
      if (selectedCard != null) {
         cardGameTable.remove(selectedCard);
         cardGameTable.revalidate();
         cardGameTable.repaint();

         basePane.add(selectedCard, 0);
         basePane.revalidate();
         basePane.repaint();
         selectedCard = null;
      }
   }

   @Override
   public void mouseDragged(MouseEvent mEvt) {
      if (selectedCard != null) {
         int x = mEvt.getX() - deltaX;
         int y = mEvt.getY() - deltaY;
         selectedCard.setLocation(x, y);
         cardGameTable.revalidate();
         cardGameTable.repaint();
      }
   }
}

class CreateCards {
   private static final int SUIT_COUNT = 4;
   private static final int RANK_COUNT = 13;

   public static List<ImageIcon> createCardIconList(String pathToDeck)
         throws MalformedURLException, IOException {
      BufferedImage fullDeckImg = ImageIO.read(new URL(pathToDeck));
      int width = fullDeckImg.getWidth();
      int height = fullDeckImg.getHeight();
      List<ImageIcon> iconList = new ArrayList<ImageIcon>();

      for (int suit = 0; suit < SUIT_COUNT; suit++) {
         for (int rank = 0; rank < RANK_COUNT; rank++) {
            int x = (rank * width) / RANK_COUNT;
            int y = (suit * height) / SUIT_COUNT;
            int w = width / RANK_COUNT;
            int h = height / SUIT_COUNT;
            BufferedImage cardImg = fullDeckImg.getSubimage(x, y, w, h);
            iconList.add(new ImageIcon(cardImg));
         }
      }
      Collections.shuffle(iconList);
      return iconList;
   }
}

這篇關于Java:如何將控件而不是其數據拖放到新位置?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 天天射中文 | 亚洲一区国产精品 | 日本午夜网站 | 999国产精品视频 | 久久精品国产久精国产 | 羞羞的视频免费观看 | 亚洲精品18 | 免费视频久久 | av电影一区二区 | 日批的视频 | 国产免费一区二区三区 | 夜夜艹天天干 | 日韩日韩日韩日韩日韩日韩日韩 | 日本一区二区不卡 | 91在线一区二区三区 | 中文福利视频 | 久久亚洲精品国产精品紫薇 | 成人免费视频一区二区 | 国产精品视频观看 | 欧美一级在线 | 狠狠躁天天躁夜夜躁婷婷老牛影视 | www.中文字幕.com | 国产免费一区二区三区 | 国产日韩欧美在线 | 国产一级在线 | 在线欧美亚洲 | 操皮视频 | 亚洲一区国产精品 | 免费毛片网 | 久久精品一区二区 | 亚洲成人精品一区 | 亚洲天堂久久 | 国产精品一区二区免费 | 91网站在线看 | 亚洲精品99 | 亚洲 成人 av| 日本不卡一区二区三区 | 成人免费一级视频 | 久久国产亚洲精品 | 国产第一页在线播放 | 久久国产精品免费一区二区三区 |