Bản quyền thuộc về TITV.vn,
vui lòng không đăng tải lại nội dung từ trang này.
Video giải thích chi tiết
Code chi tiết
package model;
public class MouseExampleModel {
private int x, y;
private int count;
private String checkIn; // yes, no
public MouseExampleModel() {
this.x = 0;
this.y = 0;
this.count = 0;
this.checkIn = "no";
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getCheckIn() {
return checkIn;
}
public void setCheckIn(String checkIn) {
this.checkIn = checkIn;
}
public void addClick() {
this.count++;
}
public void enter() {
this.checkIn = "yes";
}
public void exit() {
this.checkIn = "no";
}
}
package view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import controller.MouseExampleController;
import model.MouseExampleModel;
public class MouseExampleView extends JFrame{
private MouseExampleModel model;
private JPanel jPanel_mouse;
private JLabel jLabel_position;
private JLabel jLabel_x;
private JLabel jLabel_y;
private JLabel jLabel_count;
private JLabel jLabel_count_value;
private JLabel jLabel_empty_1;
private JLabel jLabel_check_in;
private JLabel jLabel_check_in_value;
private JLabel jLabel_empty_2;
public MouseExampleView() throws HeadlessException {
this.model = new MouseExampleModel();
this.init();
}
private void init() {
this.setTitle("MOUSE EXAMPLE");
this.setSize(600, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
MouseExampleController mouseExampleController = new MouseExampleController(this);
jPanel_mouse = new JPanel();
jPanel_mouse.setBackground(Color.cyan);
jPanel_mouse.addMouseListener(mouseExampleController);
jPanel_mouse.addMouseMotionListener(mouseExampleController);
JPanel jPanel_info = new JPanel();
jPanel_info.setLayout(new GridLayout(3, 3));
Font font = new Font("Arial", Font.BOLD, 40);
jLabel_position = new JLabel("Position: ");
jLabel_position.setFont(font);
jLabel_x = new JLabel("x = ");
jLabel_x.setFont(font);
jLabel_y = new JLabel("y = ");
jLabel_y.setFont(font);
jLabel_count = new JLabel("Number of clicks: ");
jLabel_count.setFont(font);
jLabel_count_value = new JLabel("n");
jLabel_count_value.setFont(font);
jLabel_empty_1 = new JLabel();
jLabel_empty_1.setFont(font);
jLabel_check_in = new JLabel("Mouse is in component:");
jLabel_check_in.setFont(font);
jLabel_check_in_value = new JLabel("no");
jLabel_check_in_value.setFont(font);
jLabel_empty_2 = new JLabel();
jLabel_empty_2.setFont(font);
jPanel_info.add(jLabel_position);
jPanel_info.add(jLabel_x);
jPanel_info.add(jLabel_y);
jPanel_info.add(jLabel_count);
jPanel_info.add(jLabel_count_value);
jPanel_info.add(jLabel_empty_1);
jPanel_info.add(jLabel_check_in);
jPanel_info.add(jLabel_check_in_value);
jPanel_info.add(jLabel_empty_2);
this.setLayout(new BorderLayout());
this.add(jPanel_mouse, BorderLayout.CENTER);
this.add(jPanel_info, BorderLayout.SOUTH);
this.setVisible(true);
}
public void click() {
this.model.addClick();
this.jLabel_count_value.setText(this.model.getCount()+"");
}
public void enter() {
this.model.enter();
this.jLabel_check_in_value.setText(this.model.getCheckIn());
}
public void exit() {
this.model.exit();
this.jLabel_check_in_value.setText(this.model.getCheckIn());
}
public void update(int x, int y) {
this.model.setX(x);
this.model.setY(y);
this.jLabel_x.setText(this.model.getX()+"");
this.jLabel_y.setText(this.model.getY()+"");
}
}
package controller;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import view.MouseExampleView;
public class MouseExampleController implements MouseListener, MouseMotionListener{
private MouseExampleView mouseExampleView;
public MouseExampleController(MouseExampleView mouseExampleView) {
this.mouseExampleView = mouseExampleView;
}
@Override
public void mouseClicked(MouseEvent e) {
this.mouseExampleView.click();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
this.mouseExampleView.enter();
int x = e.getX();
int y = e.getY();
this.mouseExampleView.update(x, y);
}
@Override
public void mouseExited(MouseEvent e) {
this.mouseExampleView.exit();
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
this.mouseExampleView.update(x, y);
}
}
package test;
import javax.swing.UIManager;
import view.MouseExampleView;
public class Test {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new MouseExampleView();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Không có nhận xét nào:
Đăng nhận xét