五,图形界面编程

五,图形界面编程

一,图形界面编程swt;

1,awt

1,图形类 Button

2,辅助类

1,工具类

2,字体类

3,颜色类

2,Swing

1,图形类 jButton

二,容器与组件

1,首层容器 Contoner

1,所有的中间容器和组件都放在首层容器里

2,常用的首层容器

JApplet-动画效果-过时

Jdiologo-弹出框

Jframe-常用的首层容器

Jwindow

3,先new jframe对象,在设置宽高 ,ste size(宽,高);

内宽高,先水平在垂直先宽后高,单位像素

4,设置窗体大小为可不可变 setResizable(false);

5,设置外边框 setLocation(x,y);

内宽高,先水平在垂直先宽后高,单位像素

6,工具类的调用;Toolkit tk = Toolkit.getDefaultToolkit();

tk.getScreenSize().getWidth();获取当前系统的像素宽度,

getScreenSize().getHeight();获取当前系统的像素高度

以上两个返回类型是double类型的

7,设置标题;this.setTitle("我的第一个窗体");

8,设置图标;this.setIconImage(tk.createImage("img/hp.JPG"));

9,设置窗体关闭,程序即结this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

10,设置窗体可见(放最后);this.setVisible(true);

2,中间容器

1,中间容器可以放中间容器和japanl面板

2,获取内容面板(JFrame自带;this.con = this.getContentPane();

3,改变内容面板背景色,改JFrame背景没有效果this.con.setBackground(Color.WHITE)

4,布局管理器;(有布局管理器主动设置组件大小是没有用的)

1,空布局,绝对定位,要把默认的布局管理器设为空this.con.setLayout(null);

2,边界布局管理器---BorderLayout;

this.con.setLayout(new BorderLayout());

this.con.add(两个参一个是数据,位置上下左右);

特点:

1、将整个容器划分为5个部分,口诀:东西南北中,南北要贯通,中间最大;

2、中间最大不仅指中间区域最大,也指中间权利最大。当周边不存在的时候,

中间会去占领周边;中间不存在,周边是不能占领中间的;

3、周边面积要扩大,不能靠直接设置大小和位置,

只能靠增减内容把区域撑起来----不能靠空布局来撑

4、BorderLayout是JFrame的默认的布局管理器

适用场景:

其特点决定,边界布局管理器就不是用来直接放置组件

(只有极少的情况需要一个组件占据东西南北中某个区域)

它更多的用途是用来在每个区域放置中间容器的,

即使用在面板嵌套设计当中。

边界布局中间可以使用空布局

3,流布局管理器---FlowLayout; this.consetLayout(new FlowLayout())

特点:

1、自动按照规则控制组件的位置和大小

2、位置:从上往下、从左往右、由中间开始的方式依次排列组件

3、大小:由组件内容确定

4、我们常用的中间容器JPanel的默认布局管理器是FlowLayout。

使用场景:

1、一般专用于放置组件;

2、最好是单行,依次放置的情况

4,网格布局管理器---GridLayout;

this.con.setLayout(new GridLayout(2,2));

特点:

1、把容器按行列划分为等大的几个区域;

2、根据add的顺序决定组件放在哪个区域,

先保证一行放满了,在放第二行;

3、大小是自动填充整个网格;

4、如果网格的数目和add的数目不一致,会保证行不变。

适用场景:

1、类似于边界布局,主要用来放置中间容器的;

2、在划分等大的情况下使用

5,卡片布局CardLayout;

this.con.setLayout(new CardLayout());

this.con.add(两个参,第一个数据,第二个是名字);

1,卡片布局代码格式;

CardLayout card = (CardLayout)frame.getUpP().getLayout();

card.first(frame.getUpP());//到第一页

card.previous(frame.getUpP());//上一页

card.next(frame.getUpP());//下一页

card.last(frame.getUpP());//到最后一页

2,根据名字跳转

CardLayout card = (CardLayout)TakeP.this.atm.getContentPane().getLayout();

card.show( TakeP.this.atm.getContentPane(), "选择");

3,组件 Compamet

1,组件有;

1,标签 JLabel

2,文本框 JTextField

3,密码框 JPasswordField

4,单选框 JRadioButton

5,复选框 JCheckBox

6,按钮 JButton

7,下拉列表 JComboBox

8,文本域 JTextArea

2,完成一个组件的放入分4步:

1、new出组件对象;

this.usernameLab = new JLabel();

2、调整组件细节属性(这个动作可以最后边看效果边调整)

this.usernameLab.setText("成都朗沃教育网站的用户名:");写入信息

this.usernameLab.setFont(new Font("隶书",Font.ITALIC,20))

设置标签字体大小,字体样式一般都有常量定义;

this.usernameLab.setForeground(Color.BLUE);设置前景色

this.usernameLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));

设置组件边框,和边框颜色,

3、在空布局的情况下,必须自定义位置和大小

this.usernameLab.setBounds(100, 5, 300, 30);

设置组件的位置/大小,必须保证容器的布局管理器为空,这句代码才起作用。

4、把组件添加到容器中

this.con.add(this.usernameLab);//放入

5,其他设置;

this.pwdTxt.setEchoChar('*');设置密码框的字符

this.xialaTab.setEditable(true);设置选择框可否直接输入

ButtonGroup b =new ButtonGroup();在放入组件之前放入

this.stateComb.setEnabled(false);设置禁用

this.okBtn.setIcon(new ImageIcon("img/hp.JPG"));点击前的图片

this.okBtn.setRolloverIcon(new ImageIcon("img/logo.gif"));点击后的图片

JScrollPane sp = new JScrollPane(this.inputArea);设置文本框滚动条

三,嵌套

1,把复杂界面划分成多个简单页面进行组合

2,面板嵌套,下面的页面集成JPanel面板

四,事件;

1,事件处理

1,java采用了委托事件模型

2,监听器对象绑定Gui对象。

3,不同的事件类型有不同的监听器

4,一个GUI对象身上可以绑定多个不同的监听器

5,一个监听器也可以绑定多个GUI对象

2,监听器的实现方法一

1,书写一个单独的类来实现监听接口

2,把要实现发生在事件后的要做的操作在相应的方法中实现

3,产生一个监听器对象调用事件源listenet()来进行绑定。

this.addBtn.addActionListener();

4,可以书写一个监听器同时监听多个事件源对象,

也可以为不同的事件源对象书写各自的监听对像

5,根据单一职责推荐使用为不同的事件源书写各自的监听器

6,如果要在响应过程中操作非事件源对象就需要将其传进来

优点;

可以满足单一职责;

缺点;

满足单一职责,会带来类的数量增加,如果要操作大量的非事件源对象就要大量传参

3,监听器的实现方法二

让容器类当监听器

优点;

没有单独的类,

访问当前容器的任何一个非事件源都不用传参,

缺点;

违背了单一职责

4,监听器的实现方法三

在绑定监听器对象同时实现监听器接口(匿名内部类)

优点;

1,没有独立的java文件存在维护量不大

2,只能为每个事件源书写独立的监听,满足了单一原则

缺点;

语法不太熟悉

 

5,代码格式;

this.zuoAnLiuJbu.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

YeMian a=(YeMian) ZhaoPian.this.fanYeyemian.getLayout();

//获取YeMian对象

}

});

String text=TestFrame1.this.usernameTxt.getText();//返回文本框的类容

String text=TestFrame1.this.usernameTxt.setText();//写入如文本框

TestFrame1.this.usernameTxt.setText(null);//将文本框设为空

Testzuoye2.this.okBtn.setEnabled(false);//禁止按钮使用

Testzuoye2.this.imageLab.setIcon(new ImageIcon("img/1.png"));

//替换图片标签的图片

Testzuoye2.this.okBtn1.setText("Again");//给按键更名

代码笔记1

public class TestFrame extends JFrame{

private Container con;

private JLabel usernameLab;//文字标签

private JLabel imageLab;//图片标签

private JTextField usernameTxt;//文本框

private JPasswordField pwdTxt;//密码框

private JButton okBtn;//按钮

private JButton imageBtn;//图片按钮

private JComboBox stateComb;//下拉框

private JRadioButton maleRad;//单选框

private JRadioButton femaleRad;

private JCheckBox readChc;//复选框

private JCheckBox movieChc;

private JCheckBox musicChc;

private JTextArea inputArea;//文本域(多行文本输入框)

public TestFrame(){

Toolkit tk = Toolkit.getDefaultToolkit();//工具箱类

this.setSize(500, 300);//设置窗体大小

this.setResizable(false);//设置窗体大小不可变

this.setLocation(((int)tk.getScreenSize().getWidth()-500)/2,

((int)tk.getScreenSize().getHeight()-300)/2);//设置窗体位置

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭,程序即结束

this.setTitle("我的第一个窗体");//设置标题

this.setIconImage(tk.createImage("img/hp.JPG"));//设置图标

this.addContent();

this.setVisible(true);//设置窗体可见(放最后)

}

private void addContent(){

this.con = this.getContentPane();//获取内容面板(JFrame自带)

this.con.setBackground(Color.WHITE);//改变内容面板背景色(改JFrame背景没有效果)

this.con.setLayout(null);//把容器自带的布局管理器设为空,使用空布局(绝对定位)

//完成一个组件的放入分4步:

//1、new出组件对象;

this.usernameLab = new JLabel();

//2、调整组件细节属性(这个动作可以最后边看效果边调整)

this.usernameLab.setText("成都朗沃教育网站的用户名:");

this.usernameLab.setFont(new Font("隶书",Font.ITALIC,20));

this.usernameLab.setForeground(Color.BLUE);

// this.usernameLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));

//3、在空布局的情况下,必须自定义位置和大小

this.usernameLab.setBounds(100, 5, 300, 30);//设置组件的位置/大小,

必须保证容器的布局管理器为空,这句代码才起作用。

//4、把组件添加到容器中

this.con.add(this.usernameLab);//放入

this.imageLab = new JLabel();

this.imageLab.setIcon(new ImageIcon("img/tx.JPG"));

this.imageLab.setBounds(5, 30, 88, 86);

this.con.add(this.imageLab);

this.usernameTxt = new JTextField();

this.usernameTxt.setFont(new Font("宋体",Font.PLAIN,30));

this.usernameTxt.setBounds(100, 40, 200, 30);

this.con.add(this.usernameTxt);

this.pwdTxt = new JPasswordField();

this.pwdTxt.setFont(new Font("宋体",Font.PLAIN,30));

this.pwdTxt.setEchoChar('*');

this.pwdTxt.setBounds(100, 80, 200, 30);

this.con.add(this.pwdTxt);

this.okBtn = new JButton("确定");

this.okBtn.setIcon(new ImageIcon("img/hp.JPG"));

this.okBtn.setRolloverIcon(new ImageIcon("img/logo.gif"));

this.okBtn.setFont(new Font("宋体",Font.PLAIN,24));

this.okBtn.setForeground(Color.GREEN);

this.okBtn.setBounds(320, 40, 140, 30);

this.con.add(this.okBtn);

this.imageBtn = new JButton(new ImageIcon("img/buttonClear.jpg"));

this.imageBtn.setBounds(320, 80, 140, 50);

this.con.add(this.imageBtn);

this.stateComb = new JComboBox(new String[]{"山东","河南","湖北"});

this.stateComb.addItem("四川");

this.stateComb.setEditable(true);//设置可编辑

// this.stateComb.setEnabled(false);//设置禁用

this.stateComb.setBounds(10, 150, 100, 30);

this.con.add(this.stateComb);

this.maleRad = new JRadioButton("男");

this.femaleRad = new JRadioButton("女");

ButtonGroup bg = new ButtonGroup();

bg.add(this.maleRad);

bg.add(this.femaleRad);

this.maleRad.setSelected(true);

this.maleRad.setBounds(10, 190, 50, 30);

this.femaleRad.setBounds(100, 190, 50, 30);

this.con.add(this.maleRad);

this.con.add(this.femaleRad);

this.readChc = new JCheckBox("阅读");

this.movieChc = new JCheckBox("电影");

this.musicChc = new JCheckBox("音乐");

ButtonGroup bg0 = new ButtonGroup();

bg0.add(this.readChc);

bg0.add(this.movieChc);

bg0.add(this.musicChc);

this.movieChc.setSelected(true);

this.readChc.setBounds(10, 220, 60, 30);

this.movieChc.setBounds(80, 220, 60, 30);

this.musicChc.setBounds(150, 220, 60, 30);

this.con.add(this.readChc);

this.con.add(this.movieChc);

this.con.add(this.musicChc);

//文本域默认不带外边框、滚动条

this.inputArea = new JTextArea();

JScrollPane sp = new JScrollPane(this.inputArea);

sp.setBounds(220, 150, 270, 100);

this.con.add(sp);

}

}

lst.add("String");

lst.add(new Date());

lst.add(new StudentBean("小白", 25, 70));

lst.add(250);//注意:这是自动封箱,放进去的是一个Integer对象。

System.out.println(lst.size());//得到集合中元素个数

3,删

lst.remove(0);//根据下标进行删除

lst.remove("String");//根据对象进行删除

4,改

lst.set(1, "SSSS");//根据下标完成修改

5,查

System.out.println(lst.get(0));//get方法默认返回的是Odject

6,遍历---掌握1和2,认识3

1、普通for循环

for(int i = 0; i < lst.size(); i++){

System.out.println(lst.get(i));

}

2for-each 循环---JDK1.5以后才有的功能

for(Object obj : lst){

System.out.println(obj);

}

3,迭代器---Iterator---以前主要用于没有下标的集合类完成遍历,现在被for-each替代

数组不能用---只有集合能用

Iterator it = lst.iterator();

产生和集合对象相关联的迭代器对象

while(it.hasNext()){

Object obj = it.next();

System.out.println(obj);

}

二,Set---集,不能存放重复元素;非线性,即无序,没有下标。

1,特点;

不能存放重复元素!以无序的方式存放!无下标

掌握:HashSet

1、提供了增、删除的行为,删除也只能按对象删除

2、没有修改和查询指定元素的行为(因为没有下标)

3、不接受普通for循环。

2,set集合如何判断两个元素重复:

1、它不是判断两个元素是否是同一个对象!

2、而是先调用两个对象的hashcode方法,判断是否返回同一个值;

然后再调用equals方法判断是否返回true;

----所以根据规范,重写equals方法,同时也要重写hashcode方法

同样支持泛型

HashSet<Integer> set = new HashSet<Integer>();

HashSet set = new HashSet();

3,增

set.add("String");

set.add(new Date());

set.add(new StudentBean("小样儿", 18, 70));

set.add(250);

System.out.println(set.size());//获取元素个数

set.add(new String("String"));//不能存放重复元素

set.add(new StudentBean("小样儿", 18, 70));

System.out.println(set.size());

4,删---因为没有下标,所以只能根据对象来进行删除

set.remove(new String("String"));

System.out.println(set.size());

5,改---没有提供修改某个指定元素的方法

6,查---没有获取某个指定元素的方法

7,遍历---不能用普通for

1、for-each

for(Object obj : set){

System.out.println(obj);

}

2、迭代器

Iterator it = set.iterator();

while(it.hasNext()){

System.out.println(it.next());

}

三,Map---映射---特点:以键值对的方式存放元素

1,特点;

不能存放重复元素,无序的方式存放!无下标,所有数据以key---Value的方式存放

hashmap 常用

2,掌握:HashMap的增删查改遍历的API。

补充掌握:Properties操作文件的方法

辨析:

HashMap、Hashtable

1、两者都是Map分支的实现类,拥有相同的API;

2、Hashtable是线程安全的,效率低;HashMap是线程不安全的,效率高

3、Hashtable不允许使用null做键或值,但是HashMap是允许的

(所有键中只能有一个为null)

HashMap<String, StudentBean> map = new HashMap<String, StudentBean>();

Hashtable<String, StudentBean> map = new Hashtable<String, StudentBean>();

3,增---必须保证键不一样

map.put("J136001", new StudentBean("乾隆",24,80));

map.put("J136002", new StudentBean("程骁",28,75));

map.put(null, new StudentBean());

map.put("J136004", null);

System.out.println(map.size());//获取元素个数

4,删---根据键进行删除,如果键不存在不会报异常只会删除失败

map.remove("J136003");

System.out.println(map.size());

5,改---仍然调用put方法,修改传入键所对应的值

map.put("J136002", new StudentBean("王超",32,70));

System.out.println(map.size());

6,查---根据键查找值

StudentBean stu = map.get("J136002");

7,遍历---不能直接遍历Map,只能单独遍历Map的所有键或者所有值

所以在遍历前,要得到所有的键,或所有的值

Set<String> keys = map.keySet();//得到所有的键

Collection<StudentBean> values = map.values();//得到所有的值

1、for-each

for(String key : keys){

System.out.println(key);

}

for(StudentBean tmpStu : values){

System.out.println(tmpStu.getName());

}

2、Iterator

Iterator<String> keyIt = keys.iterator();

while(keyIt.hasNext()){

System.out.println(keyIt.next());

}

Iterator<StudentBean> stuIt = values.iterator();

while(stuIt.hasNext()){

System.out.println(stuIt.next().getName());

}

8,Hashtable和HashMap的区别

1,HashMap;

线程不安全效率高

可以用null做键和值

2,Hashtable;

线程安全效率低

不可以用null做键和值

 

五,泛型---限制集合只能存放某种元素的。

HashMap<String, StudentBean> map = new HashMap<String, StudentBean>();

<String, StudentBean>

集合框架类的在默认情况下是可以操作任意数据类型的(Object),这本来是

集合要解决的数组三大问题之一。但是在实际操作中,往往我们会把相同数据类型

放到一起做统一处理。----泛型就是用来限制某个集合对象,只能存放某种类型

的元素。

六,拼接接口Comparable从写里边的方法设定排序

当第1个对象根据排序规则所决定的位置 小于 第2个对象根据规则所决定的位置,返回-1

当第1个对象根据排序规则所决定的位置 大于 第2个对象根据规则所决定的位置,返回1

当第1个对象根据排序规则所决定的位置 和 第2个对象根据规则所决定的位置 保持相等,返回0

collections是工具类

int max = Collections.max(lst);

int min = Collections.min(lst);

System.out.println("最大值:" + max);

System.out.println("最小值:" + min);

Collections.sort(lst);//自动按照自然排序(即元素类型自带的内部比较规则)进行排列

Collections.reverse(lst);//对List集合元素进行反转

Collections.shuffle(lst);//混排---随机排序

@Override

public int compare(StudentBean stu1, StudentBean stu2) {

if(stu1.getScore() > stu2.getScore()){

return -1;

}else if(stu1.getScore() < stu2.getScore()){

return 1;

}else{

if(stu1.getAge() < stu2.getAge()){

return -1;

}else if(stu1.getAge() > stu2.getAge()){

return 1;

}else{

return 0;

}

}

七,其他

1,float c=Float.parseFloat(values[2]);字符串转flot

 

原文地址:https://www.cnblogs.com/zpcbk/p/6487085.html