简体   繁体   中英

Create a clickable image with Swing

I'm trying to create an image browser in Swing that will allow a user to select an image, and then click the right half of the image to advance to the next one, and click the left half to move to the previous one.

I tried to do this with MouseListeners, but MouseClicked would not register unless I used pack(); , but that caused my image to disappear unless I dragged the UI out.

This is the code that was used to display the image:

class ImageFrame extends JFrame
{
    private final JFileChooser chooser;
    private BufferedImage image = null;
    int WIDTH = 1080; int HEIGHT = 620;

    // ==========================================
    // constructor

    public ImageFrame (int width, int height)
    {
        // --------------------------------------
        // setup the frame's attributes
        this.setTitle("Image-P3 Player");
        this.setSize( width,height );

        // add a menu to the frame
        addMenu();

        // --------------------------------------
        // setup the file chooser dialog

        chooser = new JFileChooser();
        chooser.setCurrentDirectory( new File( ".") );
    }
    addMouseListener( new MouseAdapter()
        {
        public void mouseClicked( MouseEvent e){
            System.out.println("Mouse clicked!");
            if(e.getButton() == 1){
                int x = e.getX();
                System.out.println("X: "+x);
                if(x>540){
                    System.out.println("Next image!");
                }
                else{
                    System.out.println("Previous image!");
                }
            }
        }
    } );

Then there was a JMenu that led to this series of methods:

private void open()
{
    File file = getFile();
    if( file != null)
    {
        displayFile( file );
    }
}

private File getFile()
{
    File file = null;

    if ( chooser.showOpenDialog( this ) == JFileChooser.APPROVE_OPTION )
    {
        file = chooser.getSelectedFile();
    }

    return file;
}

private void displayFile( File file )
{
//some code to resize the image to the JFrame's dimensions
}

// ------------------------------------------
// Display Buffered Image

public void displayBufferedImage( ImageIcon image )
{
    this.setContentPane( new JScrollPane( new JLabel( image  ) ) );

    this.validate();
}

Is there a simple way to add the MouseListener to the JScrollPane or JLabel so that the image displayed in the GUI will register user clicks without throwing the components out of whack?

Thanks MadProgrammer, that did the trick. I initialized and instantiated JLabel with other variables at the top:

JLabel label = new JLabel();

And then used .setIcon(ImageIcon icon) on the JLabel in my displayBufferedImage method:

public void displayBufferedImage( ImageIcon image )
{
    label.setIcon(image);
    this.setContentPane( new JScrollPane( label ) );

    this.validate();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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