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

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

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

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

在 Java 中,當(dāng)被拖動(dòng)的項(xiàng)目是源代碼控制本身時(shí),執(zhí)行拖放的最佳方式是什么?我知道控件也不過(guò)是數(shù)據(jù),但區(qū)別確實(shí)會(huì)對(duì) UI 產(chǎn)生影響.

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.

我正在創(chuàng)建一個(gè)紙牌風(fēng)格的游戲,其中我有從 JLabel 派生的 Card 類的卡片對(duì)象.我想將該卡拖放到一個(gè)尚未命名的目標(biāo)控件上,從而將其拖到另一個(gè)位置.在拖動(dòng)過(guò)程中,我希望卡片在視覺(jué)上隨鼠標(biāo)移動(dòng),并且在放下時(shí)我希望它移動(dòng)到此目標(biāo)對(duì)象或返回到其先前的位置.

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.

我進(jìn)行了各種 D-n-D 測(cè)試,但沒(méi)有發(fā)現(xiàn)任何符合 Java 的 D-D 規(guī)則的東西.

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 拖動(dòng)卡片對(duì)象,我只能創(chuàng)建卡片的幻影圖像而不是實(shí)體圖像.此外,光標(biāo)發(fā)生了變化,我寧愿它沒(méi)有(我想我可以解決這個(gè)問(wèn)題),并且源代碼控制仍然可見(jiàn)(盡管在拖動(dòng)過(guò)程中應(yīng)該很容易使其透明)

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)

另一方面,我可以通過(guò)監(jiān)聽(tīng) MouseMotionListener.mouseDragged() 事件并手動(dòng)將卡片移動(dòng)到新位置來(lái)漂亮地拖動(dòng)卡片.這很好用,但它沒(méi)有遵循正確的 D-n-D,因?yàn)檫@不會(huì)通知其他控件的拖動(dòng).我想我可以創(chuàng)建自己的系統(tǒng)來(lái)通知其他控件,但這不會(huì)使用 Java 真正的 D-n-D.此外,如果我將真正的 Java d-n-d 內(nèi)容與這種在 mouseDragged 期間從字面上移動(dòng)卡片的方法混合在一起,那么我認(rèn)為真正的 D-n-D 內(nèi)容將永遠(yuǎn)不會(huì)起作用,因?yàn)閺募夹g(shù)上講,鼠標(biāo)永遠(yuǎn)不會(huì)直接位于任何其他控件上,而不是被拖動(dòng)的卡片.這個(gè)方向看起來(lái)就像一個(gè)粗略的黑客攻擊.

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.

我希望這是有道理的.我一直在跟蹤樣本時(shí)遇到問(wèn)題,因?yàn)樗鼈兛雌饋?lái)都非常不同,而我花費(fèi)大量時(shí)間研究的樣本似乎在 D-n-D 在 1.4 版中進(jìn)行大修之前幾年就過(guò)時(shí)了.

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.

推薦答案

在單個(gè)應(yīng)用程序周圍而不是在應(yīng)用程序之間拖動(dòng)組件的一種方法是使用 JLayeredPane.例如,請(qǐng)?jiān)诖颂幉榭次业拇a:在屏幕上拖動(dòng) 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;
   }
}

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

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

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時(shí)間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
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 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 日日操av| 亚洲经典一区 | 国产精品一区二区三区四区五区 | 成人视屏在线观看 | 久久一区二区av | 自拍偷拍中文字幕 | 欧美精品久久久久久久久老牛影院 | 天天干天天操天天射 | 91视频在线看 | 中文字幕第十页 | 中文字幕国产精品 | 久久精品亚洲一区二区三区浴池 | 麻豆精品久久久 | 久久久一区二区三区四区 | 国产精品自产av一区二区三区 | 99这里只有精品视频 | 日韩午夜精品 | 亚洲精品一区二区三区四区高清 | 一区二区三区免费 | 久久亚洲天堂 | 一区二区三区精品视频 | 97国产爽爽爽久久久 | 国内精品视频免费观看 | 国产精品久久久久久52avav | 欧美日韩一区在线 | 精品成人在线视频 | 中文字幕在线一区二区三区 | 正在播放国产精品 | 一区二区影视 | 欧美精品乱码99久久影院 | 亚洲精品视频一区二区三区 | 亚洲超碰在线观看 | 国产情侣激情 | 国产精品一区二区三区在线 | 精品久久久一区 | 亚洲成人999 | 中文在线www | 免费激情av | 福利一区视频 | 国产日韩精品一区 | 久久精品一区 |