中易网

java swing 中的列表框JList如何在程序中动态的添加和删除元素

答案:3  悬赏:10  
解决时间 2021-01-15 10:18
  • 提问者网友:山高云阔
  • 2021-01-14 23:53
java swing 中的列表框JList如何在程序中动态的添加和删除元素
最佳答案
  • 二级知识专家网友:酒安江南
  • 2021-01-15 00:34
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.WindowConstants;

public class ListFrame extends javax.swing.JFrame {
private JList jList1;
private JButton jButton1;
private JButton jButton2;
private JList jList2;
private DefaultListModel listModel1;
private DefaultListModel listModel2;
private JButton jButton4;
private JButton jButton3;

{
//Set Look & Feel
try {
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch(Exception e) {
e.printStackTrace();
}
}


public static void main(String[] args) {
ListFrame inst = new ListFrame();
inst.setVisible(true);
}

public ListFrame() {
super();
initGUI();
}

private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
listModel1 = new DefaultListModel();
listModel1.addElement("item 1");
listModel1.addElement("item 2");
listModel1.addElement("item 3");
listModel1.addElement("item 4");
listModel1.addElement("item 5");
listModel1.addElement("item 6");
jList1 = new JList(listModel1);
getContentPane().add(jList1);
jList1.setBounds(42, 28, 119, 203);
}
{
listModel2 = new DefaultListModel();
jList2 = new JList(listModel2);
getContentPane().add(jList2);
jList2.setBounds(238, 28, 119, 196);
}
{
jButton1 = new JButton();
getContentPane().add(jButton1);
jButton1.setText(">");
jButton1.setBounds(168, 84, 63, 28);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
}
{
jButton2 = new JButton();
getContentPane().add(jButton2);
jButton2.setText("<");
jButton2.setBounds(168, 133, 63, 28);
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
}
{
jButton3 = new JButton();
getContentPane().add(jButton3);
jButton3.setText("添加");
jButton3.setBounds(168, 35, 63, 28);
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
}
{
jButton4 = new JButton();
getContentPane().add(jButton4);
jButton4.setText("删除");
jButton4.setBounds(168, 189, 63, 28);
jButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
}
pack();
setSize(400, 300);
setLocationRelativeTo(null);
} catch (Exception e) {
e.printStackTrace();
}
}

private void jButton1ActionPerformed(ActionEvent evt) {
if(jList1.getSelectedIndex()!=-1){
listModel2.addElement(jList1.getSelectedValue());
int i=jList1.getSelectedIndex();
listModel1.remove(i);
jList1.setSelectedIndex(i>0? i-1:0);
jList2.setSelectedIndex(listModel2.size()-1);
}
}

private void jButton2ActionPerformed(ActionEvent evt) {
if(jList2.getSelectedIndex()!=-1){
listModel1.addElement(jList2.getSelectedValue());
int i=jList2.getSelectedIndex();
listModel2.remove(i);
jList2.setSelectedIndex(i>0? i-1:0);
}
}

private void jButton3ActionPerformed(ActionEvent evt) {
listModel1.addElement("new item");
}

private void jButton4ActionPerformed(ActionEvent evt) {
if(jList1.getSelectedIndex()!=-1){
listModel1.remove(jList1.getSelectedIndex());
}

}

}
全部回答
  • 1楼网友:拾荒鲤
  • 2021-01-15 02:22
写的比较草,添了些注释
有问题给我发信息吧
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class TestJList extends JFrame implements ActionListener {
JList jl;
JScrollPane jsp;// 滑块
DefaultListModel dlm;// Jlist里面的内容的对象
JButton jb1;// 删除按钮
JButton jb2;// 添加按钮
JPanel jp;// 放2个按钮的panel
JPanel jp2;// 放文本框的panel
JTextField jtf;// 文本框,用于输入要插入的文字
public TestJList() {
// TODO Auto-generated constructor stub
jb1 = new JButton("删除");
jb2 = new JButton("添加");
jp = new JPanel();
jp2 = new JPanel();
jtf = new JTextField("请在这里输入想添加的内容");
dlm = new DefaultListModel();
jl = new JList(dlm);// 创建一个包含DefaultListModel的Jlist
jsp = new JScrollPane(jl);// 创建一个包含Jlist的滑块
// 以下是布局,大体就是把按钮添加到panel,把panel添加到窗体
jp.setLayout(new FlowLayout());
jp.add(jb1);
jp.add(jb2);
jp2.setLayout(new FlowLayout());
jp2.add(jtf);
setLayout(new GridLayout(3, 1));
add(jsp);
add(jp2);
add(jp);
jb1.addActionListener(this);// 把监听注册个按钮
jb2.addActionListener(this);// 把监听注册个按钮
setSize(200, 400);
setVisible(true);
// 添加一个点击右上“叉”关闭窗口的事件
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); // 关闭
}
});
}
// 添加一个按钮的监听
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// 如果按的是删除按钮
if (e.getSource() == jb1) {
// 移除当前选择的内容
dlm.remove(jl.getSelectedIndex());
}
// 如果按的是田间按钮
if (e.getSource() == jb2) {
// 吧文本框中的内容添加到列表
dlm.addElement(jtf.getText());
}
}
// 测试主函数
public static void main(String[] args) {
TestJList tj = new TestJList();
}
}
  • 2楼网友:老鼠爱大米
  • 2021-01-15 01:18
简单。
DefaultListModel model = new DefaulatListModel();
JList list = new JList(model);
model方法跟向量基本一致,model怎么添加删除list就怎么变。
list.isSelectedIndex()表示一个元素是否被选。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息