界面类
package BeautyCameraPC;
import javax.swing.*;
import java.awt.*;
public class UI
{
public void initUI()
{
JFrame jf = new JFrame("BeautyCameraPC");
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setSize(1000,1000);
jf.setVisible(true);
Graphics g = jf.getGraphics();
Mouse mouse = new Mouse(g);
mouse.setG(g);
jf.addMouseListener(mouse);
}
public static void main(String []args)
{
UI myUI = new UI();
myUI.initUI();
}
}
类与对象
类是对象的模板或蓝图,对象是类的实例在Java按照类名 对象名 = new 构造方法; 初始化对象1
JFrame jf = new JFrame("BeautyCameraPC");
鼠标监听器
package BeautyCameraPC;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Mouse implements MouseListener
{
private Graphics g;
public Mouse(Graphics g){this.g=g;System.out.println("construction method");}
public Mouse(){System.out.println("Default construction method");}
public void setG(Graphics g){this.g=g;}
@Override
public void mouseClicked(MouseEvent e)
{
System.out.println("click");
int x=e.getX();
int y=e.getY();
System.out.println("x="+x+" "+"y="+y);
ImageIcon image = new ImageIcon("D:\\Jave\\1.png");
g.drawImage(image.getImage(),100,100,null);
}
@Override
public void mousePressed(MouseEvent e)
{
}
@Override
public void mouseReleased(MouseEvent e)
{
}
@Override
public void mouseEntered(MouseEvent e)
{
}
@Override
public void mouseExited(MouseEvent e)
{
}
}
构造方法的重构
在初始化对象时我们会发现虽然在类中构造方法名字相同,但是参数类型和顺序不相同,这时涉及到重构2
public Mouse(Graphics g){this.g=g;System.out.println("construction method");}
public Mouse(){System.out.println("Default construction method");}