第四章 桌面应用程序开发起步
1 swing基础
1.1常用的组件:
JFrame( 框架窗口)JDialog(对话框) JPanel(面板) ButtonGroup(按钮组) Timer(定时器) ImageIcon(图标)JOptionPane(选项对话框) JColorChooser(颜色选择对话框) JFileChooser (文件选择对话框)JScrollPane(滚动窗格) JSpitPane(分割窗格)JTabbedPane(选项窗格) JScrollBar(滚动条) JLabel (标签)JTextField (文本行)JTextArea(文本区)JPasswordField(密码文本行) JButton(按钮) JRadioButton(单选按钮) JCheckBox(复选框) JList (列表框)JComboBox(组合框)JEditorPane(编辑窗格) JSpinner(微调文本框) JTable (表格)JTree (树)JDeskTopPane(桌面窗格) JInternalPane(内部框架)
1.2
一个界面首先是一个JFrame对象,即一个窗体;然后我们就可以这个窗体上放置其它元素组件.
1.3
Frame是属与容器类组件的,它有一个万能的add(Component comp)---Swing中所有继承与javax.swing.Component的对象都可以加到一个Frame上
1.4
必须在放置元素组件前,设置窗体的“布局管理器对象”,设置了布局管理器后,元素的组件就按布局管理器的规则进行排放,Java中有多种布局管理器,都是Java.awt.LayoutManager接口的子类,常用的实现类有Java.awt.FlowLayout,即流式布局管理器
1.5
所以它们都有共同性质和作同的一些方法,这些方法都具有统一的命名规则以至与你只要看到方法名字就知道是什么用途---你的类中的方法也要这样!设置组件对象属性的方法:set<要设置的组件的属性值>(设置的值类型 值)。取得组件的属性值类型 get<要设置的组件的属性值>()。取得组件boolean型属性值的方法:boolean is<组件的属性值>()。在组件上加上一个组件的方法:void add<要加的组件类型>(类型 组件名)。
2 事件机制
2.1 事件的概念:
事件处理是界面处理的基本功能,当用用点击鼠标或按下键盘时,Swing界面上获到焦点的组件都会收到一个事件通知,这个事件通知是Swing体系内部发出的,界面就会根据收到的2.2事件处理流程:
一个事件处理流程由三部分组成:事件源,即焦点所在的组件(或者说接收事件的对象)、事件对象(按键事件还是鼠标单击事件)、感兴趣的eventListener(事件处理器对象),和事件处理过程。
2.2事件的处理方法
2.2.1系统己为我们提供了常见的事件通知接口,我们的任务主要是实现相应组事的事件接口,比如所有的元素组件,都有一个.addActionListener(java.awt.ActionListener listener) 方法,这个方法接收一个实现了java.awt.ActionListener接口的对象.每个Swing组件都有一个或多个add<事件名字>Listener(事件处理器类型 处理器)的方法,所要加入的事件事处器只要实现了即可
package java.awt.event.ActionListener;
public interface ActionListener{//一事件接口
public void actionPerformed(ActionEvent e);
}
2.2.2 actionPerformed方法有一个ActionEvent(事件对象)形的参数这个ActionEvent对象有许多方法可以得到事件源的相关信息,ActionEvent对象的getSource()返回一个Object类型的对象,指向了发出这个事件的事件源,如是点击了一个JButton,就可以将这个Object强制转型为JButton类型,即可得到事件源对象;getActionCommand()方法可以得到事件源上的标签做为一个String类型,比如是一个菜单,就可得到菜单的标题。
public class MyButtonListener
implements ActionListener{//实现事件接口的一类(具体操作)
public void actionPerformed(ActionEvent e){
Object sour=e.getSource();
if(sour instanceof JButton){
JButton bu=(JButton)sour;
String lab=bu.getText();
System.out.println("按下了个按钮: "+lab);
}else{
System.out.println("其它组件发出的事件!");
}
}
}
2.2.3将事件加给某个按钮,把要加的事件也看做一个对象先创建后被调用
JButton button=new JButton();
MyButtonListener mbl=new MyButtonListener();
mbl.addActionListener(mbl);
2.2.4 上面的添加事件方法也可以在类内通过创建匿名内部类实现组件添加事件
JButton button=new JButton();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
processEvent();
}
});
Public void processEvent(ActionEvent e){
Object sour=e.getSource();
if(sour instanceof JButton){
JButton bu=(JButton)sour;
String lab=bu.getText();
System.out.println("按下了个按钮: "+lab);
}else{
System.out.println("其它组件发出的事件!");
}
}
2.3 swing组件扩展注意:
2.3.1使用一个Swing组件时,要注意查看它的构造器的说明,查看构造器中接受什么样的参数。
2.3.2 Swing组件是可重叠的,即一个组件上面可以放另外一个组件:add开头的方法
23.3 Swing的事件机制:Swing组件通加添加实现了具体事件处理接口的事件处理对象处理事件,事件处理器接口定义主要是Java.awt.event包下面;组件添加事件处理器的对应方法为add<处理器接口名>(事件接口 实现对象名字)
2.3.4 常用事件接口:
一:ActionListener接口
二:MouseListener接口 MouseMotionListener接口MouseWheelListener接口
三:KeyListener接口KeyMapListener接口
四:ComponentListener接口ContainerListener接口(用来监听所在容器的状态)
2.4组件与事件综合运用
package com.jiemian;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JTree;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTable;
public class SecondUI extends JFrame {
public SecondUI(String title){
super(title);
}
public static void main(String[] args){
SecondUI sUI=new SecondUI("第二个窗口");
sUI.setDefaultCloseOperation(3);
sUI.setResizable(true);
sUI.init();
}
public void init(){
this.setAlwaysOnTop(true);
//选择流布局
FlowLayout f1=new FlowLayout();
this.setLayout(f1);
// 创建一个Jtree对象:用系统默认的
JTree ft=new JTree();
this.add(ft);
final JLabel label=new JLabel("显示输入的内容");//注意final作用
this.add(label);
JPanel panel=new JPanel(f1);
panel.setBackground(java.awt.Color.BLUE);//设置面板背景颜色 注意颜色的设置
panel.setSize(20,20);
panel.add(new JButton("panel上的"));
this.add(panel);
String[] a=new String[]{"湖南","浙江","上海"," 天津","北京" };
final JComboBox cobox=new JComboBox(a);
cobox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s=(String)cobox.getSelectedItem();
label.setText(s);
}
});
this.add(cobox);
JList list=new JList(a);
this.add(list);
JTable table=new JTable(4,5);
this.add(table);
this.setSize(500,500);
this.setVisible(true);
}
}
3画板实现
3.1 功能分析:
用户用鼠标点两下,就可以画一个圆,或画一线条,或画…
JFrame类有一个添加鼠标事件监听器的方法: frame.addMouseListener(MouseListener l)。
过程:创建一JFrame对象展示一个界面---实现鼠标事件接口添加鼠标点击事件---实现中写具体操作方法
frame<getGraphics()<setColor<drawLine()等
3.2 综合实现案例:
主类
package com.jiemian;
import javax.swing.JFrame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Graphics;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JColorChooser;//颜色选择器组件
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JRadioButton;//单选按钮 可选择与取消 并显示对象
import javax.swing.ButtonGroup;
public class SampleDraw {
public static void main(String[] args){
SampleDraw samdraw=new SampleDraw();
samdraw.showDraw();
}
//frame<getGraphics()<setColor<drawLine()等
public void showDraw(){
JFrame frame=new JFrame("第一个画布");
frame.addMouseListener(new MouseAdapter(){//运用适配器抽象类
public void mouseReleased(MouseEvent e){
onReleased(e);//注意括号里加e
}
});
//选择颜色的按钮,点击后,弹出颜色选择器:
JButton button=new JButton("请选择颜色");
//设置按钮的事件监听器:
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
showColorSelecter();//调用弹出颜色选择器的方法
}
});
FlowLayout f1=new FlowLayout();//选择布局
frame.setLayout(f1);
frame.add(button);
//先创建三个单选按钮
JRadioButton jrLine=new JRadioButton("直线");
jrLine.setActionCommand("直线");
JRadioButton jrRact=new JRadioButton("长方形");
jrRact.setActionCommand("长方形");
JRadioButton jrCircle=new JRadioButton("填充圆");
jrCircle.setActionCommand("填充圆");
// 加入到组中:
buttonGroup.add(jrLine);
buttonGroup.add(jrRact);
buttonGroup.add(jrCircle);
//将选择组加到界面上:
frame.add(jrLine);
frame.add(jrRact);
frame.add(jrCircle);
frame.setResizable(false);
frame.setSize(500,400);
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
//JFrame类提供了一个getGraphics()方法可以得到Java.awt.Graphics对象
//取得界面上的画布对象:一定要在界面setVisible后取得!
g=frame.getGraphics();//画布可以理解为将要画的图所占用的窗口空间
}
public void onReleased(MouseEvent e){
if(clickCount==0){//如果是第一次点击
x1=e.getX();//用e调用第一次点击的坐标
y1=e.getY();
clickCount++;
}else{//第二次点击
clickCount--;//复位为0
x2=e.getX();
y2=e.getY();
//一。g.drawLine(x1,y1,x2,y2);//在画布上画线,调用画布对象的方法
//二。有NetJavaShape接口后并有了实现它的ImpLine类就可以直接调用不用上面的了
//NetJavaShape impline=new ImpLine();
//impline.draw(g, x1, y1, x2, y2, this.c);//注意不可以用c不然不可以换颜色固定为黑色
//三。不仅画直线还要画长方形和填充的圆
//看用户选中的是要画什么形状,根据用户选择,创则不同的对象:
String command=buttonGroup.getSelection().getActionCommand();
System.out.println("command="+command);
//此处为默认画直线 后面可以改
NetJavaShape shape=new ImpLine();
if(command.equals("直线")){
shape=new ImpLine();
}else{if(command.equals("长方形")){
shape=new ImpRect();
}else{if(command.equals("填充圆")){
shape=new ImpOval();
}
}
}
shape.draw(g, x1, y1, x2, y2,this.c);
}
}
public void showColorSelecter(){
//注意:弹出颜色选择器:弹出在那个组件上,标题,初始化颜色三个参数要指定
//这是JColorChooser中的一个public static方法,可直接调
this.c=JColorChooser.showDialog(null,"选择颜色",java.awt.Color.BLACK);
}
private int clickCount=0;
private int x1,y1,x2,y2;
private Graphics g; //从界面对象上得到的"画布"对象 把Graphics当做画布
private Color c=java.awt.Color.BLACK;//默认颜色为黑色
private ButtonGroup buttonGroup=new ButtonGroup();
}
画形状接口
package com.jiemian;
import java.awt.Graphics;
import java.awt.Color;
public interface NetJavaShape {
public abstract void draw(Graphics g,int x1,int y1,int x2,int y2,Color c);
}
实现形状接口的直线类
package com.jiemian;
import java.awt.Graphics;
import java.awt.Color;
public class ImpLine implements NetJavaShape {
public void draw(Graphics g,int x1,int y1,int x2,int y2,Color c){
g.setColor(c);//注意是设置画布的颜色
g.drawLine(x1,y1,x2,y2);
}
}
实现形状接口的椭圆类
package com.jiemian;
import java.awt.Graphics;
import java.awt.Color;
public class ImpOval implements NetJavaShape{
public void draw(Graphics g,int x1,int y1,int x2,int y2,Color c){
g.setColor(c);
g.drawOval(x1,y1,x2-x1,y2-y1);
g.fillOval(x1, y1, x2-x1, y2-y1);
}
}
实现形状接口的长方形类
package com.jiemian;
import java.awt.Graphics;
import java.awt.Color;
public class ImpRect implements NetJavaShape {
public void draw(Graphics g,int x1,int y1,int x2,int y2,Color c){
g.setColor(c);
g.drawRect(x1,y1,x2-x1,y2-y1);
}
}