本文介绍了摆动图像切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的任务是在捕获鼠标单击后切换两个图像,这意味着当任何用户单击两个图像时,两个图像都应切换.但是以某种方式在我的代码中无法检测到鼠标点击,
My task is to switch two images after capturing mouse click, means when any user clicks on two images both should switch.but somehow in my code not able to detect the mouse click,
imageAnimal = createImageIcon("Lion", "Lion");
Image scale = imageAnimal.getImage().getScaledInstance(200,200,Image.SCALE_SMOOTH);
imageAnimal = new ImageIcon(scale);
image1Label = new JLabel("", imageAnimal, JLabel.CENTER);
imageMot = createImageIcon("car", "car");
Image scale = imageMot.getImage().getScaledInstance(200,200,Image.SCALE_SMOOTH);
imageMot = new ImageIcon(scale);
image1Label = new JLabel("", imageMot, JLabel.CENTER);
-捕获鼠标事件的代码
public void switch() {
abstract class MouseListener implements ActionListener {
public void actionPerformed(MouseEvent event){
boolean clicked = false;
JPanel imageClicked1;
JPanel imageClicked2 = (JPanel) event.getSource();
int numClicks = 0;
for(int i = 0; i < temp.size(); i++)
{
if(clicked)
{
numClicks++;
imageClicked1 = (JPanel) event.getSource();
if(numClicks == 2)
{
switchImages(imageClicked1, imageClicked2);
}
}
MAINpanel.repaint();
MAINpanel.revalidate();
}
}
public void switchImages(JPanel img1, JPanel img2)
{
//ArrayList<JPanel>sorted = new ArrayList<JPanel>();
JPanel t;
JPanel posValue, nextValue;
for(int i = 0; i < temp.size(); i++)
{
for(int k = 1; k < temp.size(); k++)
{
if(temp.get(i) == img1 && temp.get(k) == img2)
{
posValue = temp.get(k);
nextValue = temp.get(i);
t = temp.get(k);
posValue = temp.get(i);
nextValue = t;
}
}
}
for(int i = 0; i < 5; i++)
{
mainPanel.add(temp.get(i), BorderLayout.CENTER);
}
}
}
}
推荐答案
- 要尽快获得更好的帮助,请发布最小,完整和可验证的示例或简短,自包含的正确示例.请参见下面的示例.
- 例如,获取图像的一种方法是热链接到在此问题与解答中看到的图像. .请参见下面的示例.
- 在
switch
方法中声明并添加鼠标侦听器似乎没有任何意义.但是,也许MCVE/SSCCE会清楚地说明这一点. -
MAINpanel.repaint();
很好.使用标签显示图像!切换时,交换其ImageIcon
实例.请参见下面的示例.
- For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. See example below.
- One way to get image(s) for an example is to hot link to images seen in this Q&A. See example below.
- 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. MAINpanel.repaint();
Ouch.. Use labels to display the images! On switch, swap theirImageIcon
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);
}
}
这篇关于摆动图像切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!