GUI编程---学习笔记

组件

  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘事件
  • 破解工具、外挂

GUI核心开发技术

        Swing  AWT

  弊端:

  • 界面不美观
  • 需要jre环境

  好处:

  • 可以写一些小工具
  • 有的需要Swing界面
  • 了解MVC架构 了解监听

       

AWT

包含了很多的类和接口 GUI编程 :图形用户界面编程

Eclipse:Java写的 所以启动慢

元素:窗口,按钮,文本框

Java.awt包

组件和核心

frame

Frame frame = new Frame("我的第一个Java图形界面窗口");
		
		//需要设置Frame可见性
		frame.setVisible(true);
		
		//设置窗口大小
		frame.setSize(400,400);
		
		//设置背景颜色
		Color color = new Color(85,150,68);
		frame.setBackground(color);
		
		//弹出初始位置
		frame.setLocation(200,200);
		
		//设置大小固定
		frame.setResizable(false);

自定义封装frame 实现快速打开多个窗口

class MyFrame extends Frame{
	static int id = 0;//记录窗口个数
	public MyFrame(int x,int y ,int w,int h,Color color) {
		super("Myframe"+(++id));
		setVisible(true);
		setBounds(x,y,w,h);
		setBackground(color);
	}
}

面板Panel

//可以看成一个空间 但不能单独存在
public class TestPanel {
	public static void main(String[] args) {
		Frame frame = new Frame();
		//布局的概念
		Panel panel = new Panel();
		
		//设置布局
		frame.setLayout(null);
		//坐标
		frame.setBounds(300,300,500,500);
		frame.setBackground(new Color(40,161,35));
		
		//panel设置坐标 相对于frame
		panel.setBounds(50,50,400,400);
		panel.setBackground(new Color(193,15,60));
		
		//panel放在frame里面
		frame.add(panel);
		
		frame.setVisible(true);
		
		//监听时间 监听窗口关闭时间 System.exit
		//适配器模式
		frame.addWindowListener(new WindowAdapter() {
			//窗口点击关闭的时候做的事情
			@Override
			public void windowClosing(WindowEvent e) {
				//结束程序
				System.exit(0);
			}
		});
		
	}

}

布局管理器

  • 流式布局
    //设置为流式布局
    		//frame.setLayout(new FlowLayout());
    		frame.setLayout(new FlowLayout(FlowLayout.LEFT));

  • 东南西北中
            frame.add(east,BorderLayout.EAST);
    		frame.add(west,BorderLayout.WEST);
    		frame.add(south,BorderLayout.SOUTH);
    		frame.add(north,BorderLayout.NORTH);
    		frame.add(center,BorderLayout.CENTER);
    

  • 表格布局
    frame.setLayout(new GridLayout(3,2));

  • 自动布局 
    frame.pack();//java函数 自动布局

 事件监听

class MyActionListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		System.out.println("123");
	}
	
}

输入框TextField监听

public class TestText1 {
	public static void main(String[] args) {
		new MyFrame1();
	}

}

class MyFrame1 extends Frame{
	public MyFrame1() {
		TextField textField = new TextField();
		add(textField);
		
		//监听文本框输入的文字
		MyActionListener2 myActionListener2  = new MyActionListener2();
		//按下回车触发
		textField.addActionListener(myActionListener2);
		
		//设置替换编码
		textField.setEchoChar('*');
		setVisible(true);
		pack();
	}
}

class MyActionListener2 implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		TextField Field = (TextField)e.getSource();//获得一些资源
		System.out.println(Field.getText());//获取输入框文本
		Field.setText("");
	}
	
}

简易计算器 组合+内部类

oop原则:组合,大于继承!


//简易计算器
public class TestCalc {
	public static void main(String[] args) {
		new Calculator();
	}

}

//计算器类
class Calculator extends Frame{
	public Calculator() {
		//3个文本框
		TextField num1 = new TextField(10);//字符数
		TextField num2 = new TextField(10);//字符数
		TextField num3 = new TextField(10);//字符数
		//1个按钮
		Button button = new Button("=");
		
		button.addActionListener(new MyCalculatorListener(num1,num2,num3));
		//1个标签
		Label label = new Label("+");
		//布局
		setLayout(new FlowLayout());
		
		add(num1);
		add(label);
		add(num2);
		add(button);
		add(num3);
		pack();
		setVisible(true);
		
	}
}

//监听器
class MyCalculatorListener implements ActionListener{

	//获取三个变量
	private TextField num1;
	private TextField num2;
	private TextField num3;
	public MyCalculatorListener(TextField num1,TextField num2,TextField num3) {
		// TODO Auto-generated constructor stub
		this.num1= num1;
		this.num2= num2;
		this.num3= num3;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//获得加数和被加数
		int n1 = Integer.parseInt(num1.getText());
		int n2 = Integer.parseInt(num2.getText());
		
		//把值放在第三个框
		num3.setText(n1+n2+"");
		//清楚前两个框
	}
	
}

优化后代码 改造面向对象写法

//简易计算器
public class TestCalc {
	public static void main(String[] args) {
		new Calculator().loadFrame();
	}

}

//计算器类
class Calculator extends Frame{
	//属性
	TextField num1;
	TextField num2;
	TextField num3;
	
	//方法
	public void loadFrame() {
		//3个文本框
		 num1 = new TextField(10);//字符数
		 num2 = new TextField(10);//字符数
		 num3 = new TextField(10);//字符数
		//1个按钮
		Button button = new Button("=");
		//1个标签
		Label label = new Label("+");
		
		
		button.addActionListener(new MyCalculatorListener(this));
		
		//布局
		setLayout(new FlowLayout());
		add(num1);
		add(label);
		add(num2);
		add(button);
		add(num3);
		
		
		pack();
		setVisible(true);
		
	}
	
}

//监听器
class MyCalculatorListener implements ActionListener{

	//获取计算机对象  在一个类中组合另外一个类
	Calculator calculator = null;
	
	public MyCalculatorListener(Calculator calculator) {
		// TODO Auto-generated constructor stub
		this.calculator = calculator;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//获得加数和被加数
		int n1 = Integer.parseInt(calculator.num1.getText());
		int n2 = Integer.parseInt(calculator.num2.getText());

		//把值放在第三个框
		calculator.num3.setText(n1+n2+"");
		//清楚前两个框
	}
	
}

内部类 更好的包装 内部类的好处 可以畅通无阻的访问外部的属性和方法

//简易计算器
public class TestCalc {
	public static void main(String[] args) {
		new Calculator().loadFrame();
	}

}

//计算器类
class Calculator extends Frame{
	//属性
	TextField num1;
	TextField num2;
	TextField num3;
	
	//方法
	public void loadFrame() {
		//3个文本框
		 num1 = new TextField(10);//字符数
		 num2 = new TextField(10);//字符数
		 num3 = new TextField(10);//字符数
		//1个按钮
		Button button = new Button("=");
		//1个标签
		Label label = new Label("+");
		
		
		button.addActionListener(new MyCalculatorListener());
		
		//布局
		setLayout(new FlowLayout());
		add(num1);
		add(label);
		add(num2);
		add(button);
		add(num3);
		
		
		pack();
		setVisible(true);
		
	}
	//监听器
	private class MyCalculatorListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			//获得加数和被加数
			int n1 = Integer.parseInt(num1.getText());
			int n2 = Integer.parseInt(num2.getText());
			//把值放在第三个框
			num3.setText(n1+n2+"");
			//清楚前两个框
		}
		
	}
	
}

画笔

public class TestPaint {
	public static void main(String[] args) {
		new MyPaint().loadFrame();
	}

}

class MyPaint extends Frame{
	
	public void loadFrame() {
		setBounds(200, 200, 600, 500);
		setVisible(true);
		
	}
	
	//画笔
	@Override
	public void paint(Graphics g) {
		//super.paint(g); 有的父类不能随便删 有可能有初始化的操作
		//画笔 需要有颜色 可以画画
		g.setColor(Color.red);
		//g.drawOval(100, 100, 100, 100);
		g.fillOval(100, 100, 100, 100);
		g.setColor(Color.GREEN);
		g.fillRect(150, 200, 200, 200);
		
		//画笔用完 把它颜色还原
		//g.setColor(null);
	}
}

鼠标监听

//鼠标监听事件
public class TestMouseListener {
	public static void main(String[] args) {
		new MyMouseFrame("画图");
	}
}

class MyMouseFrame extends Frame{
	//需要画笔 需要监听鼠标当前的位置 需要集合来存储这个点
	ArrayList points;
	
	public MyMouseFrame(String title) {
		super(title);
		setBounds(200,200,400,300);
		//存鼠标点击的点
		points = new ArrayList<>();
		
		setVisible(true);
		
		this.addMouseListener(new MyMouseListener());
		
	}
	
	@Override
	public void paint(Graphics g) {
		//监听鼠标事件
		Iterator iterator = points.iterator();
		while(iterator.hasNext()) {
			Point point = (Point)iterator.next();
			g.setColor(Color.gray);
			g.fillOval(point.x, point.y, 10, 10);
		}
		
	}
	//添加点
	public void addPaint(Point point) {
		points.add(point);
	}
	//适配器模式
	private class MyMouseListener extends MouseAdapter{
		//鼠标按下 弹起 按住不放
		@Override
		public void mousePressed(MouseEvent e) {
			 MyMouseFrame mouseFrame = (MyMouseFrame)e.getSource();
			 //点击的时候 就会产生一个点
			 //点就是鼠标的点
			 mouseFrame.addPaint(new Point(e.getX(),e.getY()));
			 
			 //每次点击鼠标都需要重新画一遍
			 mouseFrame.repaint();//刷新
		}
	}
}

窗口监听

public class TestWindow {
	public static void main(String[] args) {
		new WindowFrame();
	}

}

class WindowFrame extends Frame{
	
	public WindowFrame() {
		setBackground(Color.red);
		setBounds(100,100,200,200);
		setVisible(true);
		//addWindowListener(new MyWindowListener());
		
		this.addWindowListener(
				//匿名内部类
				new WindowAdapter() {
					@Override
					public void windowClosing(WindowEvent e) {
						System.out.println("你点击了X");
						System.exit(0);
					}
				});
	}
	
	class MyWindowListener extends WindowAdapter{
		@Override
		public void windowClosing(WindowEvent e) {
			setVisible(false);//隐藏窗口
			//System.exit(0);//正常推出0  非正常退出1
		}
		
	}
}

键盘监听

//键
public class TestKeyListener {
	public static void main(String[] args) {
		new KeyFrame();
	}

}

class KeyFrame extends Frame{
	public KeyFrame() {
		setBounds(1,2,300,400);
		setVisible(true);
		
		this.addKeyListener(new KeyAdapter() {
			//键盘按下
			@Override
		    public void keyPressed(KeyEvent e) {
				//获取键盘的码
				int keyCode = e.getKeyCode();//不需要记录数值 直接使用静态属性 VK_
				if(keyCode==KeyEvent.VK_UP) {
					System.out.println("你按下了上键");
					
				}
				
				//根据不同的操作 产生不同的结果
			}

		});
	}
	
}

Swing

窗口

public class JFrameDemo {
	//init();new 初始化
	public void init() {
		//顶级窗口
		JFrame jFrame =	new JFrame("这是一个JFrame窗口");
		jFrame.setVisible(true);
		jFrame.setBounds(100,100,200,200);
		jFrame.setBackground(Color.red);
		//设置文字 Jlabel
		JLabel jLabel = new JLabel("123456789");
		
		jLabel.setHorizontalAlignment(SwingConstants.CENTER);//标签居中
		jFrame.add(jLabel);
		
		//容器实例化
		Container container = jFrame.getContentPane();
		container.setBackground(Color.yellow);
		
		//关闭事件
		jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
	}
	public static void main(String[] args) {
		//建立一个窗口
		new JFrameDemo().init();
	}

}

弹窗

JDialog 用来写弹窗 默认有关闭事件

//主窗口
public class DialogDemo extends JFrame{
	public static void main(String[] args) {
		new DialogDemo();
	}
	
	public DialogDemo() {
		this.setVisible(true);
		this.setSize(700,500);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		//放东西
		Container container = this.getContentPane();
		//绝对布局
		container.setLayout(null);
		
		//按钮
		JButton button = new JButton("点击弹出以恶个对话框");
		button.setBounds(30, 30, 200, 50);
		
		//点击按钮弹出一个弹窗
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//弹窗
				new MyDialogDemo();
			}
		});
		
		container.add(button);
	}

}

//弹窗的窗口
class MyDialogDemo extends JDialog{
	public MyDialogDemo() {
		this.setVisible(true);
		this.setBounds(100,100,500,500);
		//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		Container container = this.getContentPane();
		container.setLayout(null);
		
		container.add(new Label("12138"));
	}
	
}

标签

JLabel

图标 ICON

//图标一个接口 需要实现类 Frame继承
public class IconDemo extends JFrame implements Icon {

	private int width;
	private int height;
	
	public IconDemo() {
		// TODO Auto-generated constructor stub
	}
	public IconDemo(int width,int height) {
		// TODO Auto-generated constructor stub
		this.width = width;
		this.height = height;
	}
	
	@Override
	public void paintIcon(Component c, Graphics g, int x, int y) {
		// TODO Auto-generated method stub
		g.fillOval(x, y, width, height);
		
	}

	@Override
	public int getIconWidth() {
		// TODO Auto-generated method stub
		return this.width;
	}

	@Override
	public int getIconHeight() {
		// TODO Auto-generated method stub
		return this.height;
	}
	
	public void init() {
		IconDemo iconDemo = new IconDemo(15,15);
		//图标放在标签上 也可以放在按钮上
		JLabel jLabel = new JLabel("icontest",iconDemo,SwingConstants.CENTER);
	
		Container container = getContentPane();
		container.add(jLabel);
		
		this.setVisible(true);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		new IconDemo().init();
	}

}

放图片

public class ImageIconDemo extends JFrame{
	public ImageIconDemo() {
		
		JLabel jLabel = new JLabel("ImageIcon");
		//获取图片的地址
		URL url = ImageIconDemo.class.getResource("logo.png");
		ImageIcon imageIcon =  new ImageIcon(url);//命名不要冲突
		
		jLabel.setIcon(imageIcon);
		jLabel.setHorizontalAlignment(SwingConstants.CENTER);
		
		Container container = getContentPane();
		container.add(jLabel);
		
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setBounds(100,100,200,200);
	}
	
	public static void main(String[] args) {
		new ImageIconDemo();
	}

}

面板

JPanel

public class JPanelDemo extends JFrame{
	
	public JPanelDemo() {
		Container container = getContentPane();
		container.setLayout(new GridLayout(2,1,10,10));//后面是间距
		
		JPanel jPanel1 = new JPanel(new GridLayout(1,3));
		JPanel jPanel2 = new JPanel(new GridLayout(1,2));
		JPanel jPanel3 = new JPanel(new GridLayout(2,1));
		JPanel jPanel4 = new JPanel(new GridLayout(3,2));
		jPanel1.add(new JButton("1"));
		jPanel1.add(new JButton("1"));
		jPanel2.add(new JButton("2"));
		jPanel2.add(new JButton("2"));
		jPanel3.add(new JButton("3"));
		jPanel3.add(new JButton("3"));
		jPanel4.add(new JButton("4"));
		jPanel4.add(new JButton("4"));
		
		container.add(jPanel1);
		container.add(jPanel2);
		
		container.add(jPanel3);
		container.add(jPanel4);
		
		setVisible(true);
		setSize(500,500);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new JPanelDemo();
	}
}

JScrollPanel滚动条

public class JScrollDemo extends JFrame{
	public JScrollDemo() {
		Container container = this.getContentPane();
		
		//文本域
		JTextArea jTextArea =  new JTextArea(20,50);
		jTextArea.setText("12138");
		
		//SCroll面板
		JScrollPane jScrollPane =  new JScrollPane(jTextArea);
		container.add(jScrollPane);
		
		this.setVisible(true);
		this.setBounds(100,100,300,350);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new JScrollDemo();
	}

}

按钮

图片按钮

public class JButtonDemo1 extends JFrame{
	
	public JButtonDemo1() {
		Container container =  this.getContentPane();
		//将一个图片变成一个图标
		URL url = JButtonDemo1.class.getResource("logo.png");
		Icon icon = new ImageIcon(url);
		
		//将图标放在按钮上
		JButton button = new JButton();
		button.setIcon(icon);
		button.setToolTipText("图片按钮");
		
		container.add(button);
		
		this.setVisible(true);
		this.setBounds(100,100,500,500);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		//绝对定位固定大小
	}
	
	public static void main(String[] args) {
		new JButtonDemo1();
	}

}
  • 单选按钮
    public class JButtonDemo2 extends JFrame{
    	public JButtonDemo2() {
    		Container container = this.getContentPane();
    		
    		//单选框
    		JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
    		JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
    		JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");
    		
    		//单选框只能选一个 分组
    		ButtonGroup buttonGroup = new ButtonGroup();
    		buttonGroup.add(jRadioButton1);
    		buttonGroup.add(jRadioButton2);
    		buttonGroup.add(jRadioButton3);
    		
    		container.add(jRadioButton1,BorderLayout.SOUTH);
    		container.add(jRadioButton2,BorderLayout.CENTER);
    		container.add(jRadioButton3,BorderLayout.NORTH);
    		
    		
    		
    		this.setVisible(true);
    		this.setSize(500,300);
    		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	}
    	
    	
    	
    	public static void main(String[] args) {
    		new JButtonDemo2();
    	}
    
    }

  • 多选按钮
    public class JButtonDemo3 extends JFrame{
    	public JButtonDemo3() {
    		Container container = this.getContentPane();
    		
    		//多选框
    		JCheckBox checkBox1 = new JCheckBox("checkBox1");
    		JCheckBox checkBox2 = new JCheckBox("checkBox2");
    
    		container.add(checkBox1,BorderLayout.NORTH);
    		container.add(checkBox2,BorderLayout.SOUTH);
    		
    		this.setVisible(true);
    		this.setSize(300,500);
    		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	}
    	
    	public static void main(String[] args) {
    		new JButtonDemo3();
    	}
    	
    
    }

列表

  • 下拉框 JComboBox
    public class TestComboboxDemo1 extends JFrame{
    	public TestComboboxDemo1() {
    		Container container = this.getContentPane();
    		
    		JComboBox statusBox = new JComboBox();
    		
    		statusBox.addItem("1");
    		statusBox.addItem("2");
    		statusBox.addItem("3");
    		statusBox.addItem("4");
    		
    		
    		container.add(statusBox);
    		
    		this.setVisible(true);
    		this.setSize(500,350);
    		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	}
    	
    	public static void main(String[] args) {
    		new TestComboboxDemo1();
    	}
    
    }

  • 列表框 
    public class TestComboboxDemo2 extends JFrame{
    	public TestComboboxDemo2() {
    		Container container = this.getContentPane();
    		
    		//生成列表的内容
    		//String[] c = {"1","2","3"};
    		Vector c = new Vector();
    		//列表中需要的内容
    		JList jList = new JList(c);
    		c.add("1");
    		
    		container.add(jList);
    		
    		this.setVisible(true);
    		this.setSize(500,300);
    		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	}
    	
    	public static void main(String[] args) {
    		new TestComboboxDemo2();
    	}
    }

    应用场景

  • 选择地区 下拉框

  • 列表展示信息 动态扩容

文本框

  • 文本框
    public class TestTextDemo1 extends JFrame{
    	public TestTextDemo1() {
    		Container container = this.getContentPane();
    		
    		JTextField textField = new JTextField("hello");
    		JTextField textField2 = new JTextField("world",20);
    		
    		container.add(textField,BorderLayout.NORTH);
    		container.add(textField2,BorderLayout.SOUTH);
    		
    		this.setVisible(true);
    		this.setSize(500,350);
    		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	}
    	
    	public static void main(String[] args) {
    		new TestTextDemo1();
    	}
    
    }

  • 密码框
    public class TestTextDemo1 extends JFrame{
    	public TestTextDemo1() {
    		Container container = this.getContentPane();
    		
    		JPasswordField passwordField = new JPasswordField();
    		passwordField.setEchoChar('@');
    		container.add(passwordField);
    		
    		this.setVisible(true);
    		this.setSize(500,350);
    		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	}
    	
    	public static void main(String[] args) {
    		new TestTextDemo1();
    	}
    
    }
    

  • 文本域   跟面板一起使用
    public class JScrollDemo extends JFrame{
    	public JScrollDemo() {
    		Container container = this.getContentPane();
    		
    		//文本域
    		JTextArea jTextArea =  new JTextArea(20,50);
    		jTextArea.setText("12138");
    		
    		//SCroll面板
    		JScrollPane jScrollPane =  new JScrollPane(jTextArea);
    		container.add(jScrollPane);
    		
    		this.setVisible(true);
    		this.setBounds(100,100,300,350);
    		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	}
    	
    	public static void main(String[] args) {
    		new JScrollDemo();
    	}
    
    }

贪吃蛇


帧,如果时间足够小,就是动画,一秒30帧 60帧。连起来就是动画 拆开就是静态的图片

 

学习视频来源 狂神说

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值