package com.main;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.Vector;
public class JToolBarDemo {
public static void main(String[] age){
new JToolBarDemo().init();
}
JFrame list_test = new JFrame("List Test");
ButtonGroup layoutGroup = new ButtonGroup();
JPanel selectModePanel = new JPanel();
ButtonGroup selectGroup = new ButtonGroup();
JTextArea favorite = new JTextArea(4, 40);
JList<String> bookList;
JComboBox<String> bookSelector;
JPanel layoutPanel = new JPanel();
public void init(){
String[] books={"java自学宝典","轻量级JavaEE企业应用实战","Android基础教程","JQuery实战教程","SpringBoot企业级开发"};
//组装试图
//组装JList相关内容
bookList=new JList<String>(books);
addBtn2LayoutPanel("纵向滚动",JList.VERTICAL);
addBtn2LayoutPanel("纵向换行",JList.VERTICAL_WRAP);
addBtn2LayoutPanel("纵向换行",JList.HORIZONTAL_WRAP);
addSelectModelPanel("无限制",ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
addSelectModelPanel("单选",ListSelectionModel.SINGLE_SELECTION);
addSelectModelPanel("单范围",ListSelectionModel.SINGLE_INTERVAL_SELECTION);
//对Jlist做设置
bookList.setVisibleRowCount(3);
bookList.setSelectionInterval(2,4);//第3个到第五个默认选中
//处理list选中事件
bookList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
//获取当前选中的条目
List<String> selectedValuesList = bookList.getSelectedValuesList();
favorite.setText("");
//把当前选中的内容设置到文本域中
for (String str:selectedValuesList){
favorite.append(str);
}
}
});
Box bookListVBox = Box.createVerticalBox();
bookListVBox.add(new JScrollPane(bookList));
bookListVBox.add(layoutPanel);
bookListVBox.add(selectModePanel);
//组装JComboBox
Vector<String> vector=new Vector<>();
List<String> list = List.of("java自学宝典", "轻量级JavaEE企业应用实战", "Android基础教程", "JQuery实战教程", "SpringBoot企业级开发");
vector.addAll(list);
bookSelector=new JComboBox<>(vector);
bookSelector.setEditable(true);
bookSelector.setMaximumRowCount(4);
bookSelector.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
//获取当前已经选中的条目,把内容设置到文本域
Object selectedItem = bookSelector.getSelectedItem();
favorite.setText(selectedItem.toString());
}
});
//顶部部分组装
Box horizontalBox = Box.createHorizontalBox();
horizontalBox.add(bookListVBox);
JPanel jPanel1 = new JPanel();
jPanel1.add(bookSelector);
horizontalBox.add(jPanel1);
// horizontalBox.add(bookSelector);//直接放进去结果会导致下拉框被拉伸,对此需要加一个JLabel进行过滤
//组装底部
JPanel jPanel = new JPanel();
jPanel.setLayout(new BorderLayout());
jPanel.add(new JLabel("您喜欢的图书:"),BorderLayout.NORTH);
jPanel.add(favorite);
//组装整体布局
Box holeBox = Box.createVerticalBox();
holeBox.add(horizontalBox);
holeBox.add(jPanel);
list_test.add(holeBox);
list_test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
list_test.pack();
list_test.setVisible(true);
}
//封装方法,往layoutPanel中添加单选按钮
public void addBtn2LayoutPanel(String name,int layoutType){
//设置标题边框
layoutPanel.setBorder(new TitledBorder(new EtchedBorder(),"确定选项布局"));
//创建单选按钮
JRadioButton jRadioButton = new JRadioButton(name);
layoutPanel.add(jRadioButton);
//让第一个按钮默认选中
if(layoutGroup.getButtonCount()==0){
jRadioButton.setSelected(true);
}
layoutGroup.add(jRadioButton);
jRadioButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bookList.setLayoutOrientation(layoutType);
}
});
}
//封装方法,往SelectModelPanel中添加单选按钮
public void addSelectModelPanel(String name,int ModelPanel){
//设置标题边框
selectModePanel.setBorder(new TitledBorder(new EtchedBorder(),"选择模式"));
//创建单选按钮
JRadioButton jRadioButton = new JRadioButton(name);
selectModePanel.add(jRadioButton);//添加一个组是否可以?
//让第一个按钮默认选中
if(selectGroup.getButtonCount()==0){
jRadioButton.setSelected(true);
}
selectGroup.add(jRadioButton);
jRadioButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bookList.setSelectionMode(ModelPanel);
}
});
}
}
当出现个性化需求时可以重写AbstractListModel进行个性化开发
package test.object.model;
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.math.BigDecimal;
import java.util.List;
import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ListModelTest {
public void init() {
JFrame jf = new JFrame("test");
JList<BigDecimal> jList = new JList<>(
new NumberListModel(new BigDecimal(1), new BigDecimal(5), new BigDecimal(2)));
JComboBox<BigDecimal> jComboBox = new JComboBox<BigDecimal>(
new NumberComboBoxList(new BigDecimal(0.1), new BigDecimal(0.5), new BigDecimal(0.2)));
JLabel jLabel = new JLabel("您选择的直是:");
JTextField jTextField = new JTextField(15);
// 组装试图
// 组装顶部
jList.setVisibleRowCount(4);
jList.setSelectionInterval(2, 3);
jList.setFixedCellHeight(30);
jList.setFixedCellWidth(30);
jList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
List<BigDecimal> selectedValue = jList.getSelectedValuesList();
jTextField.setText("");
for (BigDecimal data : selectedValue) {
jTextField.setText(jTextField.getText() + data.toString() + ",");
}
}
});
jComboBox.setMaximumRowCount(4);// 可视高度
jComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
Object selectedItem = jComboBox.getSelectedItem();
jTextField.setText(selectedItem.toString());
}
});
Box createHorizontalBox = Box.createHorizontalBox();
createHorizontalBox.add(new JScrollPane(jList));
JPanel jPanel = new JPanel();
jPanel.add(jComboBox);
createHorizontalBox.add(jPanel);
jf.add(createHorizontalBox);
// 组装底部
JPanel jPanel2 = new JPanel();
jPanel2.add(jLabel);
jPanel2.add(jTextField);
jf.add(jPanel2, BorderLayout.SOUTH);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] age) {
new ListModelTest().init();
}
}
NumberListModel
package test.object.model;
import java.math.BigDecimal;
import java.math.RoundingMode;
import javax.swing.AbstractListModel;
public class NumberListModel extends AbstractListModel<BigDecimal> {
BigDecimal start;
BigDecimal end;
BigDecimal step;
public NumberListModel(BigDecimal start, BigDecimal end, BigDecimal step) {
this.start = start;
this.end = end;
this.step = step;
}
@Override
public int getSize() {
// 计算一共多少个项
int doubleValue = (int) Math.floor(end.subtract(start).divide(step, 2, RoundingMode.HALF_DOWN).doubleValue());
return doubleValue + 1;
}
@Override
public BigDecimal getElementAt(int index) {
// 根据指定的索引获取条目
BigDecimal setScale = new BigDecimal(index).multiply(step).add(start).setScale(2, RoundingMode.HALF_DOWN);
return setScale;
}
}
NumberComboBoxList列表项目
package test.object.model;
import java.math.BigDecimal;
import java.math.RoundingMode;
import javax.swing.ComboBoxModel;
public class NumberComboBoxList extends NumberListModel implements ComboBoxModel<BigDecimal> {
private int seletedID;
public NumberComboBoxList(BigDecimal start, BigDecimal end, BigDecimal step) {
super(start, end, step);
// TODO Auto-generated constructor stub
}
@Override
public void setSelectedItem(Object anItem) {
// 设置选中条目的id
if (anItem instanceof BigDecimal) {
BigDecimal curr = (BigDecimal) anItem;
seletedID = curr.subtract(super.start).divide(super.step, 2, RoundingMode.HALF_DOWN).intValue();
}
}
@Override
public Object getSelectedItem() {
// 获取条目选中的内容
BigDecimal item = new BigDecimal(seletedID).multiply(super.step).add(super.start).setScale(1,
RoundingMode.HALF_DOWN);
return item;
}
}
网友评论