標題:
[隨堂測驗] 滑鼠事件 (四)
[打印本頁]
作者:
周政輝
時間:
2018-3-10 11:39
標題:
[隨堂測驗] 滑鼠事件 (四)
不限方法, 完成如下的視窗程式練習, 在視窗的右下角顯示滑鼠指標的座標. 當滑鼠指標在視窗中移動時, 座標資訊會不斷地更新.
提示: 實作「MouseMotionListener 介面」或 繼承「MouseMotionAdapter 類別」
JFrame 的設定: 100, 100, 420, 320
JLabel 的設定: 290, 250, 40, 30 & 350, 250, 40, 30
[attach]3551[/attach]
作者:
黃茂勛
時間:
2018-3-10 12:48
package bbs.istak.org.tw;
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 java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
public class Main extends JFrame {
private JPanel contentPane;
private int x, y;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setTitle("顯示滑鼠指標的座標");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 420, 320);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel();
lblNewLabel.setBounds(267, 243, 46, 15);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel();
lblNewLabel_1.setBounds(336, 243, 46, 15);
contentPane.add(lblNewLabel_1);
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
lblNewLabel.setText("x: "+x);
lblNewLabel_1.setText("y: "+y);
}
});
}
}
複製代碼
作者:
蔡季樺
時間:
2018-3-11 14:06
package bbs.istak.org.tw;
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 java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
public class Main extends JFrame {
private JPanel contentPane;
/**
* 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() {
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 TX = new JLabel("x:");
TX.setBounds(266, 237, 14, 15);
contentPane.add(TX);
JLabel TY = new JLabel("y:");
TY.setBounds(320, 237, 14, 15);
contentPane.add(TY);
JLabel x = new JLabel("");
x.setBounds(279, 237, 33, 15);
contentPane.add(x);
JLabel y = new JLabel("");
y.setBounds(332, 237, 33, 15);
contentPane.add(y);
contentPane.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
int x2 = e.getX();
int y2 = e.getY();
x.setText(String.valueOf(x2));
y.setText(String.valueOf(y2));
}
});
}
}
複製代碼
作者:
張健勳
時間:
2018-3-16 23:29
本帖最後由 張健勳 於 2018-3-16 23:57 編輯
package bbs.istak.org.tw;
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 java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
public class Mouse extends JFrame {
private JPanel contentPane;
private int X, Y;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Mouse frame = new Mouse();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Mouse() {
setTitle("\u986F\u793A\u6ED1\u9F20\u6307\u6A19\u7684\u5EA7\u6A19");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 420, 320);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel JX = new JLabel(); //X座標LB
JX.setBounds(267, 243, 46, 15);
contentPane.add(JX);
JLabel JY = new JLabel(); //Y座標Lb
JY.setBounds(336, 243, 46, 15);
contentPane.add(JY);
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
X = e.getX();//抓X座標
Y = e.getY();//抓Y座標
JX.setText("X: "+X); //顯示 X: (X座標)
JY.setText("Y: "+Y); //顯示Y: (Y座標)
}
});
}
}
複製代碼
作者:
陳泓瑜
時間:
2018-3-17 18:06
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class MOUSE
{
JFrame fm;
JLabel lbX,lbY;
MOUSE()
{
lbX= new JLabel();
lbX.setBounds(290, 250, 40, 30);
lbY= new JLabel();
lbY.setBounds(350, 250, 40, 30);
fm=new JFrame("顯示滑鼠指定的座標");
fm.setBounds(100, 100, 420, 320);
fm.setResizable(false);
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.add(lbX);
fm.add(lbY);
fm.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
// TODO 自動產生的方法 Stub
lbX.setText("X="+e.getX());
lbY.setText("Y="+e.getY());
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO 自動產生的方法 Stub
}
});
}
public static void main(String[] args)
{
new MOUSE();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2