Java swing 根据集合给一份列表框 列表框有设置状态的单选按钮、编辑的普通按钮、增加的普通按钮和删除的普通按钮
时间: 2024-01-22 07:18:04 浏览: 129
可以使用 JList 和 ListSelectionListener 来实现根据集合给一份列表框,并添加单选按钮、编辑按钮、增加按钮和删除按钮。以下是一个示例代码:
```java
import javax.swing.*;
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.util.ArrayList;
public class MyList extends JPanel implements ListSelectionListener {
private JList<String> list;
private ArrayList<String> data;
private JRadioButton activeButton;
private JButton editButton;
private JButton addButton;
private JButton deleteButton;
public MyList() {
super(new BorderLayout());
data = new ArrayList<String>();
data.add("Item 1");
data.add("Item 2");
data.add("Item 3");
data.add("Item 4");
list = new JList<String>(data.toArray(new String[0]));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
JScrollPane listScrollPane = new JScrollPane(list);
activeButton = new JRadioButton("Active");
activeButton.setSelected(true);
editButton = new JButton("Edit");
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = list.getSelectedIndex();
if (index >= 0) {
String item = data.get(index);
String newItem = JOptionPane.showInputDialog(null, "Edit Item", item);
if (newItem != null) {
data.set(index, newItem);
list.setListData(data.toArray(new String[0]));
list.setSelectedIndex(index);
}
}
}
});
addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String newItem = JOptionPane.showInputDialog(null, "Add Item");
if (newItem != null) {
data.add(newItem);
list.setListData(data.toArray(new String[0]));
list.setSelectedIndex(data.size() - 1);
}
}
});
deleteButton = new JButton("Delete");
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = list.getSelectedIndex();
if (index >= 0) {
data.remove(index);
list.setListData(data.toArray(new String[0]));
int size = data.size();
if (size == 0) {
editButton.setEnabled(false);
deleteButton.setEnabled(false);
} else {
if (index == size) {
index--;
}
list.setSelectedIndex(index);
}
}
}
});
JPanel buttonPanel = new JPanel(new GridLayout(0, 1));
buttonPanel.add(activeButton);
buttonPanel.add(editButton);
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
add(listScrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.EAST);
}
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() == -1) {
editButton.setEnabled(false);
deleteButton.setEnabled(false);
} else {
editButton.setEnabled(true);
deleteButton.setEnabled(true);
}
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("MyList");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new MyList();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
```
在这个例子中,我们创建了一个名为 MyList 的 JPanel,其中包含了一个 JList 和一些按钮。我们使用 ArrayList 来存储列表的数据,并使用 JList 来显示这些数据。根据需求,我们可以使用 add、remove 和 set 方法来添加、删除和编辑列表项。
在 JList 上添加 ListSelectionListener,以便在用户选择不同的列表项时更新按钮的状态。我们还为每个按钮添加了 ActionListener,以便在用户点击时执行相应的操作。
阅读全文