corejava学习6(界面)

 

界面

1  选择容器

2  设置布局管理器

3  添加组件

4  设置事件监听

 

 

 

Jframe    窗体     一个容器

 

Jpanel    面板    一个组件

 

可见性须最后设置,因为不这样你后面加的组件看不见。

 

FlowLayout 流式布局      它是jpanle  默认布局

 

BorderLayout  边界布局       它是jframe   默认布局

 

GridLayout  网络布局      

 

CardLayout  卡片布局

 

GridBagLayout 复杂网格布局

 

 

 

 

AWT事件模型

 

事件对象:一个事件是一个对象

事件源:发生事件的对象,报告的事件的对象。

事件监听器:

 

 

事件源预先就某种事件注册监听器,当事件发生时,事件源就给注册的监听器发送一个事件对象,由监听器作出处理。

 

事件源可以同时是多种事件的事件源,就某种事件单独注册监听器。

 

事件源就同一种事件,可以注册多个监听器。

 

一个监听器可以同时注册在多个事件源当中。

 

事件对象中会封装事件源对象。

事件监听接口中的每一个方法,都应以对应的事件对象作为参数。

 

所谓的事件源给监听器发送事件对象,其实就是,事件源以事件对象为参数,

 

监听器注册在事件源中。

 

import java.util.*;

public class TestGirl {

    public static void main(String[] args) {

           Girl g=new Girl("Luxw");

           EmotionListener l1=new GoodBoy("Huxz");

           EmotionListener l2=new BadBoy("Liucy");

           g.addEmotionListener(l1);

           g.addEmotionListener(l2);

           g.fire();

           g.removeEmotionListener(l2);

           g.fire();

           g.removeEmotionListener(l1);

           g.fire();

    }

}

class EmotionEvent extends EventObject{

    public EmotionEvent(Object source){

           super(source);

    }

}

interface EmotionListener extends EventListener{

    void whatCanIdoWhenGirlHappy(EmotionEvent e);

    void whatCanIdoWhenGirlSad(EmotionEvent e);

}

class Girl{

    String girlName;

    //element: EmotionListener

    List bfs=new ArrayList();

    public Girl(String girlName){

           this.girlName=girlName;

    }

    public String getGirlName(){

           return this.girlName;

    }

    public void addEmotionListener(EmotionListener l){

           bfs.add(l);

    }

    public void removeEmotionListener(EmotionListener l){

           bfs.remove(l);

    }

    public void fire(){

           System.out.println();

           EmotionEvent e=new EmotionEvent(this);

           for(int day=1;day<=6;day++){

                  System.out.println("Day "+day+" : ");

                  if (day%2==1)

                         System.out.println(girlName+" :今天我不爽");

                  else System.out.println(girlName+" :今天我很爽");

                 

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

                         EmotionListener l=(EmotionListener)bfs.get(i);

                         if (day%2==1){

                                l.whatCanIdoWhenGirlSad(e);

                         }

                         else{

                                l.whatCanIdoWhenGirlHappy(e);

                         }

                  }

           }

    }

}

 

class GoodBoy implements EmotionListener{

    String boyName;

    public GoodBoy(String boyName){

           this.boyName=boyName;

    }

    public void whatCanIdoWhenGirlHappy(EmotionEvent e) {

           Girl g=(Girl)e.getSource();

           String gn=g.getGirlName();

           System.out.println(boyName+" : "+gn+", 你爽我也爽");

    }

    public void whatCanIdoWhenGirlSad(EmotionEvent e) {

           Girl g=(Girl)e.getSource();

           String gn=g.getGirlName();

           System.out.println(boyName+" : "+gn+", 你不爽我也不爽");

    }

}

 

class BadBoy implements EmotionListener{

    String boyName;

    public BadBoy(String boyName){

           this.boyName=boyName;

    }

    public void whatCanIdoWhenGirlHappy(EmotionEvent e) {

           Girl g=(Girl)e.getSource();

           String gn=g.getGirlName();

           System.out.println(boyName+" : "+gn+", 你爽我就让你不爽");

    }

    public void whatCanIdoWhenGirlSad(EmotionEvent e) {

           Girl g=(Girl)e.getSource();

           String gn=g.getGirlName();

           System.out.println(boyName+" : "+gn+", 你不爽我就很爽");

    }

   

}

结果:

 

Day 1 :

Luxw :今天我不爽

Huxz : Luxw, 你不爽我也不爽

Liucy : Luxw, 你不爽我就很爽

Day 2 :

Luxw :今天我很爽

Huxz : Luxw, 你爽我也爽

Liucy : Luxw, 你爽我就让你不爽

Day 3 :

Luxw :今天我不爽

Huxz : Luxw, 你不爽我也不爽

Liucy : Luxw, 你不爽我就很爽

Day 4 :

Luxw :今天我很爽

Huxz : Luxw, 你爽我也爽

Liucy : Luxw, 你爽我就让你不爽

Day 5 :

Luxw :今天我不爽

Huxz : Luxw, 你不爽我也不爽

Liucy : Luxw, 你不爽我就很爽

Day 6 :

Luxw :今天我很爽

Huxz : Luxw, 你爽我也爽

Liucy : Luxw, 你爽我就让你不爽

 

Day 1 :

Luxw :今天我不爽

Huxz : Luxw, 你不爽我也不爽

Day 2 :

Luxw :今天我很爽

Huxz : Luxw, 你爽我也爽

Day 3 :

Luxw :今天我不爽

Huxz : Luxw, 你不爽我也不爽

Day 4 :

Luxw :今天我很爽

Huxz : Luxw, 你爽我也爽

Day 5 :

Luxw :今天我不爽

Huxz : Luxw, 你不爽我也不爽

Day 6 :

Luxw :今天我很爽

Huxz : Luxw, 你爽我也爽

 

Day 1 :

Luxw :今天我不爽

Day 2 :

Luxw :今天我很爽

Day 3 :

Luxw :今天我不爽

Day 4 :

Luxw :今天我很爽

Day 5 :

Luxw :今天我不爽

Day 6 :

Luxw :今天我很爽

 

 

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TestActionListener {

    public static void main(String[] args) {

           JFrame f=new JFrame("hehe");

           f.setSize(400,120);

           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

           f.setLayout(new FlowLayout());

           JButton b=new JButton("BUTTON");

           JTextField jtf=new JTextField(15);

           f.add(b);

           f.add(jtf);

           f.setVisible(true);

          

           ActionListener l=new ActionListener(){

                  public void actionPerformed(ActionEvent arg0) {

                         System.out.println("Action!");

                  }

           };

          

           b.addActionListener(l);

           jtf.addActionListener(l);

    }

}

 

产生随机数

 

Random  rnew  RanDom();

Int dr.nextInt();

 

你可能感兴趣的:(swing,F#)