import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JList;
import javax.swing.BorderFactory;
public class JListDemo
{
public static void main(String[] args)
{
String[] names = {"孙中山", "袁世凯", "毛泽东", "蒋介石", "邓小平", "天安门"};
JList list = new JList (names);
list.setBorder (BorderFactory.createEtchedBorder());
list.setFixedCellHeight (30);//设置每个选项的高度
list.setFixedCellWidth (70);//设置每个选项的宽度,一般不需要
list.setDragEnabled (true);
list.setBackground (Color.cyan);
list.setForeground (Color.red);
list.setSelectionBackground (Color.white);
list.setSelectionForeground (Color.black);
list.setLayoutOrientation (JList.HORIZONTAL_WRAP);
JLabel label = new JLabel("blank");
JSplitPane panel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list,label);
panel.setPreferredSize (new Dimension (600,300));
panel.setOneTouchExpandable (true);
JFrame frame = new JFrame("人名");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (panel);
frame.pack();
frame.show();
}
}
评论