且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

摆动图像切换

更新时间:2023-12-04 15:51:04

  1. 要尽快获得更好的帮助,请发布最小,完整和可验证的示例此问题与解答中看到的图像. .请参见下面的示例.
  2. switch方法中声明并添加鼠标侦听器似乎没有任何意义.但是,也许MCVE/SSCCE会清楚地说明这一点.
  3. MAINpanel.repaint();很好.使用标签显示图像!切换时,交换其ImageIcon实例.请参见下面的示例.
  1. For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. See example below.
  2. One way to get image(s) for an example is to hot link to images seen in this Q&A. See example below.
  3. It seems to make no sense to declare and add the mouse listener within the switch method. But maybe an MCVE / SSCCE would make it clear.
  4. MAINpanel.repaint(); Ouch.. Use labels to display the images! On switch, swap their ImageIcon instances. See example below.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.net.*;

public class ImageSwitch {

    private JComponent ui = null;
    JLabel label1 = new JLabel();
    JLabel label2 = new JLabel();
    ImageIcon imageIconA;
    ImageIcon imageIconB;

    ImageSwitch() {
        try {
            initUI();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }

    private void switchImages() {
        if (label1.getIcon().equals(imageIconA)) {
            label1.setIcon(imageIconB);
            label2.setIcon(imageIconA);
        } else {
            label1.setIcon(imageIconA);
            label2.setIcon(imageIconB);
        }
    }

    public void initUI() throws MalformedURLException {
        if (ui!=null) return;

        ui = new JPanel(new GridLayout(0,1,2,2));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        imageIconA = new ImageIcon(new URL("https://i.stack.imgur.com/OVOg3.jpg"));
        imageIconB = new ImageIcon(new URL("https://i.stack.imgur.com/lxthA.jpg"));
        label1.setIcon(imageIconA);
        label2.setIcon(imageIconB);
        ui.add(label1);
        ui.add(label2);

        MouseListener mouseListener = new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                switchImages();
            }
        };
        label1.addMouseListener(mouseListener);
        label2.addMouseListener(mouseListener);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                ImageSwitch o = new ImageSwitch();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}