我一直在爲這個問題苦苦掙扎了一小會兒,我可以使用一般活動偵聽器切換圖片,但無法爲可點擊區域選擇區域以允許開關。我希望創建一個邊界的矩形,使我有一個可點擊的區域來更改圖片。我嘗試過,但我不確定如何設置一個動作監聽器在jpanel的特定區域工作。任何幫助將不勝感激,謝謝!我需要在圖片的範圍內創建可點擊的圖片。然後我需要圖片在文件夾中的圖片進行切換
public class drawPictures {
Random random = new Random();
int one = random.nextInt(1200)+1;
int two = random.nextInt(600)+1;
public static void main(String[] args) {
new drawPictures();
}
public drawPictures() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImageViewPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImageViewPane extends JPanel {
private ImagePane imagePane;
private File[] fileList;
private int currentIndex = -1;
public ImageViewPane() {
fileList = new File("/Di/rec/tory").listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isFile();
}
});
imagePane = new ImagePane();
imagePane.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Rectangle bounds = new Rectangle(one, two, 100, 100);
if (bounds.contains((e).getPoint())){
// Image was clicked...
currentIndex++;
if (currentIndex >= fileList.length) {
currentIndex = 0;
}
nextImage();
}
setLayout(new GridBagLayout());
add(imagePane);
nextImage();
}});}
public void nextImage() {
if (fileList != null && fileList.length > 0) {
currentIndex++;
if (currentIndex < 0 || currentIndex >= fileList.length) {
currentIndex = 0;
}
Image image = null;
/*
Because I don't know the contents of the folder, this is a little
more complicated then it really needs to be.
If you know the file is an image, you can simply use ImageIO.read(file)
*/
while (image == null && currentIndex < fileList.length) {
System.out.println("Loading next image: " + currentIndex);
try {
ImageInputStream iis = ImageIO.createImageInputStream(fileList[currentIndex]);
if (iis != null) {
Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis);
if (imageReaders != null && imageReaders.hasNext()) {
ImageReader imageReader = imageReaders.next();
imageReader.setInput(iis);
image = imageReader.read(0);
} else {
currentIndex++;
}
} else {
currentIndex++;
}
} catch (Exception e) {
e.printStackTrace();
currentIndex++;
}
}
imagePane.setImage(image);
invalidate();
repaint();
}
}
}
public class ImagePane extends JPanel {
private Image image;
private JLabel label;
public ImagePane() {
setLayout(new GridBagLayout());
label = new JLabel("No image available");
add(label);
}
public void setImage(Image value) {``
if (image != value) {
image = value;
label.setVisible(image == null);
repaint();
}
}
@Override
public Dimension getPreferredSize() {
return image == null ? super.getPreferredSize() : new Dimension(image.getWidth(this)+140, image.getHeight(this)+200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
int width = getWidth();
int height = getHeight();
int x = (width - image.getWidth(this))/2 -500 + one;
int y = (height - image.getHeight(this))/2- 300 + two;
g.drawImage(image, x, y, this);
}
}
}
}