⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java11.txt

📁 达内IT培训Core Java全部笔记 是学习Java编程的好东西
💻 TXT
字号:
CardLayout 卡片布局卡片布局当中每个组件都要起名字,为了支持show方法,所以加入组件时必须带名字-----------------------------------------------------------------------------------------package com.tarena.day11.gui;import java.awt.CardLayout;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class TestCard implements ActionListener{	JPanel jp = new JPanel();		CardLayout cl = new CardLayout();	public TestCard(){		JFrame jf = new JFrame("Card");			jf.setLayout(new FlowLayout());		jp.setLayout(cl);					String[] str = {"first","previous","next","last"};		JButton[] jb = new JButton[str.length];		for (int i = 0; i < str.length; i++) {			jb[i] = new JButton(str[i]);			jb[i].addActionListener(this);			jf.add(jb[i]);					}		JLabel[] jl = new JLabel[6];		for (int i = 0; i < jl.length; i++) {			jl[i] = new JLabel(i+1+"");			jp.add(jl[i],""+i);		}		jf.add(jp);		jf.pack();		jf.setVisible(true);		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		}	public void actionPerformed(ActionEvent e) {		String comm = e.getActionCommand();		if(comm.equals("first")){			cl.first(jp);		}else if(comm.equals("next")){			cl.next(jp);		}else if(comm.equals("last")){			cl.last(jp);		}else{			cl.previous(jp);		}			}	public static void main(String[] args){		new TestCard();	}}-----------------------------------------------------------------------------------------GridBagLayout 增强网格布局管理器	需要的时候参见APIJFC:Icon图标:-----------------------------------------------------------------------------------------package com.tarena.day11.gui;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;public class TextIcon {	public TextIcon(){		JFrame jf = new JFrame();		Icon ic = new ImageIcon("/home/soft01/DDD.png");		JButton jb = new JButton(ic);		jf.add(jb);		jf.pack();		jf.setVisible(true);		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	}	public static void main(String[] args) {			new TextIcon();	}}-----------------------------------------------------------------------------------------多行文本域(JTextArea,JScrollPane):-----------------------------------------------------------------------------------------package com.tarena.day11.gui;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;public class TestArea {	public TestArea(){		JFrame jf = new JFrame("TestArea");		JTextArea jta = new JTextArea(10,40);		jf.add(new JScrollPane(jta));		jf.setSize(300,200);		jf.setVisible(true);		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	}	public static void main(String[] args) {			new TestArea();	}}-----------------------------------------------------------------------------------------Java.swing.Timer 计时器:-----------------------------------------------------------------------------------------package com.tarena.day11.gui;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.Timer;import javax.swing.JButton;import javax.swing.JFrame;public class TestTimer implements ActionListener{	Timer t = new Timer(1000,this);	public TestTimer(){		JFrame jf = new JFrame("Timer");		jf.setLayout(new FlowLayout());		JButton jb1 = new JButton("start");		JButton jb2 = new JButton("end");		jf.add(jb1);		jf.add(jb2);		jb1.addActionListener(this);		jb2.addActionListener(this);			jf.setSize(300,200);		jf.setVisible(true);		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	}	public void actionPerformed(ActionEvent e) {		String comm = e.getActionCommand();		if ("start".equals(comm)) {		//变量和常量比较的时候尽量使常量放在前面比较,避免变量空指针异常			t.start();		}else if("end".equals(comm)){			t.stop();		}else{				//为Timer服务的			System.out.println("Hello");		}			}	public static void main(String[] args) {		new TestTimer();	}}-----------------------------------------------------------------------------------------设计模式(单例,模板):变化行动	观察者模式-----------------------------------------------------------------------------------------package com.tarena.day11.observer;import java.util.Observable;public class Person extends Observable{	public void shout(){		System.out.println("口号已喊,待捉");		this.setChanged();		this.notifyObservers("我冤");	}}-----------------------------------------------------------------------------------------package com.tarena.day11.observer;import java.util.Observable;import java.util.Observer;public class Bianyi implements Observer{	public void update(Observable o, Object arg) { 		//是在notifyObservers(arg)传入		System.out.println("已经抓走!"+arg);		}		public static void main(String[] args){		Person p = new Person();		Bianyi b = new Bianyi();		Bianyi b1 = new Bianyi();		System.out.println("begin");		p.addObserver(b);		p.addObserver(b1);		p.shout();		System.out.println("over!");			}}输出结果:begin口号已喊,待捉已经抓走! 我冤已经抓走! 我冤over!-----------------------------------------------------------------------------------------适配器模式,用来混合任意两个不相干的模式:-----------------------------------------------------------------------------------------package com.tarena.day11.adapter;public class Mi {	public void sellMi(){		System.out.println("卖大米");	}}-----------------------------------------------------------------------------------------package com.tarena.day11.adapter;public class Rou {	public void sellRou(){		System.out.println("卖肉");	}}//要在米店买到肉-----------------------------------------------------------------------------------------package com.tarena.day11.adapter;public class MiAdapter extends Mi{	private Rou r = new Rou();	@Override	public void sellMi(){		r.sellRou();	}	public static void main(String[] args) {		Mi mi = new Mi();		mi.sellMi();		Mi mi1 = new MiAdapter();		mi1.sellMi();	}}输出结果:卖大米卖肉-----------------------------------------------------------------------------------------IO输入输出流:读出文本-----------------------------------------------------------------------------------------package com.tarena.day11.io;import java.io.FileInputStream;public class ReadFile {	public static void main(String[] args) {		try{		//用于记事本打开			FileInputStream fis = new FileInputStream("/home/soft01/syh.txt");			byte[] b = new byte[1024];			String s = "";			while(true){				int temp = fis.read(b);				if(temp == -1) break; //读完了				s = s + new String(b,0,temp);			}			fis.close();			System.out.println(s);		}catch(Exception ee){			ee.printStackTrace();		}	}}-----------------------------------------------------------------------------------------写入文本-----------------------------------------------------------------------------------------package com.tarena.day11.io;import java.io.FileOutputStream;public class WriteFile {	public static void main(String[] args) {		try{			FileOutputStream fos = new FileOutputStream("/home/soft01/Desktop/bcd.txt");			String s = "我爱北京天安门";			fos.write(s.getBytes());			fos.close();		}catch(Exception ee){			ee.printStackTrace();		}	}}-----------------------------------------------------------------------------------------线程(threads):	依赖于操作系统一个程序就是一个进程(Process)Unix下ps -aux:看进程kill -9 进程号:不顾一切的杀进程当有多个程序同时运行的时候,多线程的程序要考虑线程间的影响进程之间的堆栈内存都是独立的线程栈内存独立,堆共享,线程可以并发运行,相互之间可能有影响,因为堆内存是共享的线程是程序中单一的顺序流线程不依赖其他线程,但会影响其他线程一个进程只能有一个主线程,但是可以有N个其他线程。其他线程不一定依赖主线程,主线程结束其他线程不一定结束。java.lang.Runnable 接口,只有一个方法 run() 无构造,无返回值,无形参,没有异常抛出run()提供线程代码Thread有虚拟CPU,又实现了Runnable线程之间并行,这种并行毫无规律可言,是一种乱序运行,同一个线程内部依然是顺序执行,不会乱-----------------------------------------------------------------------------------------package com.tarena.day11.thread;public class TestThread extends Thread{	public void run(){		//线程写的是run方法,调用start方法才能启用		for (int i = 0; i < 30; i++) {			System.out.println("run:"+i);			try {				Thread.sleep(1);			} catch (InterruptedException e) {				e.printStackTrace();			}		}	}	public static void main(String[] args) {			TestThread tt = new TestThread();			tt.start();					//调用start()启动			for (int i = 0; i < 30; i++) {				System.out.println("main:"+i);				try {					Thread.sleep(1);				} catch (InterruptedException e) {					e.printStackTrace();				}			}	}}输出结果(不定):main:0run:0main:1run:1main:2run:2main:3run:3..........-----------------------------------------------------------------------------------------大多数线程采用时间片的方式实现的:1.CPU时间片是一段极短的CPU运行权利的时间2.线程只有拥有CPU时间片时才能运行3.线程调度器负责分配时间片调度线程,会从中随机挑一个线程运行   调度 -> 分配时间片 -> 分给n个线程 -> 如果CPU有空闲 ->随机选择一个线程给CPU执行对于时间段来说是并行,但在同一时刻(时间点)只有一个线程在执行

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -