如何将图像放入 JInternalFrame 中?
我创建了一个 Swing 应用程序,可以将图像导入到标签中,但我想要做的是将导入的图像放入一个内部框架中,该框架将在图像被删除后创建。
- 将图像放入 Java 应用程序中
- 创建一个
JInternalFrame
- 使图像出现在内部框架中,而不是
JFrame
中的标签
public class Test{
public static void main(String[] args) {
JFrame f = new JFrame("Testing");
JLabel l = new JLabel("Drop here");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.add(l);
l.setHorizontalAlignment(JLabel.CENTER);
l.setTransferHandler(new TransferHandler() {
private static final long serialVersionUID = 1L;
public static final DataFlavor[] SUPPORTED_DATA_FLAVORS = new DataFlavor[]{
DataFlavor.javaFileListFlavor,
DataFlavor.imageFlavor
};
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
System.out.println("1");
for (DataFlavor flavor : SUPPORTED_DATA_FLAVORS) {
if (!support.isDataFlavorSupported(flavor)) {
return true;
}
}
return true;
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
System.out.println("2");
new Test();
JInternalFrame mb = new JInternalFrame("Frame title4", true, true, true);
JLabel la = new JLabel("here");
mb.setSize(400, 300);
l.add(mb);
la.setHorizontalAlignment(JLabel.CENTER);
mb.add(la);
mb.setVisible(true);
if (canImport(support)) {
try {
Transferable t = support.getTransferable();
Component component = support.getComponent();
if (component instanceof JLabel) {
Image image = null;
if (support.isDataFlavorSupported(DataFlavor.imageFlavor)) {
image = (Image) t.getTransferData(DataFlavor.imageFlavor);
} else if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
@SuppressWarnings("rawtypes")
List files = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
if (files.size() > 0) {
image = ImageIO.read((File) files.get(0));
}
}
ImageIcon icon = null;
if (image != null) {
icon = new ImageIcon(image);
}
((JLabel) component).setIcon(icon);
return true;
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
return true;
}
});
}}
I have created a Swing application that can import images into a label, but what I want to do is to put the imported image into an internal frame that will be created after the image is dropped.
- Drop an image into the Java app
- Create a
JInternalFrame
- Make the image appear in the internal frame instead of the label in the
JFrame
public class Test{
public static void main(String[] args) {
JFrame f = new JFrame("Testing");
JLabel l = new JLabel("Drop here");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.add(l);
l.setHorizontalAlignment(JLabel.CENTER);
l.setTransferHandler(new TransferHandler() {
private static final long serialVersionUID = 1L;
public static final DataFlavor[] SUPPORTED_DATA_FLAVORS = new DataFlavor[]{
DataFlavor.javaFileListFlavor,
DataFlavor.imageFlavor
};
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
System.out.println("1");
for (DataFlavor flavor : SUPPORTED_DATA_FLAVORS) {
if (!support.isDataFlavorSupported(flavor)) {
return true;
}
}
return true;
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
System.out.println("2");
new Test();
JInternalFrame mb = new JInternalFrame("Frame title4", true, true, true);
JLabel la = new JLabel("here");
mb.setSize(400, 300);
l.add(mb);
la.setHorizontalAlignment(JLabel.CENTER);
mb.add(la);
mb.setVisible(true);
if (canImport(support)) {
try {
Transferable t = support.getTransferable();
Component component = support.getComponent();
if (component instanceof JLabel) {
Image image = null;
if (support.isDataFlavorSupported(DataFlavor.imageFlavor)) {
image = (Image) t.getTransferData(DataFlavor.imageFlavor);
} else if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
@SuppressWarnings("rawtypes")
List files = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
if (files.size() > 0) {
image = ImageIO.read((File) files.get(0));
}
}
ImageIcon icon = null;
if (image != null) {
icon = new ImageIcon(image);
}
((JLabel) component).setIcon(icon);
return true;
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
return true;
}
});
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将图像拖放到框架、面板、标签、内部框架上实际上都是同一件事。对我来说,我会关注共同点以使事情变得更容易,对我来说,这意味着
JPanel
。JPanel
可以添加到框架、内部框架、另一个面板/容器,并且可以决定如何呈现 drop,在这种情况下,它可以使用JLabel
来呈现一个图像。因此,基于 如何从java目录中拖放文件和Java 拖放特定扩展名的文件在 JFrame 上,
您可以执行类似...
注意: 在 MacOS 上,您可以t 预处理
Transferable
数据。当我使用 Windows 7 时,它确实可以在 Windows 上运行,我无法验证它是否仍然有效。Drop and image on a frame, panel, label, internal frame, is all effectively the same thing. For me, I'd focus on common denominator to make things easier, for me, this means a
JPanel
.A
JPanel
can be added to a frame, internal frame another panel/container and can make decisions about how to present the drop, in this case, it can use aJLabel
to present an image.So, based on how to drag and drop files from a directory in java and Java Drag-n-Drop files of specific extension on JFrame
You could do something like...
nb: On MacOS, you can't pre-process the
Transferable
data. It did work on Windows way back when I was using Windows 7, I can't verify if it still works.