kk Blog —— 通用基础

date [-d @int|str] [+%s|"+%F %T"]

Java WindowListener & ActionListener

Java WindowListener & ActionListener

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//package java_window;
import java.awt.*;
import java.awt.event.*;

class Window
{
	Frame fra = new Frame();
	public static int tt = 1;
	public static Label lb = new Label(" label ");
	public void go() {
		fra.addWindowListener(
			new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		fra.setSize(700, 550);
		fra.setLayout(null);
		Button but = new Button(" OK ");
		but.setBounds(200, 200, 100, 70); fra.add(but);
		lb.setBounds(200, 300, 200, 100);  fra.add(lb);

		but.addActionListener(
			new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				 lb.setText("ActionEvent "+event.getActionCommand()+"   "+(tt++));
			}
		});
		fra.show();
	}
}

public class Main {
	public static void main(String[] args) {
		Window win = new Window();
		win.go();
	}
}

Java KeyListener 的使用

Java KeyListener 的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//package java_key;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class KeyWork extends JFrame implements KeyListener
{
	private JLabel status;
	public KeyWork()
	{
		super("abcd");
		status = new JLabel();
		status.setFont(new Font("TimesRoman", Font.BOLD, 50));
		getContentPane().add(status, BorderLayout.CENTER);
		addKeyListener(this);
		setSize(700,500);
		show();
	}
	public void keyPressed(KeyEvent e) {
		char ch = e.getKeyChar();
		status.setText(String.valueOf(ch) + "   " +(int)ch);
	}
	public void keyTyped(KeyEvent e) {
	   
	}
	public void keyReleased(KeyEvent e) {
	   
	}
}

public class Main {
	public static void main(String[] args) {
		KeyWork app = new KeyWork();
		app.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
}

Java MouseListener & MouseMotionListener

Java 获取鼠标坐标
1
Point point= MouseInfo.getPointerInfo().getLocation();

Java MouseListener & MouseMotionListener 的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MouseWork extends JFrame implements MouseListener,MouseMotionListener
{
	private JLabel status;
	public MouseWork()
	{
		super("abcd");
		status = new JLabel();
		status.setFont(new Font("TimesRoman", Font.BOLD, 50));
		getContentPane().add(status, BorderLayout.CENTER);
		addMouseListener(this);
		addMouseMotionListener(this);
		setSize(700,500);
		show();
	}
	public void mousePressed(MouseEvent e) {
		status.setText(" Pressed "+e.getX()+" "+e.getY());
	}
	public void mouseExited(MouseEvent e) {
		status.setText(" Exited ");
	}
	public void mouseEntered(MouseEvent e) {
		status.setText(" Entered ");
	}
	public void mouseReleased(MouseEvent e) {
		status.setText(" Released ");
	}
	public void mouseClicked(MouseEvent e) {
		status.setText(" Clicked ");
	}
	public void mouseDragged(MouseEvent e) {
		status.setText(" Dragged "+e.getX()+" "+e.getY());
	}
	public void mouseMoved(MouseEvent e) {
		status.setText(" Moved "+e.getX()+" "+e.getY());
	}
}

public class Main {
	public static void main(String[] args) {
		MouseWork app = new MouseWork();
		app.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
}

Java 常用数据结构

Java class 的排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.*;

class Node implements Comparable
{
	int x,y;
	public int compareTo(Object obj){
		Node oo=(Node)obj;
		if(oo.x < this.x || oo.x == this.x && oo.y <this.y)return 1;
		return -1;
	}
}

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		Node a[]=new Node[11];
		int i,j,k,l;
		for(i=1;i<=10;i++) {
			a[i]=new Node();
			a[i].x=Math.abs(5-i); a[i].y=10-Math.abs(7-i);
		}
		Arrays.sort(a, 1, 11);
		for(i=1;i<=10;i++)System.out.println(a[i].x+" "+a[i].y);
	}
}

Java Vector 的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		Vector V=new Vector();
		V.add(1); V.add(1.234); V.add('a'); V.add("abcd");
		System.out.println((Integer)V.get(0));
		System.out.println((Double)V.get(1));
		System.out.println((Character)V.get(2));
		System.out.println((String)V.get(3));
	}
}

Java 队列的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		Queue Q=new LinkedList<Integer>();
		int i,j,k,l;
		while(!Q.isEmpty())Q.poll(); // 清空队列
		for(i=1;i<=10;i++)Q.offer(i);
		for(i=1;i<=10;i++){
			k=(Integer)Q.poll();
			System.out.println(k);
		}
	}
}

Java 优先队列的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		PriorityQueue<Integer> Q = new PriorityQueue<Integer>(30,
			new Comparator<Integer>(){
				public int compare(Integer aa, Integer bb) {
					if(aa < bb)return 1;
					return -1;
				}
			});
		int i,j,k,l;
	   
		Q.clear();
		for(i=1;i<=10;i++)Q.offer(i);
		while(!Q.isEmpty()) {
			k=(Integer)Q.poll();
			System.out.println(k);
		}
	}
}

http://www.yiibai.com/java/util/java_util_hashset.html

Java Collection 的使用

boolean contains(Object o) 如果此set包含指定的元素,此方法返回true。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		Collection set=new HashSet();
		Collection set1=new ArrayList();
		Collection set2=new LinkedList();

		ArrayList LI=new ArrayList();
		LinkedList LD=new LinkedList();

		int i,j,k,l;

		set.clear();          // set1 set2 LI LD 用法类似
		set.add(1); set.add(1.234); set.add('a'); set.add("abcd");
		System.out.println(set.toString());
		set.remove(1); set.remove(3);
		System.out.println(set.toString());
	}
}

http://www.yiibai.com/java/util/java_util_hashmap.html

Java HashMap(很好用) TreeMap

boolean containsKey(Object key) 如果此映射包含指定键的映射此方法返回true。

boolean containsValue(Object value) 如果此映射一个或多个键映射到指定值,该方法返回true

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		Map map=new HashMap();
		int i,j,k,l;
		map.clear();
		for(i=1;i<=10;i++)map.put(Math.pow(10,i), i);
		for(i=1;i<=10;i++)System.out.println((Integer)map.get(Math.pow(10,i)));
		System.out.println();
	   
		Map tree=new TreeMap();
		tree.clear();
		for(i=1;i<=10;i++)tree.put(i,10-i);
		for(i=1;i<=10;i++)
		{
			k=(Integer)tree.get(i);
			System.out.println(k);
		}
	}
}

Java I/O

Java 最简单输入就是用 Scanner 类 但是很慢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
import java.io.*;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int a,b;
		while(cin.hasNext())
		{
			a=cin.nextInt(); b=cin.nextInt();
			System.out.println(a+b);
		}
	}
}

输入用 StreamTokenizer ,输出用 PrintWriter 就 很快

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.*;public class Main
{
	public static void main(String[] args) throws IOException
	{
		StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		int a, b;
		while(in.nextToken() != StreamTokenizer.TT_EOF)
		{
			a = (int)in.nval;
			in.nextToken();
			b = (int)in.nval;
			out.println(a + b);
		}
		out.flush();
	}
}

但是 StreamTokenizer 有所局限, 用 StringTokenizer 能很好解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
import java.io.*;
import java.math.*;public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int a,b;
		BigInteger c,d;
		String line;
		while((line=in.readLine())!=null)
		{
			st=new StringTokenizer(line);
			a=Integer.parseInt(st.nextToken());
			b=Integer.parseInt(st.nextToken());
			System.out.println(a+b);
		   
			line=in.readLine();
			st=new StringTokenizer(line);
			c=new BigInteger(st.nextToken().trim());
			d=new BigInteger(st.nextToken().trim());
			System.out.println(c.add(d));
		}
	}
}

Java 还可以 直接用 BufferedReader 类来输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;
import java.io.*;
import java.math.*;public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
		int a,b,rr,t;
		while(true)
		{
			while((rr=read.read())==10 || rr==13 || rr==32);
			if(rr<0)break;
			if(rr=='-'){ t=1; a=0; } else { a=rr-48; t=0; }
			while((rr=read.read())!=32 && rr!=13 && rr!=10)a=a*10+rr-48;
			if(t==1)a=-a;
			while((rr=read.read())==10 || rr==13 || rr==32);
			if(rr=='-'){ t=1; b=0; } else { b=rr-48; t=0; }
			while((rr=read.read())!=10 && rr!=13 && rr!=10)b=b*10+rr-48;
			if(t==1)b=-b;
			System.out.println(a + b);
		}
	}
}

Java 文件输入 输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
import java.io.*;public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(new FileInputStream("in.txt"));
		PrintWriter out=new PrintWriter(new OutputStreamWriter(new FileOutputStream("out.txt")));
		int a,b;
		while(cin.hasNext())
		{
			a=cin.nextInt(); b=cin.nextInt();
			out.println(a + b);
		}
		out.flush();
	}
} 

Java 向文件中加入数据

1
PrintWriter out=new PrintWriter(new OutputStreamWriter(new FileOutputStream("out.txt")), false);