第1个回答 2013-07-18
//在列表框显示若干颜色选项,当单击某选项时,将文本区的背景设置为相应的颜色,当双击列表框中的某选项时,将选中颜色的文字添加到文本区中。
import java.awt.*;
import java.awt.event.*;
public class li extends Frame implements ItemListener,ActionListener
{
static li frm=new li();
static List lst=new List();
static TextArea ta=new TextArea(5,20);
public static void main(String args[])
{
frm.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
frm.setSize(350,200);
lst.add("红色");
lst.add("绿色");
lst.add("蓝色");
lst.add("黄色");
lst.addItemListener(frm);
lst.addActionListener(frm);
frm.add(lst);
frm.add(ta);
frm.setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
String clr=lst.getSelectedItem();
if(clr=="红色")
ta.setBackground(Color.red);
else if(clr=="绿色")
ta.setBackground(Color.green);
else if(clr=="蓝色")
ta.setBackground(Color.blue);
else if(clr=="黄色")
ta.setBackground(Color.yellow);
frm.setTitle("您选择了【"+clr+"】颜色");
}
public void actionPerformed(ActionEvent e)
{
String clrn=lst.getSelectedItem();
clrn="您双击的是【"+clrn+"】\n";
ta.append(clrn);
}
}
你可以参考下,试着运行,看是不是你想的那样