简体   繁体   English

如何从JFrame中选择图像,然后将其上传到imgur?

[英]How to select a image from a JFrame and then upload it to imgur?

I have done some browsing on the internet and have found this code showing how to display multiple photos in a JFrame as seen below. 我已经在Internet上进行了一些浏览,发现以下代码显示了如何在JFrame中显示多张照片,如下所示。 What I want to be able to do is select a photo and be able to upload it to imgur through clicking a button. 我想要做的是选择一张照片,然后通过单击一个按钮将其上传到imgur。 I know how to upload files to imgur through java but I don't know how to select one image. 我知道如何通过Java将文件上传到imgur,但我不知道如何选择一张图片。 Anyone Have a Solution? 有人有解决方案吗?

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ListView {

public static void main(String[] args) throws IOException {
    String path = "C:/Users/Photos";
    JFrame frame=new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();
    DefaultListModel listModel = new DefaultListModel();
    int count = 0;
    for (int i = 0; i < listOfFiles.length; i++)
    {
        System.out.println("check path"+listOfFiles[i]);
        String name = listOfFiles[i].toString();
        // load only JPEGs
        if ( name.endsWith("png") ) {
            ImageIcon ii = new ImageIcon(ImageIO.read(listOfFiles[i]));
            listModel.add(count++, ii);
        }
    }
    JList lsm=new JList(listModel);
    lsm.setVisibleRowCount(1);

    frame.add(new JScrollPane(lsm));

    frame.pack();
    frame.setVisible(true);
}
}

I don't know how to select one image. 我不知道如何选择一张图像。

  • JList uses an instance of ListSelectionModel to manage its selection. JList使用ListSelectionModel的实例来管理其选择。 By default, a list selection model allows any combination of items to be selected at a time. 默认情况下,列表选择模型允许一次选择项目的任何组合。 You can specify a different selection mode(ie, single item selection ) by calling the setSelectionMode() method on the list. 您可以通过调用列表中的setSelectionMode()方法来指定其他选择模式(即, 单项选择 )。

      jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
  • To do, on item selection event with JList , use the ListSelectionListener to select your item. 为此,在使用JList进行项目选择事件时,请使用ListSelectionListener来选择您的项目。

      jList1.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { JList jlist = (JList) e.getSource(); Object curentSelectedObject = jlist.getModel().getElementAt(e.getFirstIndex()); Object lastSelectedObject = jlist.getModel().getElementAt(e.getLastIndex()); } }); 

Using ListSelectionListener is preferable over using MouseListener with JList . 使用ListSelectionListener优于将MouseListenerJList Unlike MouseListener , being simple and higher level, it has other two functions evt.getFirstIndex() and evt.getLastIndex() which is very useful. MouseListener ,它简单而又高级,它具有其他两个非常有用的函数evt.getFirstIndex()evt.getLastIndex()

Tutorial resource: 教程资源:

  1. How to Write a List Selection Listener 如何编写列表选择监听器

If you need get what was selected in your list, you can use something like this in event handler: 如果需要获取列表中选定的内容,则可以在事件处理程序中使用类似以下的内容:

    ImageIcon selected = (ImageIcon)lsm.getSelectedValue();
    if(selected == null){
         // nothing selected
    }else{
         // something is selected
    }

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

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