简体   繁体   English

Java TransferHandler与Jbuttons一起更新Jbutton文本

[英]Java TransferHandler with Jbuttons to update Jbutton Text

My question is on how do I go about know what the text of the drag and drop location is. 我的问题是我如何知道拖放位置的文本是什么。 This is current working code. 这是当前的工作代码。

     gameCell.addMouseListener(new MouseAdapter() {
         public void mousePressed(MouseEvent e){
             JButton button = (JButton)e.getSource();

             int currentNumber = Integer.parseInt(button.getText());

             TransferHandler handle = button.getTransferHandler();
             handle.exportAsDrag(button, e, TransferHandler.COPY);

So the idea is there is a gameboard which is just a bunch of cells (all JButtons), one large table. 因此,想法是有一个游戏板,它只是一堆单元(所有JButton)和一个大桌子。 When I drag one cell to another then the dragged cell's value will become the clicked cell's value, so therefore how do I tell the original value of the JButton cell before it is copied over by the dragged cell. 当我将一个单元格拖动到另一个单元格时,被拖动的单元格的值将成为被单击的单元格的值,因此,因此,在将JButton单元格的原始值复制到被拖动的单元格之前,如何确定它的原始值。

If you are just trying to "copy" the text from one button to another then you can use code like the following: 如果您只是想将文本从一个按钮“复制”到另一个按钮,则可以使用如下代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DragIcon extends JPanel
{
    public DragIcon()
    {
        TransferHandler iconHandler = new TransferHandler( "icon" );
        MouseListener dragListener = new DragMouseAdapter();

        JLabel label1 = new JLabel("Label1");
        label1.setTransferHandler( iconHandler );
        label1.addMouseListener(dragListener);
        label1.setIcon( new ImageIcon("copy16.gif") );

        JLabel label2 = new JLabel("Label2");
        label2.setTransferHandler( iconHandler );
        label2.addMouseListener(dragListener);

        add( label1 );
        add( label2 );
    }

    private class DragMouseAdapter extends MouseAdapter
    {
        public void mousePressed(MouseEvent e)
        {
            JComponent c = (JComponent)e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
//          handler.exportAsDrag(c, e, TransferHandler.MOVE);
        }
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Drag Icon");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DragIcon());
        frame.setLocationByPlatform( true );
        frame.setSize(200, 100);
        frame.setVisible( true );
    }

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

The default TransferHandler allows you to specify a property that you want to copy. 默认的TransferHandler允许您指定要复制的属性。 In my example I'm copying the Icon. 在我的示例中,我正在复制图标。 In your case you would use: 在您的情况下,您将使用:

TransferHandler iconHandler = new TransferHandler( "text" );

to copy the text. 复制文本。

Note in my example I also tried to "move" the Icon from one label to another but it doesn't work. 请注意,在我的示例中,我还尝试将“图标”从一个标签“移动”到另一个标签,但是它不起作用。 I'm not sure what needs to be changed to move a property. 我不确定要更改属性才能更改什么。

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

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