gpt4 book ai didi

java - ImageIcons 数组,通过拖放更改数组内容

转载 作者:行者123 更新时间:2023-12-01 19:07:04 25 4
gpt4 key购买 nike

我有一个 ImageIcons 数组,我可以将其他图标拖放到它们上面来替换它们。当我点击按钮时,会从 ImageIcons 数组创建一个新的 JFrame。

如果我在不将任何其他图标拖到原始数组上的情况下执行此操作,则它可以工作。然而,一旦我将不同的图像图标放入数组中,当我点击按钮时就会出现错误。

我只是想知道这是否可能?

我考虑过使用 JTable 作为顶部面板的其他方法,或尝试使用 ArrayList,但我不太习惯使用它们。如果有人对如何完成此操作有任何意见,请告诉我!

我尽可能缩短了这个示例(200 行并不短)。但这正是我的问题所在......

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Properties;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
import java.lang.String.*;
public class test extends JFrame {

JPanel storyPanel, rightStoryPanel, leftStoryPanel,centerStoryPanel, imageSelectPanel, CreatePanel, storyFramePanel, storycard;
TransferHandler handler;
MouseListener listener;
CardLayout cl3;
JLabel[] storyLabel = new JLabel[20];
JButton playStory, nextStory,addtargetbutton;
int count, start, i, j,stop, start1;

public test(){


CreatePanel = new JPanel(new BorderLayout());
storyPanel = new JPanel(new BorderLayout());
rightStoryPanel = new JPanel(new GridLayout(6,1));
leftStoryPanel = new JPanel(new GridLayout(6,1));
centerStoryPanel = new JPanel();
JScrollPane centerscroll = new JScrollPane(centerStoryPanel);
addtargetbutton = new JButton("Add Another Image Slot");
addtargetbutton.addActionListener(new createbuttons());
playStory = new JButton("Play your story!");
leftStoryPanel.add(playStory);
playStory.addActionListener(new createbuttons());
leftStoryPanel.add(addtargetbutton);
imageSelectPanel = new JPanel(new FlowLayout());
storyPanel.add(rightStoryPanel,BorderLayout.EAST);
storyPanel.add(leftStoryPanel, BorderLayout.WEST);
storyPanel.add(centerscroll, BorderLayout.CENTER);
CreatePanel.add(storyPanel, BorderLayout.NORTH);
CreatePanel.add(imageSelectPanel, BorderLayout.CENTER);


count= 3;
start= 0;
stop = 0;
start1= 0;
ImageSelection();
targetpanel();
getContentPane().add(CreatePanel);

}//End Create}

public void ImageSelection(){



int i = 0;
int j = 0;

TransferHandler handler = new TransferHandler("icon") {
@Override
public boolean canImport(TransferSupport support) {
return super.canImport(support)
&& support.getComponent().getParent() != imageSelectPanel;}
};

MouseListener listener = new MouseAdapter(){
public void mousePressed(MouseEvent e){
JComponent c = (JComponent) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
System.out.println(e);}
}; // Drag & Drop mouse


try{

String imagePath = "";
BufferedImage[] CreateImagesFromDB = new BufferedImage[40];
JLabel[] ImageLabel = new JLabel[40];

while (count > start1) {
i = 1;
CreateImagesFromDB[i] = ImageIO.read(new File("one.jpg"));
ImageLabel[i] = new JLabel(new ImageIcon(CreateImagesFromDB[i]));
imageSelectPanel.add(ImageLabel[i]);
ImageLabel[i].addMouseListener(listener);
ImageLabel[i].setTransferHandler(handler);
i++;
start1++;
}
}//EndTRY
catch(Exception e){
System.out.println("CATCH"+ e);
}//end catch

}


public void targetpanel(){
TransferHandler handler = new TransferHandler("icon") {
@Override
public boolean canImport(TransferSupport support) {
return super.canImport(support)
&& support.getComponent().getParent() != imageSelectPanel;
}
};
MouseListener listener = new MouseAdapter(){
public void mousePressed(MouseEvent e){
JComponent c = (JComponent) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
};

BufferedImage[] storyImages = new BufferedImage[20];


try{

while(count > start){
storyImages[j] = ImageIO.read(new File("TargetImg.jpg"));
storyLabel[j] = new JLabel(new ImageIcon(storyImages[j]));
centerStoryPanel.add(storyLabel[j]);
storyLabel[j].addMouseListener(listener);
storyLabel[j].setTransferHandler(handler);
j++;
start++;
centerStoryPanel.revalidate();
//validate();
System.out.println("J in Loop is: "+j );

}//end while Loop
//System.out.println("J is equalto: "+j);
} catch(Exception e) {};
}// End TargetPanel


public void storyFrame (JLabel[] storyArray){
int i = 0;
storyFramePanel = new JPanel(new BorderLayout());
nextStory = new JButton("Next Image");
nextStory.addActionListener(new createbuttons());
storycard = new JPanel();
cl3 = new CardLayout();
storycard.setLayout(cl3);

JPanel[] storyImgPanel = new JPanel[20];
JLabel[] imglab = new JLabel[20];
storyImgPanel[i]= new JPanel();

while( i < j){

storyImgPanel[i].add(new JLabel(storyArray[i].getIcon()));
storycard.add(storyImgPanel[i], ""+i);
i++;
}




JFrame story = new JFrame("Story");
story.setSize(500,500);

storyFramePanel.add(storycard, BorderLayout.CENTER);
storyFramePanel.add(nextStory, BorderLayout.EAST);
story.add(storyFramePanel);
cl3.show(storycard, "1");
story.setVisible(true);

}

public static void main(String[] args){

System.out.println("Application Running");
JFrame mainframe = new test();
mainframe.setTitle("Let Me Know!");
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setSize(1000,1000);
mainframe.setVisible(true);

}


class createbuttons implements ActionListener{
public void actionPerformed(ActionEvent e)
{

if(e.getSource() == addtargetbutton){
count++;
targetpanel();
System.out.println("Trying to add another TargetImg, count = "+count);
}
if(e.getSource() == playStory){
storyFrame(storyLabel);
}
if(e.getSource() == nextStory){

cl3.next(storycard);
System.out.println("button pressed");
}
}
}

}

最佳答案

我明白了:

首先,每次调用 targetpanel() 时,都会创建一个 storyLabel 的新实例,但随后您的行为就像已经从之前的调用中填充了该实例。所以结果是:

第一次调用:

storyLabel[0] = something;
storyLabel[1] = something;
storyLabel[2] = something;
storyLabel[3] = null;
storyLabel[4] = null.... etc

第二次调用(您添加了另一个图像槽):

storyLabel[0] = null;
storyLabel[1] = null;
storyLabel[2] = null;
storyLabel[3] = something;
storyLabel[4] = null.... etc

因此,当您在 Storyboard中使用此数组时,您会得到 NullPointerException。您只需创建该数组一次。因此,从 targetpanel() 中删除 storyLabel = new JLabel[20] 并在构造函数中初始化数组,甚至在声明中初始化数组更好:

...
CardLayout cl3;
JLabel[] storyLabel = new JLabel[20];
JButton playStory, nextStory, addtargetbutton;
...

其次,当使用 storyFrame() 显示图像时,您更改所提供的 JLabels 的父级,它们随后会从 storyPanel 中消失。您必须为 Storyboard创建 JLabel 的新实例。

storyFrame()中,而不是

storyImgPanel[i].add(storyArray[i]);

storyImgPanel[i].add(new JLabel(storyArray[i].getIcon()));

如果我执行了所有这些操作,程序就可以正常工作。

关于java - ImageIcons 数组,通过拖放更改数组内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657442/

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