標題:
滑鼠事件 (四)
[打印本頁]
作者:
tonyh
時間:
2016-6-18 17:19
標題:
滑鼠事件 (四)
本帖最後由 tonyh 於 2019-6-22 12:58 編輯
試分別使用「成員內部類別」與「匿名內部類別」,實作「MouseListener介面」或繼承「MouseAdapter類別」,完成如下的視窗程式,視窗上的圖案點擊便能置換,且彼此不會相互干擾。
JFrame的設定: 100, 100, 420, 320
JLabel的設定:
50, 20, 128, 128
250, 20, 128, 128
50, 160, 128, 128
250, 160, 128, 128
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Ch101{
private JFrame fm;
private JLabel lb1,lb2,lb3,lb4;
private ImageIcon ii,ii1,ii2,ii3,ii4,iig1,iig2,iig3,iig4;
private Try1 try1=new Try1();
Ch101()
{
ii=new ImageIcon(Ch101.class.getResource("pic/star.png"));
ii1=new ImageIcon(Ch101.class.getResource("pic/1.png"));
ii2=new ImageIcon(Ch101.class.getResource("pic/2.png"));
ii3=new ImageIcon(Ch101.class.getResource("pic/3.png"));
ii4=new ImageIcon(Ch101.class.getResource("pic/4.png"));
iig1=new ImageIcon(Ch101.class.getResource("pic/g1.png"));
iig2=new ImageIcon(Ch101.class.getResource("pic/g2.png"));
iig3=new ImageIcon(Ch101.class.getResource("pic/g3.png"));
iig4=new ImageIcon(Ch101.class.getResource("pic/g4.png"));
lb1=new JLabel(iig1);
lb1.setBounds(50, 20, 128, 128);
lb1.addMouseListener(try1);
lb2=new JLabel(iig2);
lb2.setBounds(250, 20, 128, 128);
lb2.addMouseListener(new Try2());
lb3=new JLabel(iig3);
lb3.setBounds(50, 160, 128, 128);
lb3.addMouseListener(new MouseListener(){ //匿名式
public void mousePressed(MouseEvent e)
{
if(lb3.getIcon()==iig3)
lb3.setIcon(ii3);
else
lb3.setIcon(iig3);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
});
lb4=new JLabel(iig4);
lb4.setBounds(250, 160, 128, 128);
lb4.addMouseListener(new MouseAdapter(){ //匿名式
public void mousePressed(MouseEvent e)
{
if(lb4.getIcon()==iig4)
lb4.setIcon(ii4);
else
lb4.setIcon(iig4);
}
});
fm=new JFrame("滑鼠指標拖曳圖形");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(ii.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()==iig1)
lb1.setIcon(ii1);
else
lb1.setIcon(iig1);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try2 extends MouseAdapter //成員式
{
public void mousePressed(MouseEvent e)
{
if(lb2.getIcon()==iig2)
lb2.setIcon(ii2);
else
lb2.setIcon(iig2);
}
}
public static void main(String[] args) {
new Ch101();
}
}
複製代碼
作者:
張峻瑋
時間:
2016-6-18 17:44
本帖最後由 張峻瑋 於 2016-6-18 20:22 編輯
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Ch101{
private JFrame fm;
private JLabel lb1,lb2,lb3,lb4;
private ImageIcon i0,i1,i2,i3,i4,ig1,ig2,ig3,ig4;
private T1 t1=new T1();
Ch101()
{
i0=new ImageIcon(Ch101.class.getResource("pic/star.png"));
i1=new ImageIcon(Ch101.class.getResource("pic/2.png"));
i2=new ImageIcon(Ch101.class.getResource("pic/3.png"));
i3=new ImageIcon(Ch101.class.getResource("pic/1.png"));
i4=new ImageIcon(Ch101.class.getResource("pic/4.png"));
ig1=new ImageIcon(Ch101.class.getResource("pic/g2.png"));
ig2=new ImageIcon(Ch101.class.getResource("pic/g1.png"));
ig3=new ImageIcon(Ch101.class.getResource("pic/g3.png"));
ig4=new ImageIcon(Ch101.class.getResource("pic/g4.png"));
lb1=new JLabel(ig1);
lb1.setBounds(50,20,128,128);
lb1.addMouseListener(t1);
lb2=new JLabel(ig2);
lb2.setBounds(250,20,128,128);
lb2.addMouseListener(new T2());
lb3=new JLabel(ig3);
lb3.setBounds(50,160,128,128);
lb3.addMouseListener(new MouseListener(){
public void mousePressed(MouseEvent e){
if(lb3.getIcon()==ig3)
lb3.setIcon(i3);
else
lb3.setIcon(ig3);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
});
lb4=new JLabel(ig4);
lb4.setBounds(250,160,128,128);
lb4.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
if(lb4.getIcon()==ig4)
lb4.setIcon(i4);
else
lb4.setIcon(ig4);
}
});
fm=new JFrame("點按滑鼠切換圖形");
fm.setIconImage(i0.getImage());
fm.setBounds(100,100,420,320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.add(lb1);
fm.add(lb2);
fm.add(lb3);
fm.add(lb4);
}
class T1 implements MouseListener
{
public void mousePressed(MouseEvent e){
if(lb1.getIcon()==ig1)
lb1.setIcon(i1);
else
lb1.setIcon(ig1);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class T2 extends MouseAdapter
{
public void mousePressed(MouseEvent e){
if(lb2.getIcon()==ig2)
lb2.setIcon(i2);
else
lb2.setIcon(ig2);
}
}
public static void main(String[] args){
new Ch101();
}
}
複製代碼
作者:
劉得恩
時間:
2016-6-24 21:56
import javax.swing.*;
import java.awt.event.*;
public class Ch101 implements MouseListener{
JFrame f;
JLabel icon,lb[]=new JLabel[4];
ImageIcon ic,g[]=new ImageIcon[4],pic[]=new ImageIcon[4];
Ch101()
{
ic=new ImageIcon(Ch101.class.getResource("pic/star.png"));
for(int i=0;i<4;i++)
pic[i]=new ImageIcon(Ch101.class.getResource("pic/"+(i+1)+".png"));
for(int i=0;i<4;i++)
g[i]=new ImageIcon(Ch101.class.getResource("pic/g"+(i+1)+".png"));
f=new JFrame("xxxx");
f.setLayout(null);
f.setResizable(false);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 420, 320);
f.setIconImage(ic.getImage());
for(int i=0;i<2;i++)
{
lb[i]=new JLabel(g[i]);
lb[i].setBounds(i*200+50,20,128,128);
lb[i].addMouseListener(this);
f.add(lb[i]);
}
for(int i=0;i<2;i++)
{
lb[i+2]=new JLabel(g[i+2]);
lb[i+2].setBounds(i*200+50,160,128,128);
lb[i+2].addMouseListener(this);
f.add(lb[i+2]);
}
}
public static void main(String[] args){
new Ch101();
}
public void mousePressed(MouseEvent e)
{
JLabel l=(JLabel)e.getSource();
;
for(int i=0;i<4;i++)
if(l.getIcon()==g[i])
lb[i].setIcon(pic[i]);
else if(l.getIcon()==pic[i])
lb[i].setIcon(g[i]);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
複製代碼
作者:
林宇翔
時間:
2016-6-25 14:22
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ch00{
private JFrame fm;
private JLabel lb1,lb2,lb3,lb4;
private ImageIcon ii,ii1,ii2,ii3,ii4,iig1,iig2,iig3,iig4;
private Try1 try1=new Try1();
ch00()
{
ii=new ImageIcon(ch00.class.getResource("pic/star.png"));
ii1=new ImageIcon(ch00.class.getResource("pic/1.png"));
ii2=new ImageIcon(ch00.class.getResource("pic/2.png"));
ii3=new ImageIcon(ch00.class.getResource("pic/3.png"));
ii4=new ImageIcon(ch00.class.getResource("pic/4.png"));
iig1=new ImageIcon(ch00.class.getResource("pic/g1.png"));
iig2=new ImageIcon(ch00.class.getResource("pic/g2.png"));
iig3=new ImageIcon(ch00.class.getResource("pic/g3.png"));
iig4=new ImageIcon(ch00.class.getResource("pic/g4.png"));
lb1=new JLabel(iig1);
lb1.setBounds(50, 20, 128, 128);
lb1.addMouseListener(try1);
lb2=new JLabel(iig2);
lb2.setBounds(250, 20, 128, 128);
lb2.addMouseListener(new Try2());
lb3=new JLabel(iig3);
lb3.setBounds(50, 160, 128, 128);
lb3.addMouseListener(new MouseListener(){ //匿名式
public void mousePressed(MouseEvent e)
{
if(lb3.getIcon()==iig3)
lb3.setIcon(ii3);
else
lb3.setIcon(iig3);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
});
lb4=new JLabel(iig4);
lb4.setBounds(250, 160, 128, 128);
lb4.addMouseListener(new MouseAdapter(){ //匿名式
public void mousePressed(MouseEvent e)
{
if(lb4.getIcon()==iig4)
lb4.setIcon(ii4);
else
lb4.setIcon(iig4);
}
});
fm=new JFrame("滑鼠指標拖曳圖形");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(ii.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()==iig1)
lb1.setIcon(ii1);
else
lb1.setIcon(iig1);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try2 extends MouseAdapter //成員式
{
public void mousePressed(MouseEvent e)
{
if(lb2.getIcon()==iig2)
lb2.setIcon(ii2);
else
lb2.setIcon(iig2);
}
}
public static void main(String[] args) {
new ch00();
}
}
複製代碼
作者:
李允軒
時間:
2016-6-25 14:42
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ch01{
private JFrame fm;
private JLabel lb1,lb2,lb3,lb4;
private ImageIcon ii,ii1,ii2,ii3,ii4,iig1,iig2,iig3,iig4;
ch01()
{
ii=new ImageIcon(ch01.class.getResource("pic/star.png"));
ii1=new ImageIcon(ch01.class.getResource("pic/1.png"));
ii2=new ImageIcon(ch01.class.getResource("pic/2.png"));
ii3=new ImageIcon(ch01.class.getResource("pic/3.png"));
ii4=new ImageIcon(ch01.class.getResource("pic/4.png"));
iig1=new ImageIcon(ch01.class.getResource("pic/g1.png"));
iig2=new ImageIcon(ch01.class.getResource("pic/g2.png"));
iig3=new ImageIcon(ch01.class.getResource("pic/g3.png"));
iig4=new ImageIcon(ch01.class.getResource("pic/g4.png"));
lb1=new JLabel(iig1);
lb1.setBounds(50, 20, 128, 128);
lb1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
if(lb1.getIcon()==iig1)
lb1.setIcon(ii1);
else
lb1.setIcon(iig1);
}
});
lb2=new JLabel(iig2);
lb2.setBounds(250, 20, 128, 128);
lb2.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
if(lb2.getIcon()==iig2)
lb2.setIcon(ii2);
else
lb2.setIcon(iig2);
}
});
lb3=new JLabel(iig3);
lb3.setBounds(50, 160, 128, 128);
lb3.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
if(lb3.getIcon()==iig3)
lb3.setIcon(ii3);
else
lb3.setIcon(iig3);
}
});
lb4=new JLabel(iig4);
lb4.setBounds(250, 160, 128, 128);
lb4.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
if(lb4.getIcon()==iig4)
lb4.setIcon(ii4);
else
lb4.setIcon(iig4);
}
});
fm=new JFrame("滑鼠指標拖曳圖形");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(ii.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);
}
public static void main(String[] args) {
ch01 app=new ch01();
}
}
複製代碼
作者:
張彥承
時間:
2016-6-25 16:04
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Ch101{
private JFrame fm;
private JLabel lb1,lb2,lb3,lb4;
private ImageIcon ii,ii1,ii2,ii3,ii4,iig1,iig2,iig3,iig4;
private Try1 try1=new Try1();
Ch101()
{
ii=new ImageIcon(Ch101.class.getResource("pic/star.png"));
ii1=new ImageIcon(Ch101.class.getResource("pic/1.png"));
ii2=new ImageIcon(Ch101.class.getResource("pic/2.png"));
ii3=new ImageIcon(Ch101.class.getResource("pic/3.png"));
ii4=new ImageIcon(Ch101.class.getResource("pic/4.png"));
iig1=new ImageIcon(Ch101.class.getResource("pic/g1.png"));
iig2=new ImageIcon(Ch101.class.getResource("pic/g2.png"));
iig3=new ImageIcon(Ch101.class.getResource("pic/g3.png"));
iig4=new ImageIcon(Ch101.class.getResource("pic/g4.png"));
lb1=new JLabel(iig1);
lb1.setBounds(50, 20, 128, 128);
lb1.addMouseListener(try1);
lb2=new JLabel(iig2);
lb2.setBounds(250, 20, 128, 128);
lb2.addMouseListener(new Try2());
lb3=new JLabel(iig3);
lb3.setBounds(50, 160, 128, 128);
lb3.addMouseListener(new MouseListener(){
public void mousePressed(MouseEvent e)
{
if(lb3.getIcon()==iig3)
lb3.setIcon(ii3);
else
lb3.setIcon(iig3);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
});
lb4=new JLabel(iig4);
lb4.setBounds(250, 160, 128, 128);
lb4.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
if(lb4.getIcon()==iig4)
lb4.setIcon(ii4);
else
lb4.setIcon(iig4);
}
});
fm=new JFrame("滑鼠指標拖曳圖形");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(ii.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()==iig1)
lb1.setIcon(ii1);
else
lb1.setIcon(iig1);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Try2 extends MouseAdapter //成員式
{
public void mousePressed(MouseEvent e)
{
if(lb2.getIcon()==iig2)
lb2.setIcon(ii2);
else
lb2.setIcon(iig2);
}
}
public static void main(String[] args) {
new Ch101();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2