?? chap12-5.txt
字號:
// 程序12-5
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
public class testEventList {
JFrame frame;
Container contentPane;
JList list; // 下拉列表
JLabel label; // 標簽
// 下面定義了顏色數組和相應的顏色對象數組
String colorNames[ ]={" Black"," Blue "," Red "," White","Yellow"};
Color colors[ ]={Color.black,Color.blue,Color.red,Color.white,Color.yellow};
public testEventList( ){ // 構造函數
frame=new subJFrame("testEventList"); // 定義一個框架
contentPane=frame.getContentPane( ); // 獲取框架的內容格
contentPane.setLayout(new FlowLayout( )); // 設置布局管理器
list=new JList(colorNames); // 依據數組生成列表
list.setVisibleRowCount(4); // 設置列表一次顯示的項數
// 設置選擇模式,一次只能選擇一條
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// 生成一個滾動面板,并將下拉列表加入滾動面板
contentPane.add(new JScrollPane(list));
label=new JLabel(" 顏色 "); // 定義標簽
contentPane.add(label); // 將標簽加入內容格
list.addListSelectionListener(
// 生成一個匿名類對象,處理JList事件
new ListSelectionListener( ) { // 下面是匿名類的定義
// 匿名類中只有一個方法,處理JList選擇項改變事件
public void valueChanged(ListSelectionEvent e) {
label.setText(list.getSelectedValue( ).toString( ));
label.setForeground(colors[list.getSelectedIndex( )]);
} // end of valueChanged
} // end of inner class
); // end of addListSelectionListener
frame.setSize(200,120);
frame.show( );
}
public static void main(String args[ ]){
testEventList obj=new testEventList( );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -