📄 listccolor.java
字号:
import java.awt.*;
import java.awt.event.*;
public class ListcColor extends WindowAdapter implements ItemListener,WindowListener
{
List list1;
Frame f;
public ListcColor() //构建窗口界面
{
f=new Frame("List Test"); //创建框架,默认布局是BorderLayout
f.setLayout(new FlowLayout()); //布局管理器设为FlowLayout
f.setSize(300,300); //设置框架大小
list1=new List();
list1.add("Red");
list1.add("Blue");
list1.add("Green");
list1.add("Black");
list1.add("Yellow");
list1.add("Gray");
list1.add("Pink");
list1.addItemListener(this); //注册监听列表框单击事件
f.add(list1);
f.setBackground(Color.red);
f.setVisible(true);
f.addWindowListener(this); //注册监听框架的关闭事件
}
public void itemStateChanged(ItemEvent e)
{
String str=list1.getSelectedItem();
if(str=="Red")
f.setBackground(Color.red);
if(str=="Blue")
f.setBackground(Color.blue);
if(str=="Green")
f.setBackground(Color.green);
if(str=="Black")
f.setBackground(Color.black);
if(str=="Yellow")
f.setBackground(Color.yellow);
if(str=="Gray")
f.setBackground(Color.gray);
if(str=="Pink")
f.setBackground(Color.pink);
}
public void windowClosing(WindowEvent e) //关闭框架窗口
{
System.exit(0); //关闭窗口
}
public static void main(String args[])
{
new ListcColor();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -