日期:2014-05-20 浏览次数:20947 次
import java.awt.*; import java.awt.event.*; class MyFrame extends Frame implements ItemListener, ActionListener { List list; TextArea text; MyFrame() { list=new List(4,false); //list有四个商品名称 text=new TextArea(20,20); //text输出单击或双击结果 list.add("苹果"); list.add("香蕉"); list.add("桃子"); list.add("芒果"); add(list,BorderLayout.NORTH); add(text,BorderLayout.CENTER); list.addItemListener() list.addActionListener() this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); setBounds(100,100,300,300); validate(); } public void itemStateChanged(ItemEvent e) //实现单击时显示商品价格和产地 { //text.setText(null); text.setText(list.getSelectedItem()+":\n"); int n=list.getSelectedIndex(); if(n==0) { text.append("5元/斤\n"+"产自澳大利亚"); } else if(n==1) { text.append("8元/斤\n"+"产自巴拿马"); } else if(n==2) { text.append("4元/斤\n"+"产自台湾"); } else { text.append("9/元/斤\n"+"产自厦门"); } } public void actionPerformed(ActionEvent ee) //实现双击时显示广告而不显示其他内容 { //text.setText(null); int k=list.getSelectedIndex(); //text.setText(list.getSelectedItem()+":\n"); if(k==0) { text.setText("ad1:aaa"); } else if(k==1) { text.setText("ad2:bbb"); } else if(k==2) { text.setText("ad3:ccc"); } else { text.setText("ad4:ddd"); } } } class E_7_10 { public static void main(String args[]) { new MyFrame(); } }
list.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e) { if(e.getClickCount() == 1){ int k = list.getSelectedIndex(); // text.setText(list.getSelectedItem()+":\n"); if (k == 0) { text.setText("ad1:aaa"); } else if (k == 1) { text.setText("ad2:bbb"); } else if (k == 2) { text.setText("ad3:ccc"); } else { text.setText("ad4:ddd"); } } else if(e.getClickCount() == 2){ text.setText(list.getSelectedItem() + ":\n"); int n = list.getSelectedIndex(); if (n == 0) { text.append("5元/斤\n" + "产自澳大利亚"); } else if (n == 1) { text.append("8元/斤\n" + "产自巴拿马"); } else if (n == 2) { text.append("4元/斤\n" + "产自台湾"); } else { text.append("9/元/斤\n" + "产自厦门"); } } } });