java列表框(JList和JComboBox)使用

tony_xj發表於2020-10-10

java列表框有JList和JCombox兩種型別,使用的方法類似。

實現頁面

程式程式碼

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class List1 {
	JFrame jf=new JFrame("列表框測試");
	String[] books={"java", "android", "C++", "C", "C#", "HTML", "js"};
	JPanel layoutPanel=new JPanel();
	ButtonGroup layoutGroup=new ButtonGroup();
	JPanel selectModelPanel=new JPanel();
	ButtonGroup selectModeGroup=new ButtonGroup();

	JTextArea favorite=new JTextArea(4, 40);
	//JList和JComboBox方法類似
	JList<String> bookList;
	JComboBox<String> bookSelector;

	public void init(){
		//設定容器和元件的邊框
		layoutPanel.setBorder(new TitledBorder(new EtchedBorder(), "確定選項佈局"));
		selectModelPanel.setBorder(new TitledBorder(new EtchedBorder(), "確定選擇模式"));
		favorite.setBorder(new EtchedBorder());
		//建立JList
		bookList=new JList<>(books);
		bookList.setVisibleRowCount(3);
		bookList.setSelectionInterval(1,2);
		//建立JComboBox
		bookSelector=new JComboBox<>(books);
		bookSelector.setEditable(true);
		bookSelector.setMaximumRowCount(4);
		bookSelector.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				Object selectedItem = bookSelector.getSelectedItem();
				if(selectedItem!=null){
					favorite.setText(selectedItem.toString());
				}
			}
		});
		//設定佈局按鈕
		addBtn2LayoutPanel("縱向滾動", JList.VERTICAL);
		addBtn2LayoutPanel("縱向換行", JList.VERTICAL_WRAP);
		addBtn2LayoutPanel("橫向換行", JList.HORIZONTAL_WRAP);
		//設定選擇按鈕
		addBtn2SelectPanel("無限制", ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		addBtn2SelectPanel("單選", ListSelectionModel.SINGLE_SELECTION);
		addBtn2SelectPanel("單範圍", ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		//垂直佈局容器
		Box leftVBox = Box.createVerticalBox();
		//使用JScrollPane,才會有滾動條
		leftVBox.add(new JScrollPane(bookList));
		leftVBox.add(layoutPanel);
		leftVBox.add(selectModelPanel);
		//JComboBox放在Panel中才會填滿介面
		JPanel bookSelectorPanel = new JPanel();
		bookSelectorPanel.add(bookSelector);
		//頂部容器
		Box topBox = Box.createHorizontalBox();
		topBox.add(leftVBox);
		topBox.add(bookSelectorPanel);
		//底部容器
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new BorderLayout());
		bottomPanel.add(new JLabel("你最喜歡的圖書:"), BorderLayout.NORTH);
		bottomPanel.add(favorite);
		//組合在window中
		Box box = Box.createVerticalBox();
		box.add(topBox);
		box.add(bottomPanel);
		jf.add(box);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.pack();
		jf.setVisible(true);
	}

	//新增改變佈局的按鈕
	public void addBtn2LayoutPanel(String name, int layoutType){
		JRadioButton btn=new JRadioButton(name);
		layoutPanel.add(btn);
		if(layoutGroup.getButtonCount()==0){
			btn.setSelected(true);
		}
		layoutGroup.add(btn);
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				bookList.setLayoutOrientation(layoutType);
			}
		});
	}

	//新增改變選取的按鈕
	public void addBtn2SelectPanel(String name, int selectType){
		JRadioButton btn=new JRadioButton(name);
		selectModelPanel.add(btn);
		if(selectModeGroup.getButtonCount()==0){
			btn.setSelected(true);
		}
		selectModeGroup.add(btn);
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				bookList.setSelectionMode(selectType);
			}
		});
	}

	//main()
	public static void main(String[] args) {
		new List1().init();
	}
}

 

相關文章