第二章ppt
- 编程判断奇数偶数
package test1;
import java.util.*;
public class test3{
public static void main(String[] args) {
System.out.println("请输入一个整数");
Scanner in=new Scanner(System.in);
int n=in.nextInt();
if(n%2==0) {
System.out.println(n+"是偶数");
}else {
System.out.println(n+"是奇数");
}
}
}
- 判断等级
package test1;
import java.util.*;
public class test3{
public static void main(String[] args) {
System.out.println("请输入成绩");
Scanner in=new Scanner(System.in);
int n=in.nextInt();
if(n>80) {
System.out.println(n+":优");
}else if(n>70){
System.out.println(n+":良");
}else if(n>60){
System.out.println(n+":中");
}else {
System.out.println(n+":差");
}
}
}
- 星期几
package test1;
import java.util.*;
public class test3{
public static void main(String[] args) {
System.out.println("请输入数字");
Scanner in=new Scanner(System.in);
int n=in.nextInt();
switch(n) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("输入数字不正确");
break;
}
}
}
- 季节
package test1;
import java.util.*;
public class test3{
public static void main(String[] args) {
System.out.println("请输入数字");
Scanner in=new Scanner(System.in);
int n=in.nextInt();
switch(n) {
case 12:
case 1:
case 2:
System.out.println("冬季");
break;
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
default:
System.out.println("输入数字不正确");
break;
}
}
}
- 录入成绩并求平均
package test1;
import java.util.*;
public class test3{
public static void main(String[] args) {
System.out.println("班级人数");
Scanner in=new Scanner(System.in);
int i=1,sum=0;
int pnum=in.nextInt();
while(i<=pnum) {
System.out.println("请输入第"+i+"个同学的成绩");
Scanner grade=new Scanner(System.in);
int grade1=grade.nextInt();
sum = sum + grade1;
i++;
}
float avg=sum/pnum;
System.out.println("平均成绩为"+avg);
}
}
- 录入学生姓名
package test1;
import java.util.*;
public class test3{
public static void main(String[] args) {
String name;
do{
System.out.println("请输入学生姓名");
Scanner input=new Scanner(System.in);
name=input.nextLine();
}while(!name.equals("q"));
System.out.println("over!");
}
}
- 1~4累加
package test1;
public class test3{
public static void main(String[] args) {
int sum=0;
for(int i=1;i<=4;i++) {
sum+=i;
}
System.out.println("sum="+sum);
System.out.println("over!");
}
}
- 嵌套循环打印三角形
package test1;
public class test3{
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("over!");
}
}
- 九九乘法表
package test1;
public class test3{
public static void main(String[] args) {
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++) {
System.out.print(i+"*"+j+"="+i*j+"\t");
}
System.out.println();
}
System.out.println("over!");
}
}
- 数组排序(冒泡法)
package test1;
public class test3{
public static void main(String[] args) {
int[] a=new int[] {7,3,4,9,5,6};
int temp;
for(int i=0;i<6;i++){
for(int j=1;j<6;j++) {
if(a[j]<a[j-1]) {
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
}
}
for(int i=0;i<6;i++)
System.out.print(a[i]);
}
}
- person类
package test1;
class person{
String name;
int age;
float height;
float weight;
void run(){
System.out.println("the person is running");
}
void speak(){
System.out.println("the person is speaking");
}
}
public class test3{
public static void main(String[] args) {
person p1=new person();
p1.name="小红";
p1.age=10;
System.out.println(p1.name);
System.out.println(p1.age);
p1.run();
}
}
- equals和==应用比较
package test1;
public class test3{
public static void main(String[] args) {
String str1 = new String("abc");
String str2 = new String("abc");
String str3 = str1;
if(str1==str2)
System.out.println("str1==str2");
else
System.out.println("str1!= str2");
if(str1==str3)
System.out.println("str1 == str3");
else
System.out.println("str1 != str3");
}
}
package test1;
public class test3{
public static void main(String[] args) {
String str1 = new String("abc");
String str2 = new String("abc");
String str3 = str1;
if(str1.equals(str2))
System.out.println("str1 equal str2");
else
System.out.println("str1 not equal str2");
if(str1.equals(str3))
System.out.println("str1 equal str3");
else
System.out.println("str1 not equal str3");
}
}
- 图书查询系统
package test1;
import java.util.LinkedList;
import java.util.*;
class Book{
int id;
String name;
double price;
Book(int id,String name,double price){
this.id=id;
this.name=name;
this.price=price;
}
public int getId(){
return id;
}
public void setId(int Id) {
this.id=id;
}
public String getName(){
return name;
}
public void setName(String Name) {
this.name=name;
}
public double getPrice(){
return price;
}
public void setPrice(double Price) {
this.price=price;
}
}
public class test3{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Book book1=new Book(000,"书0",19);
Book book2=new Book(001,"书1",19);
Book book3=new Book(002,"书2",19);
LinkedList Link=new LinkedList();
Link.add(book1);
Link.add(book2);
Link.add(book3);
System.out.println("———————图书信息展示———————");
System.out.println("图书总数:"+Link.size());
System.out.println("———————图书名称展示———————");
for(int i=0;i<Link.size();i++) {
Book books=(Book)Link.get(i);
System.out.println(books.getId()+"\t"+books.getName()+"\t"+books.getPrice());
};
System.out.println("———————选择用户类型:管理员-1,普通用户-2———————");
int type=in.nextInt();
if(type==1) {
System.out.println("———————图书编号———————");
int new_id=in.nextInt();
Book books=(Book)Link.get(new_id);
System.out.println("———————图书new price———————");
double new_price=in.nextInt();
books.price=new_price;
System.out.println(books.getId()+"\t"+books.getName()+"\t"+books.getPrice());
}
else {
System.out.println("———————图书编号———————");
int new_id=in.nextInt();
Book books=(Book)Link.get(new_id);
System.out.println(books.getName()+"\t"+books.getPrice());
}
}
}
- 父子女儿继承类
package test1;
import java.util.*;
class Person{
void speak(){
System.out.println("I am a father,I have two children.");
}
}
class Boy extends Person {
void speak(){
super.speak();
System.out.println("I am the son.");
}
}
class Girl extends Person {
void speak(){
super.speak();
System.out.println("I am the daughter.");
}
}
public class test3 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Choose your sex,boy-B,girl-G");
String sex=in.nextLine();
if(sex.equals("B")) {
Boy boy=new Boy();
boy.speak();
}
else{
Girl girl=new Girl();
girl.speak();
}
}
}
- 接口实现乐器选择
package test1;
public interface Instrument {
public void play();
}
package test1;
import java.util.*;
class Piano implements Instrument{
public void play() {
System.out.println("play the piano");
}
}
class Violin implements Instrument{
public void play() {
System.out.println("play the violin");
}
}
public class test3{
public static void main(String[] args) {
System.out.println("choose instrument");
Scanner in=new Scanner(System.in);
String type=in.nextLine();
if(type.equals("p")) {
Piano piano=new Piano();
piano.play();
}else {
Violin violin=new Violin();
violin.play();
}
}
}
- 抽象类演奏乐器
package test1;
import java.util.*;
abstract class Instrument1{
abstract void play();
}
class Piano extends Instrument1{
public void play() {
System.out.println("play the piano");
}
}
class Violin extends Instrument1{
public void play() {
System.out.println("play the violin");
}
}
public class test3{
public static void main(String[] args) {
System.out.println("choose instrument");
Scanner in=new Scanner(System.in);
String type=in.nextLine();
if(type.equals("p")) {
Piano piano=new Piano();
piano.play();
}else {
Violin violin=new Violin();
violin.play();
}
}
}
- 字符串的基本操作
package test1;
public class test3{
public static void main(String[] args) {
String str1="abcabcbacdba";
int length=str1.length();
System.out.println("length="+length);
System.out.println(str1.substring(0,1));
System.out.println(str1.indexOf("c"));
System.out.println(str1.lastIndexOf("c"));
System.out.println(str1.indexOf("abc"));
System.out.println(str1.lastIndexOf("abc"));
}
}
- 字符串的转换操作
package test1;
public class test3{
public static void main(String[] args) {
String str1="abcABcbacdba1";
char atr1[]=str1.toCharArray();
for(int i=0;i<str1.length();i++) {
System.out.print(atr1[i]);
}
System.out.println();
int i=1;
String j=String.valueOf(i);
System.out.println(str1.toUpperCase());
System.out.println(str1.toLowerCase());
}
}
- 字符串的替换和去空操作
package test1;
public class test3{
public static void main(String[] args) {
String str1=" http :// localhost : 8080 ";
System.out.println(str1);
System.out.println(str1.trim());
System.out.println(str1.replaceAll(" +",""));
}
}
- 字符串的判断操作
package test1;
public class test3{
public static void main(String[] args) {
String s1 = "Starter" ;
String s2 = "St";
System.out.println(s1.startsWith(s2));
System.out.println(s1.endsWith(s2));
System.out.println(s1.contains(s2));
System.out.println(s1.isEmpty());
System.out.println(s1.equals(s2));
}
}
- 字符串截取和分割操作
package test1;
public class test3{
public static void main(String[] args) {
String s1 = "2018-01-24" ;
String[] a2=s1.split("-");
for(int i=0;i<a2.length;i++) {
System.out.println(a2[i]);
}
System.out.println(s1.substring(6));
System.out.println(s1.substring(6,8));
}
}
- 学生信息管理系统
package test1;
import java.util.*;
class Student{
String id;
String name;
int seat;
Student(String id,String name,int seat){
this.id=id;
this.name=name;
this.seat=seat;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public int getSeat() {
return seat;
}
public void setSeat(int seat) {
this.name=name;
}
}
public class test3{
public static void main(String[] args) {
Student stu1=new Student("a","b",1);
Student stu2=new Student("c","d",2);
Student stu3=new Student("e","f",3);
ArrayList list=new ArrayList();
list.add(stu1);
list.add(stu2);//增添操作
list.add(stu3);
list.remove(1);//删除操作
System.out.println("学生人数:" + list.size()); //数组长度
System.out.println("第2个元素是:" + list.get(0));//元素
System.out.println("系统中的学生有:");
for(int i=0;i<list.size();i++){
//从集合中取出后为Object类型,需要进行强制类型转换
Student stu=(Student)list.get(i);//取某位数组,转化为Student定义格式
System.out.println(stu.getName());
}
}
}
- 新闻管理系统
package test1;
import java.util.*;
class News{
String id;
String title;
String writer;
News(String id,String title,String writer){
this.id=id;
this.title=title;
this.writer=writer;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id=id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title=title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer=writer;
}
}
public class test3{
public static void main(String[] args) {
News new1=new News("a","b","1");
News new2=new News("c","d","2");
News new3=new News("e","f","3");
LinkedList list=new LinkedList();
list.add(new1);
list.add(new2);
list.add(new3);
System.out.println("sumTitle="+list.size());
System.out.println("first:");
for(int i=0;i<list.size();i++) {
News new_=(News)list.get(i);
System.out.print(new_.getTitle());
}
System.out.println();
News new4=new News("g","h","4");
list.offer(new4);
System.out.println("second:");
for(int i=0;i<list.size();i++) {
News new_=(News)list.get(i);
System.out.print(new_.getTitle());
}
System.out.println();
list.removeLast();
System.out.println("first:");
for(int i=0;i<list.size();i++) {
News new_=(News)list.get(i);
System.out.print(new_.getTitle());
}
System.out.println();
}
}
- Iterator操作
package test1;
import java.util.*;
public class test3 {
public static void main(String[] args) {
ArrayList list = new ArrayList();
// 向该集合中添加字符串
list.add("data_1");
list.add("data_2");
list.add("data_3");
Iterator iterator = list.iterator(); // 获取Iterator对象
// 判断集合中是否存在下一个元素
while (iterator.hasNext()) {
Object obj = iterator.next(); // 取出ArrayList集合中的元素
System.out.println(obj);
}
}
}
- foreach循环遍历集合
package test1;
import java.util.ArrayList;
public class test3 {
public static void main(String[] args) {
ArrayList list = new ArrayList(); // 创建ArrayList集合
list.add("data_1"); // 向ArrayList集合中添加字符串元素
list.add("data_2");
list.add(3);
for (Object obj : list) { // 使用foreach循环遍历ArrayList对象
System.out.println(obj); // 取出并打印ArrayList集合中的元素
}}}
- foreach循环不能改变元素
package test1;
public class test3 {
static String[] strs = { "aaa", "bbb", "ccc" };
public static void main(String[] args) {
// 1、foreach循环遍历数组
for (String str : strs) {
str = "ddd";
}
System.out.println("foreach循环修改后的数组:" + strs[0] + ","
+ strs[1] + "," + strs[2]);
// 2、for循环遍历数组
for (int i = 0; i < strs.length; i++) {
strs[i] = "ddd";
}
System.out.println("普通for循环修改后的数组:" + strs[0] + ","
+ strs[1] + "," + strs[2]);
}
}
- MapHash的基本用法(键值对)
package test1;
import java.util.HashMap;
import java.util.Map;
public class test3 {
public static void main(String[] args) {
Map map = new HashMap(); // 创建HashMap对象
// 1、向Map存储键值对元素
map.put("1", "Jack");
map.put("2", "Rose");
map.put("3", "Lucy");
map.put("4", "Lucy");
map.put("1", "Tom");
System.out.println(map);
// 2、查看键对象是否存在
System.out.println(map.containsKey("1"));
// 3、获取指定键对象映射的值
System.out.println(map.get("1"));
// 4、获取集合中的键对象和值对象集合
System.out.println(map.keySet());
System.out.println(map.values());
// 6、删除指定键对象映射的键值对元素
map.remove("1");
System.out.println(map);
}
}
- Map接口
package test1;
import java.util.HashMap;
import java.util.Map;
class Student {
String name;
public Student(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setName() {
this.name=name;
}
}
public class test3 {
public static void main(String[] args) {
Map map=new HashMap();
map.put("1",new Student("jack"));
map.put("2",new Student("green"));
map.put("4",new Student("siri"));
if(map.containsKey("1")) {
Student s1=(Student)map.get("1");//储存的是Student类内容
System.out.println(s1.getName());
}
}
}
- MapHash存储图书,通过编号获取名字
package test1;
import java.util.HashMap;
import java.util.Map;
class Book{
String title;
public Book(String title) {
this.title=title;
}
public String getTitle() {
return title;
}
public void setTitle() {
this.title=title;
}
}
public class test3 {
public static void main(String[] args) {
Map map=new HashMap();
map.put("1",new Book("jack"));
map.put("2",new Book("green"));
map.put("4",new Book("siri"));
if(map.containsKey("1")) {
Book s1=(Book)map.get("1");
System.out.println("the first:"+s1.getTitle());
}
}
}
- 读文件
package test1;
import java.io.*;
public class test3 {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
try {
in = new FileInputStream("C:\\Users\\yanghuiwen\\Desktop\\io.txt");
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
System.exit(-1);
}
try {
long num = 0;
while((b=in.read())!=-1){
System.out.print((char)b);
num++;
}
in.close();
System.out.println();
System.out.println("共读取了 "+num+" 个字节");
} catch (IOException e1) {
System.out.println("文件读取错误"); System.exit(-1);
}
}
}
- 文件复制
package test1;
import java.io.*;
public class test3 {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("C:\\Users\\yanghuiwen\\Desktop\\io.txt");
out = new FileOutputStream("C:\\Users\\yanghuiwen\\Desktop\\io1.txt");
long beginTime = System.currentTimeMillis();
while((b=in.read())!=-1){
out.write(b);
}
long endTime = System.currentTimeMillis();
long time = endTime-beginTime;
System.out.println("耗时"+time);
in.close();
out.close();
} catch (FileNotFoundException e2) {
System.out.println("找不到指定文件"); System.exit(-1);
} catch (IOException e1) {
System.out.println("文件复制错误"); System.exit(-1);
}
System.out.println("文件已复制");
} }
- 以字节流的缓冲区复制文件
package test1;
import java.io.*;
public class test3{
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("C:\\\\Users\\\\yanghuiwen\\\\Desktop\\\\io.txt");
out = new FileOutputStream("C:\\\\Users\\\\yanghuiwen\\\\Desktop\\\\io1.txt");
int len = 0;
byte[] buff = new byte[1024];
long beginTime = System.currentTimeMillis();
while ((len = in.read(buff)) != -1) {
out.write(buff,0,len);
}
long endTime = System.currentTimeMillis();
long time = endTime-beginTime;
System.out.println(time);
in.close();
out.close();
} catch (FileNotFoundException e2) {
System.out.println("找不到指定文件"); System.exit(-1);
} catch (IOException e1) {
System.out.println("文件复制错误"); System.exit(-1);
}
System.out.println("文件已复制");
}
}
- 字节缓冲流拷贝
package test1;
import java.io.*;
public class test3{
public static void main(String[] args) throws Exception {
// 创建用于输入和输出的字节缓冲流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\yanghuiwen\\Desktop\\io.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\yanghuiwen\\Desktop\\io1.txt"));
// 定义一个int类型的变量len
int len = 0;
// 获取拷贝文件前的系统时间
long beginTime = System.currentTimeMillis();
// 通过循环读取输入字节缓冲流中的数据,并通过输出字节缓冲流写入到新文件
while ((bis.read()) != -1) {
bos.write(len);
}
// 获取拷贝之后的系统时间
long endTime = System.currentTimeMillis();
// 输出拷贝花费时间
System.out.println("花费时间为:"+(endTime-beginTime) +"毫秒");
// 关闭流
bis.close();
bos.close();
}
}
- 字符流操作文件
package test1;
import java.io.*;
public class test3{
public static void main(String[] args) throws Exception {
// 创建一个FileReader对象用来读取文件中的字符
FileReader reader = new FileReader("C:\\\\Users\\\\yanghuiwen\\\\Desktop\\\\io.txt");
int ch; // 定义一个变量用于记录读取的字符
// 循环判断是否读取到文件的末尾
while ((ch = reader.read()) != -1) {
// 不是字符流末尾就转为字符打印
System.out.println((char) ch);
}
reader.close(); // 关闭文件读取流,释放资源
}
}
- 单线程创建和运行
package test1;
public class test3 {
public static void main(String[] args) {
MyThread myThread=new MyThread(); // 创建MyThread实例对象
myThread.run(); // 调用MyThread类的run()方法
for(int i=0;i<10;i++) //循环打印输出语句
System.out.println("Main方法在运行");
}
}
class MyThread {
public void run() {
for(int i=0;i<10;i++) { // 循环打印输出语句
System.out.println("MyThread类的run()方法在运行");
}
}
}
- 继承Thread创建多线程
package test1;
public class test3 {
public static void main(String[] args) {
MyThread myThread = new MyThread(); // 创建线程MyThread的线程对象
myThread.start(); // 开启线程
for(int i=0;i<10;i++) {
System.out.println("main()方法在运行");
}
}
}
class MyThread extends Thread {
public void run() {
for(int i=0;i<10;i++) {
System.out.println("MyThread类的run()方法在运行");
}
}
}
- Runnable接口创建多线程
package test1;
public class test3 {
public static void main(String[] args) {
MyThread myThread = new MyThread(); // 创建MyThread的实例对象
Thread thread=new Thread(myThread); // 创建线程对象
thread.start(); // 开启线程,执行线程中的run()方法
while (true) {
System.out.println("main()方法在运行");
}
}
}
class MyThread implements Runnable {
public void run() { // 线程的代码段,当调用start()方法时,线程从此处开始执行
while (true) {
System.out.println("MyThread类的run()方法在运行");
}
}
}
- 线程和优先级的调度
package test1;
class MaxPriority implements Runnable {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "正在输出:" + i);
}
}
}
// 定义类MinPriority实现Runnable接口
class MinPriority implements Runnable {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "正在输出:" + i);
}
}
}
public class test3 {
public static void main(String[] args) {
// 创建两个线程
Thread minPriority = new Thread(new MinPriority(), "优先级较低的线程");
Thread maxPriority = new Thread(new MaxPriority(), "优先级较高的线程");
minPriority.setPriority(Thread.MIN_PRIORITY); // 设置线程的优先级为1
maxPriority.setPriority(10); // 设置线程的优先级为10
// 开启两个线程
maxPriority.start();
minPriority.start();
}
}
- 设置线程休眠若干毫秒
package test1;
public class test3 {
public static void main(String[] args) {
Runner1 r = new Runner1();
Thread th = new Thread(r);
th.start();
} }
class Runner1 implements Runnable {
public void run() {
for(int i=1; i<=10; i++) {
System.out.println("线程 :" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- 成功卖票
package test1;
class Ticket1 implements Runnable {
private int tickets = 10; // 定义变量tickets,并赋值10
Object lock = new Object(); // 定义任意一个对象,用作同步代码块的锁
public void run() {
synchronized (lock) { // 定义同步代码块
while (tickets > 0) {
try {
Thread.sleep(100); // 经过此处的线程休眠100毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "---卖出的票"+ tickets--);
}
}
}
}
public class test3 {
public static void main(String[] args) {
Ticket1 ticket = new Ticket1(); // 创建Ticket1对象
// 创建并开启四个线程
new Thread(ticket, "线程一").start();
new Thread(ticket, "线程二").start();
new Thread(ticket, "线程三").start();
new Thread(ticket, "线程四").start();
}
}
- swing和awt比较
package test1;
import javax.swing.*;
import java.awt.*;
public class eg5 {
public static void main(String[] args) {
JFrame jf = new JFrame("Java swing");
jf.setLayout(null);
jf.setSize(250,200);
JButton jb = new JButton("swing button");
jb.setSize(100,60);
jb.setLocation(47,70);
jf.getContentPane().add(jb);
jf.setVisible(true);
Frame f = new Frame("Java awt");
f.setLayout(null);
Button b = new Button("awt Button");
f.add(b);
b.setLocation(47,70);
b.setSize(100,60);
f.setSize(250,200);
f.setVisible(true);}}
- 简单窗口创建
import javax.swing.*;
import java.awt.*;
public class FirstFrame {
public static void main(String[] args) {
JFrame jf = new JFrame("我的第一个窗体");
//jf.setSize(250,200);//窗口大小
jf.setBounds(250,200, 300, 200);//大小和位置
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOS
E);//中止程序
jf.setVisible(true);
} }
- 简单窗口2
import javax.swing.*;
import java.awt.*;
public class aa extends JFrame{
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton start,end = null;;
start = new JButton("start");
end = new JButton("end");
jf.add(start);
jf.add(end);
jf.setBounds(300, 300, 300, 200);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
}}
- Jpanel方法
import javax.swing.*;
import java.awt.*;
public class aa {
public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JButton start,end;
start = new JButton("start");
end = new JButton("end");
jp.add(start);
jp.add(end);
jf.add(jp);
jf.setBounds(300, 300, 300, 200);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
} }
- JScrollPane
import java.awt.*;
import javax.swing.*;
public class aa {
public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JTextArea textarea;
textarea = new JTextArea(3,15);
int rows = textarea.getRows();
textarea.insert("初始化文本域行数为"+rows+"行", 0);
JScrollPane sp = new JScrollPane(textarea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jp.add(sp);
jf.add(jp);
jf.setBounds(300, 300, 300, 200);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
}}
- 页面布局管理器
import java.awt.*;
import javax.swing.*;
public class tp1 {
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton jb1,jb2 ;
FlowLayout fset = new FlowLayout();
jb1 = new JButton("strat");
jb2 = new JButton("end");
jf.setLayout(fset);
jf.add(jb1);
jf.add(jb2);
jf.setSize(300,200);
jf.show();
}}
- GirdLayout
import java.awt.*;
import javax.swing.*;
public class tp1 {
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton jb1,jb2,jb3,jb4,jb5,jb6 ;
GridLayout gset = new GridLayout(3,2);
jb1 = new JButton("A");
jb2 = new JButton("B");
jb3 = new JButton("C");
jb4 = new JButton("D");
jb5 = new JButton("E");
jb6 = new JButton("F");
jf.setLayout(gset);
jf.add(jb1);jf.add(jb2);jf.add(jb3);
jf.add(jb4);jf.add(jb5);jf.add(jb6);
jf.setSize(300,200);
jf.show();
}}
- BorderLayout
import java.awt.*;
import javax.swing.*;
public class tp1 extends JFrame{
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton jb1,jb2,jb3,jb4,jb5,jb6 ;
BorderLayout bset = new BorderLayout();
jb1 = new JButton("A");
jb2 = new JButton("B");
jb3 = new JButton("C");
jb4 = new JButton("D");
jb5 = new JButton("E");
jb6 = new JButton("F");
jf.setLayout(bset);
jf.add(jb1,"North");jf.add(jb2,"Center");jf.add(jb3,"West");
jf.add(jb4,"East");jf.add(jb5,"South");
jf.setBounds(300, 300, 500, 300);
jf.show();
}}
- Button
import javax.swing.*;
public class ButtonTest {
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton start;
start = new JButton(“start”);
jf.add(start);
jf.setBounds(300, 300, 300, 200);
jf.setVisible(true);
}}
- JLable
public class Test2 {
public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JLabel jl ;
ImageIcon icon = new ImageIcon("E://1.jpg");
jl = new JLabel("标签",icon,JLabel.CENTER);
jp.add(jl);
jf.add(jp);
jf.setBounds(300, 300, 300, 200);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
} }
- 文本框JTextField
import javax.swing.*;
import java.awt.*;
public class tp1 {
public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JLabel jl ;
JTextField jtext;
jl = new JLabel("用户名:");
jtext = new JTextField("请输入用户名",10);
//jtext.setEnabled(false);
jp.add(jl);
jp.add(jtext);
jf.add(jp);
jf.setBounds(300, 300, 300, 200);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
}}
- 文本框JTestArea
import javax.swing.*;
import java.awt.*;
public class tp1 {
public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JTextArea textarea;
textarea = new JTextArea(3,10);
jp.add(textarea);
//int rows = textarea.getRows();
//textarea.insert("初始化文本域行数为"+rows+"行", 0);
jf.add(jp);
jf.setBounds(300, 300, 300, 200);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
}}
- 复选框
import java.awt.*;
import javax.swing.*;
import javax.swing.JCheckBox;
public class tp1 {
public static void main(String[] args) {
JFrame jf = new JFrame("选择爱好");
GridLayout glayout = new GridLayout(5,1);
JLabel label = new JLabel("你有什么爱好?");
JCheckBox box1,box2,box3,box4;
jf.setLayout(glayout);
box1 = new JCheckBox("唱歌");
box2 = new JCheckBox("打游戏");
box3 = new JCheckBox("篮球");
box4 = new JCheckBox("跳舞");
jf.add(label);
jf.add(box1);jf.add(box2);jf.add(box3);jf.add(box4);
jf.setBounds(300, 300, 300, 200);
jf.setVisible(true);
}}
- 单选框
import java.awt.*;
import javax.swing.*;
public class tp1 extends JFrame{
public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JLabel jl ;
JRadioButton[] jr = new JRadioButton[2];
jl = new JLabel("选择你的性别:");
jr[0] = new JRadioButton("男");
jr[1] = new JRadioButton("女");
//ButtonGroup group = new ButtonGroup();
jp.add(jl);
for(int i = 0;i<2;i++){
//group.add(jr[i]);
jp.add(jr[i]);
}
jf.add(jp);
jf.setBounds(300, 300, 300, 200);
jf.setVisible(true);
}}
- 下拉列表
import java.awt.*;
import javax.swing.*;
public class tp1 {
public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JLabel jl ;
JComboBox jc = new JComboBox();
jl = new JLabel("选择你的所在的城市:");
jc.addItem("北京");
jc.addItem("上海");
jc.addItem("郑州");
jc.addItem("武汉");
jc.addItem("青岛");
jp.add(jl);
jp.add(jc);
jf.add(jp);
jf.setBounds(300, 300, 300, 200);
jf.setVisible(true);
}}