用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

列表

2017-12-26 作者: 柯侧耳倾听者举报

[java]代码库

import java.util.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class ListTest
{
	private JFrame mainWin = new JFrame("测试列表框");
	String[] books = new String[]
	{
		"疯狂Java讲义"
		, "轻量级Java EE企业应用实战"
		, "疯狂Android讲义"
		, "疯狂Ajax讲义"
		, "经典Java EE企业应用实战"
	};
	JList<String> bookList = new JList<>(books);
	JComboBox<String> bookSelector;
	JPanel layoutPanel = new JPanel();
	ButtonGroup layoutGroup = new ButtonGroup();
	JPanel selectModePanel = new JPanel();
	ButtonGroup selectModeGroup = new ButtonGroup();
	JTextArea favoriate = new JTextArea(4 , 40);
	public void init()
	{
		bookList.setVisibleRowCount(3);
		bookList.setSelectionInterval(2, 4);
		addLayoutButton("纵向滚动", JList.VERTICAL);
		addLayoutButton("纵向换行", JList.VERTICAL_WRAP);
		addLayoutButton("横向换行", JList.HORIZONTAL_WRAP);
		addSelectModelButton("无限制", ListSelectionModel
			.MULTIPLE_INTERVAL_SELECTION);
		addSelectModelButton("单选", ListSelectionModel
			.SINGLE_SELECTION);
		addSelectModelButton("单范围", ListSelectionModel
			.SINGLE_INTERVAL_SELECTION);
		Box listBox = new Box(BoxLayout.Y_AXIS);
		listBox.add(new JScrollPane(bookList));
		listBox.add(layoutPanel);
		listBox.add(selectModePanel);
		bookList.addListSelectionListener(e -> {  
			List<String> books = bookList.getSelectedValuesList();
			favoriate.setText("");
			for (String book : books )
			{
				favoriate.append(book + "\n");
			}
		});
		Vector<String> bookCollection = new Vector<>();
		bookCollection.add("疯狂Java讲义");
		bookCollection.add("轻量级Java EE企业应用实战");
		bookCollection.add("疯狂Android讲义");
		bookCollection.add("疯狂Ajax讲义");
		bookCollection.add("经典Java EE企业应用实战");
		bookSelector = new JComboBox<>(bookCollection);
		bookSelector.addItemListener(e -> {  
			Object book = bookSelector.getSelectedItem();
			favoriate.setText(book.toString());
		});
		bookSelector.setEditable(true);
		bookSelector.setMaximumRowCount(4);
		JPanel p = new JPanel();
		p.add(bookSelector);
		Box box = new Box(BoxLayout.X_AXIS);
		box.add(listBox);
		box.add(p);
		mainWin.add(box);
		JPanel favoriatePanel = new JPanel();
		favoriatePanel.setLayout(new BorderLayout());
		favoriatePanel.add(new JScrollPane(favoriate));
		favoriatePanel.add(new JLabel("您喜欢的图书:")
			, BorderLayout.NORTH);
		mainWin.add(favoriatePanel , BorderLayout.SOUTH);
		mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainWin.pack();
		mainWin.setVisible(true);
	}
	private void addLayoutButton(String label, final int orientation)
	{
		layoutPanel.setBorder(new TitledBorder(new EtchedBorder()
			, "确定选项布局"));
		JRadioButton button = new JRadioButton(label);
		layoutPanel.add(button);
		if (layoutGroup.getButtonCount() == 0)
		button.setSelected(true);
		layoutGroup.add(button);
		button.addActionListener(event ->
			bookList.setLayoutOrientation(orientation));
	}
	private void addSelectModelButton(String label, final int selectModel)
	{
		selectModePanel.setBorder(new TitledBorder(new EtchedBorder()
			, "确定选择模式"));
		JRadioButton button = new JRadioButton(label);
		selectModePanel.add(button);
		if(selectModeGroup.getButtonCount() == 0)
			button.setSelected(true);
		selectModeGroup.add(button);
		button.addActionListener(event ->
			bookList.setSelectionMode(selectModel));
	}
	public static void main(String[] args)
	{
		new ListTest().init();
	}
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...