gpt4 book ai didi

java - 将图像从浏览器拖放到 JLabel 上

转载 作者:行者123 更新时间:2023-11-30 07:29:03 26 4
gpt4 key购买 nike

我希望能够在 JLabel 上设置图像,例如从网络浏览器。我的实现基于 Drag-and-Drop Support for Images .

@Override
public boolean importData(JComponent comp, Transferable t) {
if (comp instanceof JLabel) {
JLabel label = (JLabel) comp;
if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
try {
image = (Image) t.getTransferData(DataFlavor.imageFlavor);
ImageIcon icon = new ImageIcon(image);
label.setIcon(icon);
return true;
} catch (Throwable th) {
log.error("Failed to accept dropped image", th);
}
} else if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
try {
@SuppressWarnings("unchecked")
List<File> files = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
if (files.size() == 1) {
ImageIcon icon = new ImageIcon(files.get(0).getAbsolutePath());
label.setIcon(icon);
}
return true;
} catch (Throwable th) {
log.error("Failed to accept dropped image", th);
}
}
}
return false;
}

对于 DataFlavor.javaFileListFlavor 部分,一切正常,但对于 DataFlavor.imageFlavour,生成的图像始终为空,并且在实例化 ImageIcon 时抛出 NullPointerException。

有人知道为什么会这样吗?

最佳答案

您的代码似乎没问题。我使用以下代码对其进行了测试,将图像从我的网络浏览器 (Google Chrome) 拖放到 JPanel 上会在 JLabel 中设置图像。

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.TransferHandler;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class DragAndDropTester {
private static final JLabel TEST_LABEL = new JLabel( "TestLabel" );

public static void main( String[] args ) {
try {
EventQueue.invokeAndWait( new Runnable() {
public void run() {

JFrame testFrame = new JFrame( "Test" );

JPanel contents = new JPanel( new BorderLayout() );
contents.add( TEST_LABEL, BorderLayout.CENTER );

contents.setTransferHandler( createTransferHandler() );

testFrame.getContentPane().add( contents );
testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
testFrame.setSize( 200, 200 );
testFrame.setVisible( true );
}
} );
} catch ( InterruptedException e ) {
} catch ( InvocationTargetException e ) {
}
}

private static TransferHandler createTransferHandler(){
return new TransferHandler( ){
@Override
public boolean importData( JComponent comp, Transferable aTransferable ) {
try {
Object transferData = aTransferable.getTransferData( DataFlavor.imageFlavor );
TEST_LABEL.setIcon( new ImageIcon( ( Image ) transferData ) );
} catch ( UnsupportedFlavorException e ) {
} catch ( IOException e ) {
}
return true;
}

@Override
public boolean canImport( JComponent comp, DataFlavor[] transferFlavors ) {
return true;
}
};
}
}

这是在我的 Mac 上使用 JDK1.6 测试的

java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-10M3527)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02-402, mixed mode)

关于java - 将图像从浏览器拖放到 JLabel 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8769336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com