標題:
滑鼠事件 (三)
[打印本頁]
作者:
tonyh
時間:
2015-10-3 14:48
標題:
滑鼠事件 (三)
本帖最後由 tonyh 於 2016-6-16 16:36 編輯
試定義四個內部類別 (InnerClass) 來實作 MouseListener 介面, 再分別將它們註冊給視窗中的四個標籤, 使能各別點擊它們來置換圖案. 執行狀況如下圖所示:
JFrame的設定: 100, 100, 420, 320
JLabel的設定:
50, 20, 128, 128
250, 20, 128, 128
50, 160, 128, 128
250, 160, 128, 128
[attach]1419[/attach]
[attach]1420[/attach]
[attach]1421[/attach]
[attach]1422[/attach]
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Ch89{
private JFrame fm;
private JLabel lb1,lb2,lb3,lb4;
private ImageIcon icon1,icon2,icon3,icon4,icon5,icon6,icon7,icon8,icon9;
Ch89()
{
icon1=new ImageIcon(Ch89.class.getResource("pic/star.png"));
icon2=new ImageIcon(Ch89.class.getResource("pic/gift-green.png"));
icon3=new ImageIcon(Ch89.class.getResource("pic/hippo.png"));
icon4=new ImageIcon(Ch89.class.getResource("pic/gift-blue.png"));
icon5=new ImageIcon(Ch89.class.getResource("pic/monkey.png"));
icon6=new ImageIcon(Ch89.class.getResource("pic/gift-purple.png"));
icon7=new ImageIcon(Ch89.class.getResource("pic/cat.png"));
icon8=new ImageIcon(Ch89.class.getResource("pic/gift-white.png"));
icon9=new ImageIcon(Ch89.class.getResource("pic/panda.png"));
lb1=new JLabel(icon2);
lb1.setBounds(50, 20, 128, 128);
lb1.addMouseListener(new Try1());
lb2=new JLabel(icon4);
lb2.setBounds(250, 20, 128, 128);
lb2.addMouseListener(new Try2());
lb3=new JLabel(icon6);
lb3.setBounds(50, 160, 128, 128);
lb3.addMouseListener(new Try3());
lb4=new JLabel(icon8);
lb4.setBounds(250, 160, 128, 128);
lb4.addMouseListener(new Try4());
fm=new JFrame("點按滑鼠切換圖形");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(icon1.getImage());
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb1);
fm.add(lb2);
fm.add(lb3);
fm.add(lb4);
}
class Try1 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb1.getIcon()==icon2)
lb1.setIcon(icon3);
else
lb1.setIcon(icon2);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try2 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb2.getIcon()==icon4)
lb2.setIcon(icon5);
else
lb2.setIcon(icon4);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try3 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb3.getIcon()==icon6)
lb3.setIcon(icon7);
else
lb3.setIcon(icon6);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try4 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb4.getIcon()==icon8)
lb4.setIcon(icon9);
else
lb4.setIcon(icon8);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
public static void main(String[] args) {
Ch89 app=new Ch89();
}
}
複製代碼
作者:
許逸群
時間:
2015-10-17 14:27
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Ch89{
private JFrame fm;
private JLabel lb1,lb2,lb3,lb4;
private ImageIcon icon1,icon2,icon3,icon4,icon5,icon6,icon7,icon8,icon9;
Ch89()
{
icon1=new ImageIcon(Ch89.class.getResource("pic/star.png"));
icon2=new ImageIcon(Ch89.class.getResource("pic/gift-green.png"));
icon3=new ImageIcon(Ch89.class.getResource("pic/hippo.png"));
icon4=new ImageIcon(Ch89.class.getResource("pic/gift-blue.png"));
icon5=new ImageIcon(Ch89.class.getResource("pic/monkey.png"));
icon6=new ImageIcon(Ch89.class.getResource("pic/gift-purple.png"));
icon7=new ImageIcon(Ch89.class.getResource("pic/cat.png"));
icon8=new ImageIcon(Ch89.class.getResource("pic/gift-white.png"));
icon9=new ImageIcon(Ch89.class.getResource("pic/panda.png"));
lb1=new JLabel(icon2);
lb1.setBounds(50, 20, 128, 128);
lb1.addMouseListener(new Try1());
lb2=new JLabel(icon4);
lb2.setBounds(250, 20, 128, 128);
lb2.addMouseListener(new Try2());
lb3=new JLabel(icon6);
lb3.setBounds(50, 160, 128, 128);
lb3.addMouseListener(new Try3());
lb4=new JLabel(icon8);
lb4.setBounds(250, 160, 128, 128);
lb4.addMouseListener(new Try4());
fm=new JFrame("點按滑鼠切換圖形");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(icon1.getImage());
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb1);
fm.add(lb2);
fm.add(lb3);
fm.add(lb4);
}
class Try1 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb1.getIcon()==icon2)
lb1.setIcon(icon3);
else
lb1.setIcon(icon2);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try2 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb2.getIcon()==icon4)
lb2.setIcon(icon5);
else
lb2.setIcon(icon4);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try3 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb3.getIcon()==icon6)
lb3.setIcon(icon7);
else
lb3.setIcon(icon6);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try4 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb4.getIcon()==icon8)
lb4.setIcon(icon9);
else
lb4.setIcon(icon8);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
public static void main(String[] args) {
Ch89 app=new Ch89();
}
}
複製代碼
作者:
劉泳鱔
時間:
2015-10-17 14:46
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Ch89
{
private JFrame fm;
private JLabel lb1,lb2,lb3,lb4;
private ImageIcon icon1,icon2,icon3,icon4,icon5,icon6,icon7,icon8,icon9;
Ch89()
{
icon1=new ImageIcon(Ch89.class.getResource("pic/star.png"));
icon2=new ImageIcon(Ch89.class.getResource("pic/gift-green.png"));
icon3=new ImageIcon(Ch89.class.getResource("pic/hippo.png"));
icon4=new ImageIcon(Ch89.class.getResource("pic/gift-blue.png"));
icon5=new ImageIcon(Ch89.class.getResource("pic/monkey.png"));
icon6=new ImageIcon(Ch89.class.getResource("pic/gift-purple.png"));
icon7=new ImageIcon(Ch89.class.getResource("pic/cat.png"));
icon8=new ImageIcon(Ch89.class.getResource("pic/gift-white.png"));
icon9=new ImageIcon(Ch89.class.getResource("pic/panda.png"));
lb1=new JLabel(icon2);
lb1.setBounds(50, 20, 128, 128);
lb1.addMouseListener(new Try1());
lb2=new JLabel(icon4);
lb2.setBounds(250, 20, 128, 128);
lb2.addMouseListener(new Try2());
lb3=new JLabel(icon6);
lb3.setBounds(50, 160, 128, 128);
lb3.addMouseListener(new Try3());
lb4=new JLabel(icon8);
lb4.setBounds(250, 160, 128, 128);
lb4.addMouseListener(new Try4());
fm=new JFrame("點按滑鼠切換圖形");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(icon1.getImage());
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb1);
fm.add(lb2);
fm.add(lb3);
fm.add(lb4);
}
class Try1 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb1.getIcon()==icon2)
lb1.setIcon(icon3);
else
lb1.setIcon(icon2);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try2 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb2.getIcon()==icon4)
lb2.setIcon(icon5);
else
lb2.setIcon(icon4);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try3 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb3.getIcon()==icon6)
lb3.setIcon(icon7);
else
lb3.setIcon(icon6);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try4 implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(lb4.getIcon()==icon8)
lb4.setIcon(icon9);
else
lb4.setIcon(icon8);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
public static void main(String[] args)
{
Ch89 app=new Ch89();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2