一、初步认识Swing
JFrame是GUI中的容器
JButton是最常见的组件-按钮
注意:容器对象.setVisible(true);会对所有的组件进行渲染,所以一定要放在最后面
简单的例子见如下代码
package learn;
import javax.swing.JFrame;
import javax.swing.JButton;
public class GUI {
public static void main(String[] args) {
//主窗体
JFrame f=new JFrame("爱情系统");
//主窗体设置大小(长宽)
f.setSize(600,400);
//主窗体设置位置(在Eclipse上显示的位置)
f.setLocation(350,350);
//主窗体中的组件设置为绝对定位
f.setLayout(null);
//按钮组件
JButton z=new JButton("射手座");
JButton w=new JButton("水瓶座");
//设置组件的大小和位置
//第一个数值是距离左边的距离,第二个数值是距离上边的距离,第三和第四分别是组件的长宽
z.setBounds(50,50,150,30);
w.setBounds(300,50,150,30);
//把按钮加入到主窗体中
f.add(z);
f.add(w);
//关闭窗口的时候,退出程序
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//让窗口变得可见
f.setVisible(true);
}
}
二、事件监听
import java.awt.event.ActionEvent;动作事件
import java.awt.event.ActionListener;动作监听程序
import javax.swing.ImageIcon;图像标志
import javax.swing.JLable;标志
按钮监听见如下代码:
package learn;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class love {
public static void main(String[] args) {
JFrame f = new JFrame("爱之名");
f.setSize(700, 600);
f.setLocation(580, 200);
f.setLayout(null);
final JLabel l = new JLabel();
ImageIcon i = new ImageIcon("D:/新建文件夹/sheshou.jpg");
l.setIcon(i);
l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());
final JLabel ll=new JLabel();
ImageIcon ii = new ImageIcon("D:/新建文件夹/shuiping.jpg");
ll.setIcon(ii);
ll.setBounds(300,50,ii.getIconWidth(),ii.getIconHeight());
JButton b = new JButton("水瓶座");
b.setBounds(120, 300, 100, 30);
JButton bb=new JButton("射手座");
bb.setBounds(350,300,100,30);
// 给按钮 增加 监听
b.addActionListener(new ActionListener() {
// 当按钮被点击时,就会触发 ActionEvent事件
// actionPerformed 方法就会被执行
public void actionPerformed(ActionEvent e) {
l.setVisible(false);
}
});
f.add(l);
f.add(ll);
f.add(b);
f.add(bb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
键盘监听见如下代码:
import java.awt.event.KeyListener;键盘监听器
import java.awt.event.KeyEvent;键盘事件
keyPressed代表 键被按下
keyReleased 代表键被弹起
keyTyped 代表一个向下弹起的组合动作
keyEvent.getKeyCode() 可以获取当前点下了哪个键
package learn;
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 learn2 {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
final JLabel l = new JLabel();
ImageIcon i = new ImageIcon("D:/新建文件夹/sheshou.jpg");
l.setIcon(i);
l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());
// 增加键盘监听
f.addKeyListener(new KeyListener() {
// 键被弹起
public void keyReleased(KeyEvent e) {
// 39代表按下了 “右键”
if (e.getKeyCode() == 39) {
// 图片向右移动 (y坐标不变,x坐标增加)
l.setLocation(l.getX() + 10, l.getY());
}
}
//键被按下
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
// 一个按下弹起的组合动作
public void keyTyped(KeyEvent e) {
}
});
f.add(l);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
鼠标监听:
import java.awt.event.MouseEvent;鼠标事件
import java.awt.event.MouseListener;鼠标监听器
mouseReleased 鼠标释放
mousePressed 鼠标按下
mouseExited 鼠标退出
mouseEntered 鼠标进入
mouseClicked 鼠标点击
举例子代码如下,使用mouseEntered,当鼠标进入图片的时候,图片就移动位置
package learn;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JButton;
public class ml {
public static void main(String[] args) {
// TODO Auto-generated method stub
final JFrame f=new JFrame("来抓住我吧");
f.setSize(800,600);
f.setLocationRelativeTo(null);
f.setLayout(null);
final JLabel l=new JLabel();
ImageIcon i=new ImageIcon("D:/新建文件夹/sheshou.jpg");
l.setIcon(i);
l.setBounds(300,300,i.getIconWidth(),i.getIconHeight());
f.add(l);
l.addMouseListener(new MouseListener(){
//释放鼠标
public void mouseReleased(MouseEvent e){
}
//按下鼠标
public void mousePressed(MouseEvent e){
}
//退出鼠标
public void mouseExited(MouseEvent e){
}
//鼠标进入
public void mouseEntered(MouseEvent e){
Random r=new Random();
int x=r.nextInt(f.getWidth() - l.getWidth());
int y=r.nextInt(f.getHeight() - l.getHeight());
l.setLocation(x,y);
}
//按下释放组合动作为点击鼠标
public void mouseClicked(MouseEvent e){
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
适配器
import java.awt.event.MouseAdapter鼠标监听适配器
一般在写监听器的时候,会实现MouseListener。但是MouseListener里面有很多方法都没有用到,比如mouseReleased,mousePressed等等;这个时候就可以使用MouseAdapter,只需要重写必要的方法即可
// MouseAdapter 适配器,只需要重写用到的方法,没有用到的就不用写了
l.addMouseListener(new MouseAdapter() {
// 只有mouseEntered用到了
public void mouseEntered(MouseEvent e) {
Random r = new Random();
int x = r.nextInt(f.getWidth() - l.getWidth());
int y = r.nextInt(f.getHeight() - l.getHeight());
l.setLocation(x, y);
}
});
三、容器
java的图形界面中,容器是用来存放按钮、输入框等组件的
窗体容器有两种,一个是JFrame,一个是JDialog
写法一样,注意区别的是JFrame右上角有最大化最小化按钮,JDialog没有。
模态JDialog:当一个对话框被设置为模态的时候,其背后的父窗体是不能被激活的,除非该对话框被关闭,见如下代码
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("外部窗体");
f.setSize(800, 600);
f.setLocation(100, 100);
// 根据外部窗体实例化JDialog
JDialog d = new JDialog(f);
// 设置为模态
d.setModal(true);
d.setTitle("模态的对话框");
d.setSize(400, 300);
d.setLocation(200, 200);
d.setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);