下面是Java图形用户界面之列表框的完整攻略:
一、什么是列表框?
列表框(JList)是 Java Swing 组件之一,它除了能够显示列表以外,还能与其他组件协同工作,允许用户进行选择和编辑,非常适用于多项选择的情况下。
二、列表框的基本用法
1. 创建列表框
使用 JList
类创建一个列表框:
JList list = new JList();
2. 给列表框设置数据模型
列表框的数据模型是列表显示的内容,可以使用 ListModel
接口来设置:
String[] data = {"项1", "项2", "项3", "项4"};
ListModel listModel = new DefaultListModel();
for (String d : data) {
listModel.addElement(d);
}
JList list = new JList(listModel);
除此之外,还可以使用 Vector
或者其他集合类来提供数据源:
Vector<String> data = new Vector<>();
data.add("项1");
data.add("项2");
data.add("项3");
JList list = new JList(data);
3. 将列表框添加到面板中
以 JFrame
和 JPanel
为例:
JPanel panel = new JPanel();
panel.add(list);
JFrame frame = new JFrame();
frame.add(panel);
4. 监听列表框的选择事件
当用户选择列表中的项时,可以监听列表框的选择事件来处理:
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// 处理选择事件
}
});
三、列表框的高级用法
1. 自定义列表项的显示
可以使用 ListCellRenderer
接口来自定义列表项的显示,使其更符合实际需求。例如,为每个列表项增加一个复选框:
class CheckBoxRenderer extends JCheckBox implements ListCellRenderer {
public CheckBoxRenderer() {
setOpaque(true);
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
setText(value.toString());
setSelected(isSelected);
return this;
}
}
JList list = new JList(data);
list.setCellRenderer(new CheckBoxRenderer());
2. 在列表框中显示复杂组件
在某些情况下,可能需要在列表框中显示更复杂的组件,例如按钮和标签等。可以使用 JPanel
将这些组件组装起来,然后设置成列表项的显示组件:
class ButtonPanelRenderer implements ListCellRenderer {
private JPanel panel;
private JLabel label;
private JButton button;
public ButtonPanelRenderer() {
panel = new JPanel();
label = new JLabel();
button = new JButton("按钮");
panel.setLayout(new BorderLayout());
panel.add(label, BorderLayout.CENTER);
panel.add(button, BorderLayout.EAST);
panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
panel.setOpaque(true);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
label.setText(value.toString());
return panel;
}
}
JList list = new JList(data);
list.setCellRenderer(new ButtonPanelRenderer());
四、示例说明
1. 实现一个复选框列表
以下示例实现了一个复选框列表,每个列表项都带有一个复选框和标签。当用户选择某个复选框时,文字显示为删除线的效果。
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class CheckBoxListDemo {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String[] data = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
JList list = new JList(data);
class CheckBoxRenderer extends JCheckBox implements ListCellRenderer {
private Border border = BorderFactory.createEmptyBorder(2, 2, 2, 2);
public CheckBoxRenderer() {
setOpaque(true);
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
setEnabled(list.isEnabled());
setSelected(((CheckBoxList) list).isItemSelected(index));
setText(value.toString());
setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
setFont(list.getFont());
setBorder(border);
return this;
}
}
list.setCellRenderer(new CheckBoxRenderer());
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int index = list.locationToIndex(e.getPoint());
if (index != -1) {
JCheckBox checkbox = (JCheckBox) list.getCellRenderer().getListCellRendererComponent(list, list.getModel().getElementAt(index), index, false, false);
checkbox.setSelected(!checkbox.isSelected());
((CheckBoxList) list).setItemSelection(index, checkbox.isSelected());
list.repaint();
}
}
};
list.addMouseListener(mouseAdapter);
panel.add(list);
frame.add(panel);
frame.setPreferredSize(new Dimension(300, 200));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class CheckBoxList extends JList {
protected int[] selectedIndexes;
public CheckBoxList() {
this.selectedIndexes = new int[]{};
}
public boolean isItemSelected(int index) {
for (int i : selectedIndexes) {
if (i == index) {
return true;
}
}
return false;
}
public void setItemSelection(int index, boolean isSelected) {
if (isSelected) {
addSelectedIndex(index);
} else {
removeSelectedIndex(index);
}
}
private void addSelectedIndex(int index) {
int n = selectedIndexes.length;
int[] newIndexes = new int[n + 1];
System.arraycopy(selectedIndexes, 0, newIndexes, 0, n);
newIndexes[n] = index;
selectedIndexes = newIndexes;
}
private void removeSelectedIndex(int index) {
int n = selectedIndexes.length;
int[] newIndexes = new int[n - 1];
for (int i = 0, j = 0; i < n; i++) {
if (selectedIndexes[i] != index) {
newIndexes[j++] = selectedIndexes[i];
}
}
selectedIndexes = newIndexes;
}
}
}
2. 实现一个列表框带有按钮列表
以下示例实现了一个列表框,每个列表项都包含一个标签和按钮。当用户点击某个按钮时,该项将被从列表中删除。
import javax.swing.*;
import java.awt.*;
public class ButtonListDemo {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String[] data = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
JList list = new JList(data);
class ButtonPanelRenderer implements ListCellRenderer {
private JPanel panel;
private JLabel label;
private JButton button;
public ButtonPanelRenderer() {
panel = new JPanel();
label = new JLabel();
button = new JButton("删除");
panel.setLayout(new BorderLayout());
panel.add(label, BorderLayout.CENTER);
panel.add(button, BorderLayout.EAST);
panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
panel.setOpaque(true);
button.addActionListener(e -> {
int index = list.getSelectedIndex();
if (index != -1) {
DefaultListModel model = (DefaultListModel) list.getModel();
model.remove(index);
}
});
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
label.setText(value.toString());
return panel;
}
}
list.setCellRenderer(new ButtonPanelRenderer());
panel.add(list);
frame.add(panel);
frame.setPreferredSize(new Dimension(300, 200));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java图形用户界面之列表框 - Python技术站