multilistener.java~19~

来自「提供了常用的JAVA技术的示例」· JAVA~19~ 代码 · 共 69 行

JAVA~19~
69
字号
package MultiListener;import java.awt.*;import java.awt.event.*;import java.util.*;public class MultiListener implements ActionListener,ItemListener,WindowListener{    private Frame f;    private java.awt.List lst;    private TextField tf;    static Random num=new Random(1000);    public void create() {        f = new Frame("testing the listing");        tf=new TextField();        lst=new java.awt.List(4,false);        lst.add("1");        lst.add("2");        lst.add("3");        lst.add("4");        lst.addItemListener(this);        lst.addActionListener(this);        f.setSize(300,300);        f.add(lst,"Center");        f.add(tf,"South");        f.addWindowListener(this);        f.setVisible(true);    }    public void itemStateChanged(ItemEvent e){        int id=((java.awt.List)e.getSource()).getSelectedIndex();        switch(id){            case 0:{tf.setText("id-1 is :"+num.nextInt());                    tf.setBackground(new Color(225,230,0));                    break;                    }            case 1:{tf.setText("id-2 is :"+num.nextInt());                    break;                    }            case 2:{tf.setText("id-3 is :"+num.nextInt());                    break;                    }            case 3:{tf.setText("id-4 is :"+num.nextInt());                    break;                    }        }    }   public void actionPerformed(ActionEvent e){      /*  tf.setForeground(Color.blue);        tf.setBackground(new Color(225,230,0));        lst.setBackground(new Color(225,230,0));        lst.setForeground(new Color(0,0,255));*/    }   // public void windowClosing(WindowEvent e){System.exit(1);}    public void windowActivated(WindowEvent e){}    public void windowClosed(WindowEvent e){}    public void windowDeactivated(WindowEvent e){}    public void windowDeiconified(WindowEvent e){}    public void windowIconified(WindowEvent e){}    public void windowOpened(WindowEvent e){}    public static void main(String[] args) {      MultiListener mul = new MultiListener();      mul.create();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?