標題:
滑鼠事件 (二)
[打印本頁]
作者:
周政輝
時間:
2018-3-10 10:16
標題:
滑鼠事件 (二)
實作指標位置對準圖片, 點擊滑鼠左鍵切換圖片.
[attach]3543[/attach]
[attach]3544[/attach]
[attach]3545[/attach][attach]3546[/attach][attach]3547[/attach]
作者:
黃茂勛
時間:
2018-3-10 10:32
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Main extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
ImageIcon icon = new ImageIcon(Main.class.getResource("star.png"));
Image img = icon.getImage().getScaledInstance(45, 45, java.awt.Image.SCALE_FAST);
frame.setIconImage(img);
frame.setTitle("點按滑鼠切換圖形");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
ImageIcon icon = new ImageIcon(Main.class.getResource("gift-green.png"));
ImageIcon icon_2 = new ImageIcon(Main.class.getResource("hippo.png"));
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setBounds(144, 56, 164, 154);
lblNewLabel.setIcon(icon);
contentPane.add(lblNewLabel);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(e.getButton() == 1){
lblNewLabel.setIcon(icon_2);
}
if(e.getButton() == 3){
lblNewLabel.setIcon(icon);
}
}
});
}
}
複製代碼
作者:
蔡庭豪
時間:
2018-3-10 10:35
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MouseButton extends JFrame {
private JPanel contentPane;
private ImageIcon icon ;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MouseButton frame = new MouseButton();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MouseButton() {
icon = new ImageIcon(MouseButton.class.getResource("gift-green.png"));
Image img = icon.getImage().getScaledInstance(109,105,java.awt.Image.SCALE_FAST );
ImageIcon newicon = new ImageIcon(img);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lbl = new JLabel(newicon);
lbl.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if(arg0.getButton() == arg0.BUTTON1){
icon = new ImageIcon(MouseButton.class.getResource("hippo.png"));
}else if(arg0.getButton() == arg0.BUTTON3){
icon = new ImageIcon(MouseButton.class.getResource("gift-green.png"));
}
Image img = icon.getImage().getScaledInstance(109,105,java.awt.Image.SCALE_FAST );
ImageIcon newiconMB = new ImageIcon(img);
lbl.setIcon(newiconMB);
}
});
lbl.setBounds(125, 71, 109, 105);
contentPane.add(lbl);
}
}
複製代碼
作者:
張健勳
時間:
2018-3-10 10:38
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Toolkit;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Main extends JFrame {
private JPanel contentPane;
private ImageIcon icon;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("pic/star.png")));
setTitle("點擊滑鼠切換圖形");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
ImageIcon newIcon = new ImageIcon(Main.class.getResource("gift-green.png"));
ImageIcon newhippo = new ImageIcon(Main.class.getResource("hippo.png"));
JLabel lb = new JLabel(newIcon);
lb.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(e.getButton()==1) {
lb.setIcon(newhippo);
}
}
});
lb.setBounds(120, 72, 200, 116);
contentPane.add(lb);
}
}
複製代碼
作者:
陳泓瑜
時間:
2018-3-10 10:40
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GIFT extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GIFT frame = new GIFT();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GIFT() {
setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\TEST\\GIFT\\bin\\star.png"));
setTitle("\u9EDE\u6309\u6ED1\u9F20\u5207\u63DB\u5716\u5F62");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel gift = new JLabel("");
gift.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == 1)
{
gift.setIcon(new ImageIcon(GIFT.class.getResource("hippo.png")));
}
}
});
gift.setIcon(new ImageIcon(GIFT.class.getResource("gift-green.png")));
gift.setBounds(139, 54, 128, 142);
contentPane.add(gift);
}
}
複製代碼
作者:
蔡季樺
時間:
2018-3-10 10:49
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Toolkit;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Main extends JFrame {
private JPanel contentPane;
private ImageIcon icon;
private JLabel label;
private int t=0;
private Icon newIcon;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/bbs/istak/org/tw/star.png")));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
String[] s = {"hippo.png","gift-green.png"};
icon = new ImageIcon(Main.class.getResource(s[0]));
Image img = icon.getImage().getScaledInstance(50, 50, Image.SCALE_FAST);
newIcon = new ImageIcon(img);
JLabel label = new JLabel(newIcon);
label.setBounds(143, 57, 115, 111);
contentPane.add(label);
label.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
icon = new ImageIcon(Main.class.getResource(s[t]));
Image img = icon.getImage().getScaledInstance(50, 50, Image.SCALE_FAST);
newIcon = new ImageIcon(img);
label.setIcon(newIcon);
if(t==1)
{
t=0;
}else
{
t++;
}
}
});
}
}
複製代碼
作者:
林侑成
時間:
2018-3-10 11:06
package asdf;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Toolkit;
public class Asdf extends JFrame {
private JPanel contentPane;
private ImageIcon icon ;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Asdf frame = new Asdf();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Asdf() {
setTitle("\u9EDE\u6309\u6ED1\u9F20\u5207\u63DB\u5716\u5F62");
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\student\\Downloads\\star.png"));
icon = new ImageIcon(Asdf.class.getResource("gift-green.png"));
Image img = icon.getImage().getScaledInstance(109,105,java.awt.Image.SCALE_FAST );
ImageIcon newicon = new ImageIcon(img);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel bg = new JLabel(newicon);
bg.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if(arg0.getButton() == 1){
icon = new ImageIcon(Asdf.class.getResource("hippo.png"));
}
if(arg0.getButton() == 3){
icon = new ImageIcon(Asdf.class.getResource("gift-green.png"));
}
Image img = icon.getImage().getScaledInstance(109,105,java.awt.Image.SCALE_FAST );
ImageIcon newiconMB = new ImageIcon(img);
bg.setIcon(newiconMB);
}
});
bg.setBounds(125, 71, 109, 105);
contentPane.add(bg);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2