Swing笔记

1.关于事件聆听器

 处理事件聆听的方法大致有四种:

1 )调用类继承聆听接口,然后在本类中实现聆听方法;(适用于聆听对象较少的情况)

2 )匿名类;(简洁快速)

3 )建立一个全新的聆听类

4 )祥见第十四条

2.匿名类中不能出现这,因为匿名类的句柄你无法得到!

 

3.JFrame JApplet JWindow, JDialog 是重的密码,并非用纯一种新型的计算机语言代码编写的,摇摆的其他组件大都是继承了 JComponent(这些类都是光密码) 

 

4添加MenuBar的方法有两种:

(1).JFrame添加法:这时你要使用到JFrame的方法setJMenuBar(JMenuBar),这是你无须指定放到顶端!

(2) ContentPane添加法:ContentPane添加 MenuBar 时有时 MenuBar 会占据全部的容器,这是往往要通过下边的代码使它放置到顶端

Container container=getContentPane();

container.add(MenuBar ,BorderLayout.NORTH);

记住 BorderLayout 没有顶端这个常量.

注意:添加JToolBar时,你只能把它添加到ContentPane

 

5.要想构造一个具有word风格的多文档窗口,swing为你提供了一个很方便的组合:DesktopPaneInternalFrame.

Container.add(DesktopPane),然后DesktopPaneadd多个InternalFrame,每个InternalFrame可以add多文本JTextAreaJTextField.                         

 

6.AB的子类,A类中可以直接调用B中的普通方法,但要调用父类的构造方法必须使用关键字super()

 

7.把类中多个方法需要调用的变量定义为类一级别的变量,把只会在单独的一个类中出现的变量定义为方法级别的变量。

 

9,类与类之间尽可能的保持个自的独立性,一个类中的资源最好不要涉及到另一个类,他的资源尽可能是它自己特有的或共有的(如static变量),这就是面向对象常说的高聚合,低耦合.

 

10.菜单项”Open”的实现:

open.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

openDocument();}

});

public void openDocument(){

JFileChooser fileChooser=new JFileChooser();

fileChooser.showOpenDialog(this);

}

 

11.常常遇到使某个对象处于激活状态或非激活状态。这是我们常常使用方法xx.setActive(boolean)xx.setEnabled(boolean).(注意:是setEnabled不是setEnable

 

12.setOpaque(boolean)javax.swing.JComponent的方法,因此除过Swing的那四个Heavy Component,其他组件都有这个方法!这个方法的作用如下:

public void setOpaque(boolean isOpaque)

If true the component paints every pixel within its bounds. Otherwise, the component may not paint some or all of its pixels, allowing the underlying pixels to show through. The default value of this property is false for JComponent. However, the default value for this property on most standard JComponent subclasses (such as JButton and JTree) is look-and-feel dependent

 

13.给菜单项添加Icon

为了美化菜单,常给菜单添加一些图标,所用的方法:

JMenuItem new1=new JMenuItem("New",new ImageIcon("icons/new24.gif"));

这时的图标,在”New”的左侧,如果你觉得这样还不美观,想把图标放到”New”的右侧,可以:

JMenuItem new1 = new JMenuItem("New",new ImageIcon("icons/new24.gif"));

new1.setHorizontalTextPosition(SwingConstants.LEFT);

 

14.工具栏(JToolBar)

一个例程:

protected void buildToolBar(){

    toolBar = new JToolBar();

    toolBar.setFloatable(true);

    ToolBarAction tba_new   = new ToolBarAction("new",new ImageIcon("icons/new.jpg"));

    ToolBarAction tba_open  = new ToolBarAction("open",new ImageIcon("icons/open.jpg"));

    ToolBarAction tba_close  = new ToolBarAction("close",new ImageIcon("icons/close.jpg"));

    ToolBarAction tba_save  = new ToolBarAction("save",new ImageIcon("icons/save.jpg"));

    JButton JB;

    JB = toolBar.add(tba_new);

    JB.setActionCommand("TB_NEW");

    JB.setToolTipText((String)tba_new.getValue(Action.NAME));

    JB = toolBar.add(tba_open);

    JB.setActionCommand("TB_OPEN");

    JB.setToolTipText((String)tba_open.getValue(Action.NAME));

    JB = toolBar.add(tba_close);

    JB.setActionCommand("TB_CLOSE");

    JB.setToolTipText((String)tba_close.getValue(Action.NAME));

    toolBar.addSeparator();

    JB = toolBar.add(tba_save);

    JB.setActionCommand("TB_SAVE");

    JB.setToolTipText((String)tba_save.getValue(Action.NAME));

    tba_save.setEnabled(false);

  }//end of buildToolBar

 

这里ToolBarAction是自己定义的一个实现AbstractAction的类,如下:

class ToolBarAction extends AbstractAction{

public ToolBarAction(String name,Icon icon){

        super(name,icon);

    }

public void actionPerformed(ActionEvent e){

        try{

            theArea.append(e.getActionCommand()+"/n");

        }catch(Exception ex){}

    }

}//end of inner class ToolBarAction

(1):是否把工具条设置为浮动的:JToolBar.setFloatable(true);

(2):这时留意JToolBaradd()方法的定义:public JButton add(Action a)

它的返回值是JButton,参数是Action.Action接口继承了ActionListener接口,下边是文档中的解释:

Adds a new JButton which dispatches the action.

As of 1.3, this is no longer the preferred method for adding Actions to a container. Instead it is recommended to configure a control with an action using using setAction, and then add that control directly to the Container.

Parameters: a - the Action object to add as a new menu item

Returns: the new button which dispatches the action

正如文档中所说,这种给容器添加行为的方法已经不太提倡,现在常用的方法是添加控件给容器,然后给这个控件setAction.

(3):这里还需要了解一下接口Action和类AbstractAction

Java.util.EventListener

 
                                

                      Interface

                 

Java.awt.event.ActionListener

 
                      Interface

                   



Javax.swing.Action

 
 


                      Interface

 


Javax.swing.AbstractAction

 
                       class

 

我们不常使用Action,取而代之的使用他的子类AbstractAction在这个类中我们只要实现方法public void actionPerformed(ActionEvent e)ok了!

所以这就给我们提供了又一种添加事件响应的方法:即添加控件到容器中,然后使用setAction(Action)方法来设置聆听器,同时你要实现Action的子类AbstractAction中的public void actionPerformed(ActionEvent e)方法

EG: toolBar = new JToolBar();

toolBar.setFloatable(true);

JButton openButton=new JButton(new ImageIcon("icons/open.jpg"));

openButton.setAction(new OpenAction());

toolBar.add(openButton);

其中OpenAction类我是这样写的:

private class OpenAction extends AbstractAction{

public void actionPerformed(ActionEvent e){

openDocument();//作用是启动打开对话框

}

}

 

15.文件路径的表示方法有两种:

(1)new ImagIcon(“.//icons/open.gif”);      (Windows的习惯方式)

(2) new ImagIcon(“/icons/open.gif”);      Unix的习惯方式)

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值