练习
基础
实验报告(c++)
#include <iostream>
using namespace std;
class Stock
{
private:
int Number;
float Max,Min,Begin,End;
public:
Stock(int n,float ma,float mi,float b,float e);
Stock();
Stock(const Stock &s);
~Stock();
void Set_Stock(int n,float ma,float mi,float b,float e);
float Get_End();
void Show_Stock();
};
Stock::Stock(int n, float ma,float mi,float b,float e) {
Number = n;
Max = ma;
Min = mi;
Begin = b;
End = e;
cout << "构造函数使用中......" << endl;
}
Stock::Stock()
{
Number = 0;
Max = 0;
Min = 0;
Begin = 0;
End = 0;
cout << "不带参数的构造函数使用中......" << endl;
}
Stock::~Stock()
{
cout<<"析构函数使用中......ByeBye!"<<endl;
}
void Stock::Set_Stock(int n, float ma,float mi,float b,float e)
{
Number = n;
Max = ma;
Min = mi;
Begin = b;
End = e;
}
float Stock::Get_End()
{
return End;
}
void Stock::Show_Stock()
{
cout<<Number<<"\t";
cout<<Max<<"\t";
cout<<Min<<"\t";
cout<<Begin<<"\t";
cout<<End<<endl;
}
Stock::Stock(const Stock &s)
{
Max = s.Max;
Min = s.Min;
Begin = s.Begin;
End = s.End;
}
int main()
{
Stock s1(1, 4.18,4.00,4.05,4.09);
Stock s2(2,4.41,4.03,4.04,4.40);
cout<<"\t"<<(s2.Get_End()-s1.Get_End())/s1.Get_End()*100<< endl;
Stock s3(3,4.82,4.12,4.42,4.80);
Stock s;
Stock s4(s3);
return 0;
}
丢手绢游戏(建立链表)
package com.zhongjiamin.java;
public class bbb{
public static void main(String []args) {
CycLink cyl = new CycLink();
cyl.setLen(9);
cyl.createLink();
cyl.sta(2);
cyl.shu(2);
cyl.fin();
cyl.show();
}
}
class Child
{
int nom;
Child nextChild = null;
public Child(int nom)
{
this.nom = nom;
}
}
class CycLink
{
Child firstChild = null;
Child temp = null;
int len = 0;
int begin=0;
int far=0;
public void setLen(int len)
{
this.len = len;
}
public void createLink()
{
for(int i=1;i<=len;i++)
{
if(i==1)
{
Child ch = new Child(i);
this.firstChild = ch;
this.temp = ch;
temp.nextChild = this.firstChild;
}
else
{
if(i == len)
{
Child ch = new Child(i);
temp.nextChild = ch;
temp = ch;
temp.nextChild = firstChild;
}
else
{
Child ch = new Child(i);
temp.nextChild = ch;
temp = ch;
}
}
}
}
public void show()
{
Child temp = this.firstChild;
do {
System.out.println(temp.nom);
temp = temp.nextChild;
}while(temp != firstChild);
}
public void sta(int x)
{
this.begin = x;
}
public void shu(int x)
{
this.far = x;
}
public void fin()
{
Child temp = this.firstChild;
for(int i=1;i<begin;i++)
{
temp = temp.nextChild;
}
for(int j=1;j<far;j++)
{
temp = temp.nextChild;
}
Child temp1 = temp.nextChild;
temp.nextChild = temp1.nextChild;
}
}
主人喂食(多态、抽象)
package com.zhongjiamin.java;
public class bbb{
public static void main(String []args) {
Master master = new Master();
master.feed(new Dog(),new Bone());
master.feed(new Cat(),new Fish());
}
}
class Master
{
public void feed(Animal an,Food f)
{
an.eat();
f.showName();
}
}
abstract class Animal
{
int age;
String name;
abstract public void cry();
abstract public void eat();
}
class Cat extends Animal
{
abstract public void cry()
{
System.out.println("喵喵喵");
}
abstract public void eat()
{
System.out.println("猫爱吃鱼");
}
}
class Dog extends Animal
{
abstract public void cry()
{
System.out.println("汪汪汪");
}
abstract public void eat()
{
System.out.println("狗爱吃骨头");
}
}
class Food
{
String foodname;
public void showName()
{
System.out.println("不知道是什么吃的");
}
}
class Fish extends Food
{
public void showName()
{
System.out.println("鱼");
}
}
class Bone extends Food
{
public void showName()
{
System.out.println("骨头");
}
}
机器开关(接口)
从本质上讲,接口是一种特殊的抽象类,这种抽象类中只包含常量和方法的定义,
而没有变量和方法的实现。(接口的多态比较常用)
接口中的变量都是static的并且还是final,不能又private等修饰
接口中的变量必须被赋初值,即必须为一个常数,并且在主函数内可以直接用Usb.a进行访问
接口内的成员函数必须在类内被实现
package com.zhongjiamin.java1;
interface Aaa{
public void aaa();
}
interface Usb extends Aaa{
int a = 0; //接口中的变量都是static的并且还是final,不能又private等修饰
public void start();//接口中的变量必须被赋初值,即必须为一个常数,并且
public void stop();//在主函数内可以直接用Usb.a进行访问;
}
class Camera implements Usb{
public void start() {
System.out.println("我是相机,开始工作了!");
}
public void stop() {
System.out.println("我是相机,停止工作了!");
}
public void aaa()
{
}
}
class Phone implements Usb,Aaa{
public void start() {
System.out.println("我是手机,开始工作了!");
}
public void stop() {
System.out.println("我是手机 ,停止工作了!");
}
public void aaa()
{
}
}
class Computer{
public void useUsb(Usb usb) {
usb.start();
usb.stop();
}
}
public class shijianchuli{
public static void main(String []args) {
System.out.println(Usb.a);
Computer computer = new Computer();
Camera camera = new Camera();
Phone phone = new Phone();
computer.useUsb(camera);
computer.useUsb(phone);
}
}
final的用法
在java中,final的含义在不同的场景下有细微的差别,但总体上来说,它指的是“这是不可变的”。
final关键字是我们经常使用的关键字之一,它的用法有很多,但是并不是每一种用法都值得我们去广泛使用。它的主要用法有以下四种:
1、用来修饰数据,包括成员变量和局部变量,该变量只能被赋值一次且它的值无法被改变。对于成员变量来讲,我们必须在声明时或者构造方法中对它赋值;
2、用来修饰方法参数,表示在变量的生存期中它的值不能被改变;
3、修饰方法,表示该方法无法被重写(覆盖);(子类继承后不能覆盖)
4、修饰类,表示该类无法被继承。
面包厂买面包(综合)
package com.zhongjiam.java;
public class text {
public static void main(String[] args) {
//笑出猪叫王小明
Person wxm = new Person("wxm", 50);
//一点不慌快递员
Person gkd = new Person("gkd", 50);
//仓库
Stock sss = new Stock(20);
System.out.println("王小明余额:"+wxm.money);
System.out.println("快递员余额:"+gkd.money);
System.out.println("面包数量:"+sss.BreadNum);
int f1 = gkd.pay(49);
int f2 = wxm.get(49);
int f3 = sss.sell(49);
//此时快递员和王小明余额变化,但是面包数量因为没有49个扣费失败
//上面的三步应该是一个整体,要么都成功,要么都失败
System.out.println("王小明余额:"+wxm.money);
System.out.println("快递员余额:"+gkd.money);
System.out.println("面包数量:"+sss.BreadNum);
//所以应该这样
if(f1 < 0 || f2 < 0 || f3 < 0) {
//如果有一个事件失败,则全部将上一步的操作补偿回来,执行回滚函数
wxm.undo();
gkd.undo();
sss.undo();
}
System.out.println("王小明余额:"+wxm.money);
System.out.println("快递员余额:"+gkd.money);
System.out.println("面包数量:"+sss.BreadNum);
}
}
class Person{
String name;
int money;
int undoMoney;
public Person(String name, int money) {
this.money = money;
this.name = name;
}
public int pay(int x) {
if(this.money < x) {
this.undoMoney = x;
this.money -= x;
System.out.println("余额不足");
return this.money;
}else {
//记录当前的操作金额,为回滚做准备
this.undoMoney = (-1) * x;
this.money = this.money - x;
return this.money;
}
}
public int get(int y) {
//记录当前的操作金额,为回滚做准备
this.undoMoney = y;
this.money = this.money + y;
return this.money;
}
public void undo() {
//将上一步加上或者减去的金额补偿回来
this.money -= undoMoney;
}
}
class Stock{
int BreadNum;
int undoNum;
public Stock(int num) {
this.BreadNum = num;
}
public int sell(int num) {
if(this.BreadNum < num) {
this.undoNum = -num;
this.BreadNum -= num;
System.out.println("面包不够了");
return this.BreadNum;
}else {
this.undoNum = num;
this.BreadNum = this.BreadNum - num;
return this.BreadNum;
}
}
public void undo() {
//将操作造成的面包回滚回来
this.BreadNum = this.BreadNum - this.undoNum;
}
}
输入参数
package com.zhongjiam.java;
import java.util.Scanner;
public class text {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);//定义scanner,等待输入
System.out.println("请输入你的姓名:");
String name = in.nextLine();//字符类型的输入方式
System.out.println(name);
System.out.println("请输入你的年龄:");
int age = in.nextInt();//整数类型的输入方式
System.out.println(age);
System.out.println("请输入你的身高:");
double height = in.nextDouble();//小数类型的输入方式
System.out.println(height);
}
}
给三个人的投票系统(完全使用面向对象思想)
package com.zhongjiam.java;
import java.util.*;
public class text {
public static void main(String[] args) {
Person p1 = new Person(1,"张山");
Person p2 = new Person(2,"李四");
Person p3 = new Person(3,"王五");
Celect c = new Celect();
Scanner in = new Scanner(System.in);
int x = in.nextInt();
c.vote(x, p1, p2, p3);
int max = c.showBig(p1, p2, p3);
c.showName(max, p1, p2, p3);
}
}
class Person
{
private int num;
private String name;
private int all=0;
public Person(int num,String name)
{
this.name=name;
this.num=num;
}
public int returnNum()
{
return this.num;
}
public String returnName()
{
return this.name;
}
public void addALL()
{
this.all++;
}
public int returnAll()
{
return this.all;
}
}
class Celect
{
public void vote(int x,Person p1,Person p2,Person p3)
{
if(x==p1.returnNum())
p1.addALL();
if(x==p2.returnNum())
p2.addALL();
if(x==p3.returnNum())
p3.addALL();
}
public int showBig(Person p1,Person p2,Person p3)
{
int max = 0;
if(p1.returnAll()>max)
max = p1.returnAll();
if(p2.returnAll()>max)
max = p2.returnAll();
if(p3.returnAll()>max)
max = p3.returnAll();
return max;
}
public void showName(int max,Person p1,Person p2,Person p3)
{
if(p1.returnAll()==max)
System.out.println(p1.returnName());
if(p2.returnAll()==max)
System.out.println(p2.returnName());
if(p3.returnAll()==max)
System.out.println(p3.returnName());
}
}
查找数值(递归法,二分法)
package com.zhongjiam.java;
public class text {
public static void main(String[] args) {
int a[] = {3,2,1,6,5,4,9,8,7};
int left = 0;
int right = a.length;
int n = a.length;
Fin fin = new Fin();
fin.find(left, right, 8, a);
}
}
class Fin
{
public void find(int leftIndex,int rightIndex,int x,int a[])
{
if(leftIndex<=rightIndex)
{
int mindIndex = (leftIndex+rightIndex)/2;
int mindVal = a[mindIndex];
if(mindVal < x)
{
int left = mindIndex+1;
int right = rightIndex;
find(left,right,x,a);
}
else if(mindVal >x)
{
int left = leftIndex;
int right = mindIndex-1;
find(left,right,x,a);
}
else if(mindVal == x)
{
System.out.println("找到了,下标为:"+mindIndex);
}
}
}
}
集合
集合类及其特点
Collection(单列集合)
List 有序,可重复
ArrayList
优点: 底层数据结构是数组,查询快,增删慢。
缺点: 线程不安全,效率高
Vector
优点: 底层数据结构是数组,查询快,增删慢。
缺点: 线程安全,效率低
LinkedList
优点: 底层数据结构是链表,查询慢,增删快。
缺点: 线程不安全,效率高
Set 无序,唯一
HashSet
底层数据结构是哈希表。(无序,唯一)
如何来保证元素唯一性?
1.依赖两个方法:hashCode()和equals()
LinkedHashSet
底层数据结构是链表和哈希表。(FIFO插入有序,唯一)
1.由链表保证元素有序
2.由哈希表保证元素唯一
TreeSet
底层数据结构是红黑树。(唯一,有序)
1. 如何保证元素排序的呢?
自然排序
比较器排序
2.如何保证元素唯一性的呢?
根据比较的返回值是否是0来决定
Map无序
TreeMap是有序的
HashMap
是无序的
方法不是同步的
不是线程安全的
允许null值(key和value都允许)
效率较高
HashTable
是无序的
方法是同步的
线程安全的
效率较低
不允许null值
集合遍历的方法
//一、List集合
List<String> list = new ArrayList<>();
list.add("henrly");
list.add("nancy");
list.add("lucy");
list.add("jeacy");
//遍历List集合
//1.使用for循环
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//2.使用迭代器
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//3.使用增强for循环
for (String l : al) {
System.out.println(l);
}
//二、Set集合
Set<Integer> set = new TreeSet<>();
set.add(111);
set.add(222);
set.add(333);
set.add(444);
//遍历Set集合
//Set集合无索引,所以无法使用for循环遍历
//1.使用迭代器
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//2.使用增强for循环
for (Integer i : set) {
System.out.println(i);
}
//三、Map集合
HashMap<Integer, String> hmap = new HashMap<>();
hmap.put(17, "h_nancy");
hmap.put(18, "h_jerry");
hmap.put(10, "h_lucy");
hmap.put(12, "h_henrly");
TreeMap<Integer, String> map = new TreeMap<>();
map.put(17, "nancy");
map.put(18, "jerry");
map.put(10, "lucy");
map.put(12, "henrly");
//遍历HashMap集合
//Map集合的遍历使用keySet和entrySet
//1.使用keySet
for (Integer key : hmap.keySet()) {
System.out.println(key + "----" + hmap.get(key));
}
System.out.println("------------------------------------------------");
//2.使用entrySet
for (Map.Entry<Integer, String> entry : hmap.entrySet()) {
System.out.println(entry);
}
//遍历HashMap集合
//Map集合的遍历使用keySet和entrySet
******//1.使用keySet
for (Integer key : map.keySet()) {
System.out.println(key + "----" + hmap.get(key));
}
System.out.println("------------------------------------------------");
//2.使用entrySet
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "---" + entry.getValue());
}
员工管理系统(Arrylist(动态数组))
package com.zhongjiam.java;
import java.util.*;
import java.util.Scanner;
public class text1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
EmpManage manage = new EmpManage();
while(true)
{
System.out.println("请选择你需要的服务:");
System.out.println("1.添加成员");
System.out.println("2.展示员工信息");
System.out.println("3.修改薪水");
System.out.println("4.删除成员");
System.out.println("5.按薪资排序(暂无)");//还不会
System.out.println("6.统计平均,最低,最高工资");
System.out.println("7.显示全体成员");
System.out.println("8.退出");
Scanner in = new Scanner(System.in);
int x=in.nextInt();
if(x==1)
{
String name,nom;
float sal;
System.out.println("请输入姓名:");
name = in.next();
System.out.println("请输入编号:");
nom = in.next();
System.out.println("请输入薪水");
sal = in.nextFloat();
Emp emp = new Emp(name,nom,sal);
manage.addEmp(emp);
}
else if(x==2)
{
System.out.println("请输入工号:");
String nom = in.next();
manage.showInfo(nom);
}
else if(x==3)
{
System.out.println("请输入工号:");
String nom = in.next();
System.out.println("请输入工资:");
float salar = in.nextFloat();
manage.updateSal(nom,salar);
}
else if(x==4)
{
System.out.println("请输入工号:");
String nom = in.next();
manage.delEmp(nom);
}
else if(x==5)
{
manage.orderSal();
}
else if(x==6)
{
manage.showSal();
}
else if(x==7)
{
manage.showAllInfo();
}
else if(x==8)
{
System.out.println("欢迎下次使用");
break;
}
}
}
}
class EmpManage
{
private ArrayList al=null;
public EmpManage()
{
al=new ArrayList();
}
public void addEmp(Emp emp)
{
al.add(emp);
}
public void showSal()
{
float sum=0f,equal=0f,max=0f,min=1000000f;
for(int i=0;i<al.size();i++)
{
Emp emp =(Emp)al.get(i);
if(emp.getSalar()>max)
max = emp.getSalar();
if(emp.getSalar()<min)
min = emp.getSalar();
sum += emp.getSalar();
}
equal = sum/al.size();
System.out.println("最高工资:"+max);
System.out.println("最低工资:"+min);
System.out.println("平均工资:"+equal);
}
public void orderSal()
{
for(int i=0;i<al.size();i++)
{
Emp emp =(Emp)al.get(i);
}
}
public void showInfo(String empNom)
{
for(int i=0;i<al.size();i++)
{
Emp emp =(Emp)al.get(i);
if(emp.getNom().equals(empNom))
{
System.out.println("已找到该员工,其信息为:");
System.out.println("名字:"+emp.getName());
System.out.println("工号:"+emp.getNom());
System.out.println("薪水:"+emp.getSalar());
}
}
}
public void showAllInfo()
{
for(int i=0;i<al.size();i++)
{
Emp emp =(Emp)al.get(i);
System.out.println("已找到该员工,其信息为:");
System.out.println("名字:"+emp.getName());
System.out.println("工号:"+emp.getNom());
System.out.println("薪水:"+emp.getSalar());
}
}
public void updateSal(String empNom,float newSal)
{
for(int i=0;i<al.size();i++)
{
Emp emp =(Emp)al.get(i);
if(emp.getNom().equals(empNom))
{
emp.setSalar(newSal);
}
}
}
public void delEmp(String empNom)
{
for(int i=0;i<al.size();i++)
{
Emp emp =(Emp)al.get(i);
if(emp.getNom().equals(empNom))
{
al.remove(i);
}
}
}
public void ordSalEmp()
{
}
}
class Emp
{
private String name;
private String nom;
private float salar;
public Emp(String name,String nom,float salar)
{
this.nom=nom;
this.name=name;
this.salar=salar;
}
public void setName(String name)
{
this.name=name;
}
public void setNom(String nom)
{
this.nom=nom;
}
public void setSalar(float salar)
{
this.salar=salar;
}
public String getName()
{
return this.name;
}
public String getNom()
{
return this.nom;
}
public float getSalar()
{
return this.salar;
}
}
泛型的应用
package com.zhongjiam.java;
import java.lang.reflect.Method;
public class text {
public static void main(String[] args) {
List<Bird> al = new ArrayList<Bird>()
Bird bird1 = new Bird();
Bird temp = al.get(0); //不用强转(Bird)
Gen<Bird> gen1 = new Gen<Bird>(new Bird());
gen1.showTypeName();
}
}
class Bird
{
public void cry()
{
System.out.println("gegege");
}
public void eat()
{
System.out.println("grass");
}
}
class Gen<T>
{
private T o;
public Gen(T o)
{
this.o=o;
}
public void showTypeName()
{
System.out.println("类型是:"+o.getClass().getName()); //得到变量的类型名
Method m[]=o.getClass().getDeclaredMethods(); //得到全部方法名
for(int i=0;i<m.length;i++ )
{
System.out.println(m[i].getName());
}
}
}
集合的增删查改
ist的增删改查
package 集合;
import java.util.*;
public class List {
public static void main(String[] args) {
//增加
ArrayList list= new ArrayList();
list.add("1 liuwei");
list.add("2 longqun");
list.add(2, "longpan");
System.out.println(list);
ArrayList sublist = new ArrayList();
sublist.add("刘德华");
sublist.add("殷俊");
list.addAll(1, sublist);
System.out.println(list);
//删除
//删除指定元素
list.remove("刘德华");
System.out.println(list);
//根据索引删除
list.remove(2);
list.remove(2);
System.out.println(list);
//批量删除
list.removeAll(sublist);
System.out.println(list);
//修改
list.set(0, "leilong");
System.out.println(list);
//查看
ArrayList li = new ArrayList();
li.add("王晶");
li.add("关之琳");
li.add("罗艺");
li.add("王晶");
list.addAll(li);
list.add("1小虎");
//根据索引查看
System.out.println(list.get(1));
System.out.println(list);
//显示第一次出现的位置
System.out.println(list.indexOf("1小虎"));
//显示最后一次出现的位置
System.out.println(list.lastIndexOf("王晶"));
//查看指定元素最后一个不显示
System.out.println(list.subList(1, 3));
}
}
2.set的增删改查
package 集合;
import java.util.*;
public class Set {
public static void main(String[] args) {
HashSet hs = new HashSet();
//增加
HashSet s =new HashSet();
s.add("雨");
s.add("暴风");
hs.add("剑雨");
hs.add("冰");
hs.addAll(s);
hs.add("旱");
System.out.println(hs);
//删除
hs.remove("旱");
hs.removeAll(s);
System.out.println(hs);
//修改
hs.add("han");
System.out.println(hs);
TreeSet ts=new TreeSet();
ts.add("1春");
ts.add("2夏");
ts.add("3秋");
ts.add("4冬");
System.out.println(ts);
//显示第一个
System.out.println(ts.first());
//显示最后一个
System.out.println(ts.last());
System.out.println(ts.subSet("2夏","4冬"));
//删除
ts.remove("4冬");
System.out.println(ts);
}
}
3.map的增删改查
package 集合;
import java.util.*;
import java.util.Set;
public class Map {
public static void main(String[] args) {
HashMap hm = new HashMap();
//增加
hm.put(1, "太阳");
hm.put(2, "海洋");
hm.put(3, "大地");
System.out.println(hm);
//删除
hm.remove(2);
System.out.println(hm);
//修改
hm.put(3, "冰雪");
System.out.println(hm);
Set kt = hm.keySet();
for(Object s:kt){
System.out.println(hm.get(s));
}
System.out.println(hm.get(3));
TreeMap tm = new TreeMap();
//增加
tm.put(1, "提莫");
tm.put(2, "皮城");
System.out.println(tm);
//删除
tm.remove(2);
System.out.println(tm);
//修改
tm.put(1, "寒冰");
System.out.println(tm);
}
}
4.linkedlist的增删改查
package 集合;
import java.util.*;
public class LinkedListDemo{
public static void main(String[] args) {
//创建一个集合
LinkedList ld = new LinkedList();
//增加
ld.add("上");
ld.add("下");
ld.add("左");
ld.add("右");
System.out.println(ld);
//删除
ld.remove("下");
System.out.println(ld);
//修改
ld.set(2, "宇宙");
System.out.println(ld);
}
}
罗马数字转整型
package com.zhongjiam.java;
import java.util.*;
import java.lang.reflect.Method;
public class text {
public static void main(String[] args) {
Map<Character,Integer> hm = new HashMap<>();
hm.put('I',1);
hm.put('V',5);
hm.put('X',10);
hm.put('L',50);
hm.put('C',100);
hm.put('D',500);
hm.put('M',1000);
Scanner in = new Scanner(System.in);
String n = in.next();
char a[] = n.toCharArray();
int sum=0;
for(int i=0;i<a.length;i++)
{
if((i<a.length-1)&&(hm.get(a[i])<hm.get(a[i+1])))
{
sum = sum-hm.get(a[i]);
}
else
sum = sum+hm.get(a[i]);
}
System.out.println(sum);
}
}
异常的查找、处理
FileReader fr = mull;
try{
FileReader fr = new FileReader("d:\\aaa.txt");
}catch(Exception e){
e.printStackTrace();
}finally{
//无论try有无异常都执行
//一般把必须执行的,需要关闭的资源(文件,链接,内存)放在这
if(fr!=null)
{
try{
fr.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
类创建子类构建函数
package com.zhongjiam.java;
import java.util.*;
import java.io.*;
public class text1 {
public static void main(String[] args) {
Kkk kk = new Kkk(100,"zjm");
}
}
class Base
{
int age;
String name;
public Base(int age,String name)
{
this.age=age;
this.name=name;
}
}
class Kkk extends Base
{
public Kkk(int age,String name)
{
super(age,name);
System.out.println(age);
}
}
窗口
窗口、按键建立
package com.zhongjiam.java;
//import java.util.*;
import java.awt.*;
import javax.swing.*;
public class text1 extends JFrame{
//swing组件的定义
JButton jb1 = null;
public static void main(String[] args) {
text1 t1 = new text1(); //创建容器
}
public text1()
{
jb1 = new JButton("毁灭世界");//创建按钮
this.setTitle("hello world");//创建窗口标题
this.setSize(500,500);//窗口大小
this.add(jb1);//把按键放入窗口中
this.setLocation(100, 200);//窗口初始位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口默认关闭方式
this.setVisible(true);//窗口显示
}
}