Hello Swing
初识Swing!
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGuiA {
public static void main(String[] args) {
// TODO Auto-generated method stub
//新建一个主窗体
JFrame j=new JFrame("lol外挂");
//设置主窗体窗大小
j.setSize(400, 400);
//设置主窗体位置
j.setLocation(200, 200);
//主窗体的位置为绝对位置
j.setLayout(null);
//新建一个按钮
JButton jb=new JButton("外挂");
//设置按钮的位置,宽度和高度
jb.setBounds(140, 140, 100, 50);
//把按钮添加到主窗体中
j.add(jb);
//设置关闭窗口的同时结束程序
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 让窗体变得可见
j.setVisible(true);
}
}
事件监听
按钮监听
匿名类对外部变量的访问要加上final
创建一个匿名类实现ActionListener接口,当按钮被点击时,actionPerformed方法就会被调用!
JLabel标签,ImageIcon图形组件根据图像画图形组件
package mygui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestGuiB {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf=new JFrame("LOL");
jf.setSize(400,400);
jf.setLocation(500, 500);
jf.setLayout(null);
final JLabel l=new JLabel();//创建一个新的标签
ImageIcon image=new ImageIcon("F:/Wangchuang/shana.png");//图片组件
l.setIcon(image);//将图片放到组件中去
l.setBounds(50, 50, image.getIconWidth(), image.getIconHeight());
JButton jb=new JButton("隐藏琴女");
jb.setBounds(150,200, 100, 30);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
l.setVisible(false);//设置标签不可见
}
});
jf.add(l);
jf.add(jb);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
键盘监听
JFrame.addKeyListener (new KeyListener(){
重写方法
});
重写方法(KeyListenner)里面,keyPressed键按下
keyReleased键弹起
keyTyped代表一个按下弹起的组合动作
KeyEvent.getKeyCode()代表获取当前点下了哪一个键!
package mygui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestGuiB {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf=new JFrame("LOL");
jf.setSize(400,400);
jf.setLocation(500, 500);
jf.setLayout(null);
final JLabel l=new JLabel();//创建一个新的标签
ImageIcon image=new ImageIcon("F:/Wangchuang/shana.png");//图片组件
l.setIcon(image);//将图片放到组件中去
l.setBounds(50, 50, image.getIconWidth(), image.getIconHeight());
JButton jb=new JButton("隐藏琴女");
jb.setBounds(150,200, 100, 30);
//键盘监听
jf.addKeyListener(new KeyListener() {
@Override
//组合键
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
//键被按下
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
//键被弹起
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode() == 39) {
l.setLocation(l.getX()+50,l.getY());
}
}
});
jf.add(l);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
鼠标监听
MouseListener 鼠标监听器
mouseReleased 鼠标释放
mousePressed 鼠标按下
mouseExited 鼠标退出
mouseEntered 鼠标进入
mouseClicked 鼠标点击
package mygui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestGuiB {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf=new JFrame("LOL");
jf.setSize(400,400);
jf.setLocation(500, 500);
jf.setLayout(null);
final JLabel l=new JLabel();//创建一个新的标签
ImageIcon image=new ImageIcon("F:/Wangchuang/shana.png");//图片组件
l.setIcon(image