📄 listtest.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ListTest extends JFrame {
private JList colorList;
private Container c;
private String colorNames[] =
{ "Black", "Blue", "Cyan", "Dark Gray", "Gray", "Green",
"Light Gray", "Magenta", "Orange", "Pink", "Red",
"White", "Yellow" };
private Color colors[] =
{ Color.black, Color.blue, Color.cyan, Color.darkGray,
Color.gray, Color.green, Color.lightGray,
Color.magenta, Color.orange, Color.pink, Color.red,
Color.white, Color.yellow };
public ListTest()
{
super( "List Test" );
c = getContentPane();
c.setLayout( new FlowLayout() );
// create a list with the items in the colorNames array
colorList = new JList( colorNames );
colorList.setVisibleRowCount( 5 );
// do not allow multiple selections
colorList.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION );
// add a JScrollPane containing the JList
// to the content pane
c.add( new JScrollPane( colorList ) );
// set up event handler
colorList.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged( ListSelectionEvent e )
{
if (e.getValueIsAdjusting()==false)
c.setBackground(
colors[ colorList.getSelectedIndex() ] );
}
}
);
setSize( 350, 150 );
show();
}
public static void main( String args[] )
{
ListTest app = new ListTest();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -