事件中的一些功能
窗口事件: windowOpened(WindowEvent e) 窗体被打开 windowClosing(WindowEvent e) 窗体正在关闭 windowIconified(WindowEvent e) 窗体最小化 windowDeiconified(WindowEvent e) 窗体从最小化恢复 windowActivated(WindowEvent e) 窗体被选中 windowDeactivated(WindowEvent e) 取消窗体被选中
鼠标事件: mouseEntered(MouseEvent e) 鼠标移入组件 mouseExited(MouseEvent e) 鼠标移出组件 mousePressed(MouseEvent e) 鼠标按下 mouseReleased(MouseEvent e) 鼠标松开 mouseClicked(MouseEvent e) 鼠标点击
鼠标事件监听: mouseMoved (MouseEvent) 鼠标移动 mouseDragged(MouseEvent) 鼠标拖动
鼠标滑轮监听: mouseWheelMoved(MouseWheelEvent e) 滑轮滚动
事件中的一些方法
int getClickCount() 点击次数
int getX() 当前鼠标所在的x的坐标
int getY() 当前鼠标所在的y的坐标
实现绝对布局(空布局)与文件选择器 颜色选择器 发送信息操作等操作
package com.zking; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.table.JTableHeader; /** * 实现发送禁言 文件选择器 颜色选择器 * @author lkf * */ public class NullA extends JFrame{ //设置绝对布局 private JPanel jpa=new JPanel(null); private JTextArea jta=new JTextArea(10,10); //把文本域放入滚筒条中间组建中 private JScrollPane jsp=new JScrollPane(jta); private JTextField jtfa=new JTextField(15); private JButton jba=new JButton("发送"); private JButton jbb=new JButton("禁用"); private JButton jbc=new JButton("启用"); private JButton jbe=new JButton("文件选色器"); private JButton jbf=new JButton("颜色选色器"); private JButton jbg=new JButton("退出"); public void Myshow() { 绝对布局坐标和组件的大小 x y 宽 长 jsp.setBounds(0, 0, 504, 250); jpa.add(jsp); jtfa.setBounds(0, 270, 520, 50); jpa.add(jtfa); // jba.setBounds(10, 340, 60, 30); jpa.add(jba); jbb.setBounds(80, 340, 60, 30); jpa.add(jbb); jbc.setBounds(150, 340, 60, 30); jpa.add(jbc); jbe.setBounds(220, 340, 100, 30); jpa.add(jbe); jbf.setBounds(330, 340, 100, 30); jpa.add(jbf); jbg.setBounds(440, 340, 60, 30); jpa.add(jbg); } public void myshow() { jba.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String text = jtfa.getText(); // jta.setText(text);//不规范 jta.append(text+"\n");//后尾追加 不覆盖 jta.setLineWrap(true);//内容超过最大宽度时自动跨行 清空输入框 jtfa.setText("");//发送后自动清空 } }); jbb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtfa.setEditable(false); //注意用哪个方法关闭的就要用哪个方法开启 //第二种禁用方式 // jtfa.setEnabled(false); } }); jbc.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtfa.setEditable(true); //第二种启用方式 // jtfa.setEnabled(true); } }); /** * 文件管理器 */ jbe.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //实例化一个文件管理器 JFileChooser jfa=new JFileChooser(); //设置打开 jfa.showDialog(null, "请选择"); //获取文件的详细信息 File file = jfa.getSelectedFile();//获取文件选择其中所选中的文件 //获取选中文件的存储路径 if(null!=file) {//给与非空判断 否则没有选择会报错 jtfa.setText(file.getPath()); } } }); /** * 颜色选择器 */ jbf.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //实例化一个颜色选择器 JColorChooser jcc=new JColorChooser(); //设置打开 initialColor设置颜色选择器打开后选择的颜色 //选择器返回的一个颜色对象 Color color=jcc.showDialog(null, "请你选择颜色", Color.YELLOW); //设置输入框字体颜色 jtfa.setForeground(color); jta.setBackground(color); } }); jbg.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public NullA() { this.setTitle(""); this.setSize(520,430); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); Myshow(); myshow(); this.getContentPane().add(jpa); this.setVisible(true); } public static void main(String[] args) { new NullA(); } }
窗口事件
package com.zking; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JFileChooser; import javax.swing.JFrame; /** *窗体事件 * @author lkf * */ public class Mywindow extends JFrame{ public Mywindow() { this.setTitle(""); this.setSize(200,400); this.setDefaultCloseOperation(3); this.setLocationRelativeTo(null); this.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { System.out.println("窗体被打开"); } @Override public void windowIconified(WindowEvent e) { System.out.println("窗体最小化"); } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub System.out.println("窗体被还原"); } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub System.out.println("窗体取消选中"); } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.out.println("窗体正在关闭中"); } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub System.out.println("窗口已经关闭"); } @Override public void windowActivated(WindowEvent e) { System.out.println("窗体被选中"); } }); this.setVisible(true); } public static void main(String[] args) { new Mywindow(); } }
鼠标事件
package com.zking;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* 鼠标事件
* @author lkf
*
*/
public class Myshubiao extends JFrame{
private JPanel jpa=new JPanel(null);
//设置一个图片做演示
private JLabel jla=new JLabel(new ImageIcon("Image\\飞天猪.gif"));
public Myshubiao() {
this.setTitle("");
this.setSize(500,400);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
//将图片插入窗口中
jla.setBounds(300, 260, 71, 88);
jpa.add(jla);
//鼠标事件
this.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("鼠标松开");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("鼠标按下");
//获取鼠标按下 之后的 x y 的坐标
int x2 = e.getX();
int y2 = e.getY();
//设置图片坐标
jla.setLocation(x2, y2);//坐标 不用设置大小
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("鼠标移出");
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("鼠标进入");
int x2 = e.getX();
int y2 = e.getY();
jla.setLocation(x2, y2);
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("鼠标被点击");
}
});
this.getContentPane().add(jpa);
this.setVisible(true);
}
public static void main(String[] args) {
new Myshubiao();
}
}
鼠标事件监听
package com.zking; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * 鼠标事件监听 * @author lkf * */ public class Myshubiao2 extends JFrame{ private JPanel jpa=new JPanel(null); private JLabel jla=new JLabel(new ImageIcon("Image\\飞天猪.gif")); private JLabel jlb=new JLabel(new ImageIcon("Image\\草地.gif")); public Myshubiao2() { this.setTitle(""); this.setSize(841,310); this.setDefaultCloseOperation(3); this.setLocationRelativeTo(null); //将草地放入窗口中 jlb.setBounds(0, 0, 841, 310); jpa.add(jlb); //将图片插入窗口中 jla.setBounds(730, 165, 71, 88); jlb.add(jla); this.addMouseMotionListener(new MouseMotionListener() { @Override public void mouseMoved(MouseEvent e) { // jla.setVisible(true); // System.out.println("鼠标移动"); int x2 = e.getX(); int y2 = e.getY(); // jla.setLocation(x2-40, y2-60);//小猪满地图跑 jla.setLocation(x2-40, 165);//规定路线跑 if(x2>820||x2<20) { jla.setVisible(false); }else { jla.setVisible(true); } // // System.out.println(x2); } @Override public void mouseDragged(MouseEvent e) { // System.out.println("鼠标拖拽"); } }); this.getContentPane().add(jpa); this.setVisible(true); } public static void main(String[] args) { new Myshubiao2(); } }
键盘事件
package com.zking; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.Timer; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * 键盘事件!! * @author lkf * */ public class Myjianpan extends JFrame{ private JPanel jpa=new JPanel(null); private JLabel jla=new JLabel(new ImageIcon("Image\\飞天猪.gif")); private JLabel jlb=new JLabel(new ImageIcon("Image\\草地.gif")); private JLabel jlc=new JLabel(new ImageIcon("Image\\炸弹.gif")); private JLabel jld=new JLabel(new ImageIcon("Image\\飞天猪1.gif")); //用于保存按键图片的左右 private String cha="a"; //判断炸弹往那边发射 private int x=0; public Myjianpan() { this.setTitle(""); this.setSize(841,310); this.setDefaultCloseOperation(3); this.setLocationRelativeTo(null); //将草地放入窗口中 jlb.setBounds(0, 0, 841, 310); jpa.add(jlb); //将图片插入窗口中 jla.setBounds(730, 165, 71, 88); jlb.add(jla); //反转猪 // jld.setBounds(jla.getX(),jla.getY(), 71, 88); jlb.add(jld); jld.setVisible(false); //将炸弹插入窗口中 jlc.setBounds(735, 177, 40, 38); jlb.add(jlc); //设置炸弹隐藏 jlc.setVisible(false); this.addMouseMotionListener(new MouseMotionListener() { @Override public void mouseMoved(MouseEvent e) { // jla.setVisible(true); // System.out.println("鼠标移动"); // int x2 = e.getX(); // int y2 = e.getY(); jla.setLocation(x2-40, y2-60);//小猪满地图跑 // jla.setLocation(x2-40, 165);//规定路线跑 // if(x2>820||x2<20) { // jla.setVisible(false); // }else { // jla.setVisible(true); // } // // System.out.println(x2); } @Override public void mouseDragged(MouseEvent e) { // System.out.println("鼠标拖拽"); System.out.println("鼠标移动"); int x2 = e.getX(); int y2 = e.getY(); // jla.setLocation(x2-40, y2-60);//小猪满地图跑 jla.setLocation(x2-40, 165);//规定路线跑 if(x2>820||x2<20) { jla.setVisible(false); }else { jla.setVisible(true); } } }); //键盘事件 :: this.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { char keyChar = e.getKeyChar();//获得当个键位 //往左走 if(keyChar=='a') { cha="a"; jld.setBounds(jla.getX(),jla.getY(), 71, 88); jld.setVisible(false); jla.setVisible(true); jla.setLocation(jla.getX()-10, jla.getY()); //往右走 }else if(keyChar=='d') { cha="d"; jla.setVisible(false); jld.setVisible(true); jla.setLocation(jld.getX(), jld.getY()); jld.setLocation(jld.getX()+10, jld.getY()); //上走 }else if(keyChar=='w') { if(cha.equals("a")) { jla.setLocation(jla.getX(), jla.getY()-10); }else if(cha.equals("d")){ jld.setLocation(jld.getX(), jld.getY()-10); } //下走 }else if(keyChar=='s') { if(cha.equals("a")) { jla.setLocation(jla.getX()-10, jla.getY()+10); }else if(cha.equals("b")) { jld.setLocation(jld.getX()-10, jld.getY()+10); } //实现往前跳 }else if(keyChar==' ') { jla.setLocation(jla.getX()-20, jla.getY()-20); //实现往后跳 }else if(keyChar=='j') { jla.setLocation(jla.getX()+20, jla.getY()-20); //发射炸弹 }else if(keyChar=='k') { //猪到哪 炸弹跟随 jlc.setLocation(jla.getX(), jlc.getY()); jlc.setVisible(true); if(cha.equals("a")) { x=-20; }else { x=20; } // 多线程 new Thread() { public void run() { while (true) { try { Thread.sleep(100);//休眠 100毫秒刷新一次 } catch (InterruptedException e2) { e2.printStackTrace(); } jlc.setLocation(jlc.getX()+x, jlc.getY()); if(jlc.getX()<-30||jlc.getX()>900) { break; } } }; }.start(); } } @Override public void keyReleased(KeyEvent e) { // System.out.println("松开"); if(e.getKeyChar()==' ') { jla.setLocation(jla.getX()-20, jla.getY()+20); // TODO Auto-generated method stub }else if(e.getKeyChar()=='j') { jla.setLocation(jla.getX()+20, jla.getY()+20); } } @Override public void keyPressed(KeyEvent e) {//键盘按下的那一刻 // TODO Auto-generated method stub } }); this.getContentPane().add(jpa); this.setVisible(true); } public static void main(String[] args) { new Myjianpan(); } }
上传头像流程
package com.zking; import java.awt.Color; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Myshangchuang extends JFrame{ private JPanel jpa=new JPanel(null); private JLabel jla=new JLabel(); private JButton jba=new JButton("更换头像"); public void Myshow() { jla.setBounds(50, 70, 300, 300); jla.setBorder(BorderFactory.createLineBorder(Color.PINK, 5)); jpa.add(jla); jba.setBounds(370, 340, 100, 30); jpa.add(jba); } public Myshangchuang() { this.setTitle(""); this.setSize(600,500); this.setDefaultCloseOperation(3); this.setLocationRelativeTo(null); Myshow(); jba.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //实例化文件管理器 JFileChooser jfc=new JFileChooser(); jfc.showDialog(null, "请你选择头像"); //获取选中图片对象 file File file = jfc.getSelectedFile(); if(null!=file) { //file.getPath()获取文件路径 //1:1压缩 jla.setIcon(new ImageIcon(new ImageIcon(file.getPath()).getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT))); } } }); this.getContentPane().add(jpa); this.setVisible(true); } public static void main(String[] args) { new Myshangchuang(); } }