jList1的第一个元素要添加到jList2怎么弄?????
15 个解决方案
#1
把它转换成ArrayList 类型 通过get(int index)就可以取出指定位置的元素了
#2
jList2。add(jList1。get(0))
#3
其实这里有个问题
就是楼主把这个元素从list1移到list2之后
还要不要在list1上显示?
就是楼主把这个元素从list1移到list2之后
还要不要在list1上显示?
#4
jList1能转成ArrayList?
怎么转啊
#5
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
public class TestJList extends JFrame {
private JList jl1;
private Vector<String> v1;
private JList jl2;
private Vector<String> v2;
public TestJList() {
this.setTitle("JList测试");
this.setSize(800, 600);
v1 = new Vector<String>();
v1.add("liuming");
v1.add("wang");
v1.add("zhang");
v1.add("sun");
jl1 = new JList(v1);
jl1.addMouseListener(new MouseAdapter() {
//在JList1中单击一次鼠标,就把选中的项加入到JList2
@Override
public void mouseClicked(MouseEvent e) {
v2.add(v1.get(jl1.getSelectedIndex()));
jl2.updateUI();
}
});
this.setLayout(new GridLayout(1,2));
v2 = new Vector<String>();
v2.add("JList2");
jl2 = new JList(v2);
this.add(jl1);
this.add(jl2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJList();
}
}
#6
转成ArrayList干吗,看看JList里面的方法。
getModel
public ListModel getModel()返回保存由 JList 组件显示的项列表的数据模型。
返回:
提供显示的项列表的 ListModel
#7
楼主啊,建议多看看API,这个API中都有的!JList有个getModel()方法,返回 ListModel, ListModel接口中有相应的方法getElementAt(int index),这不就结了吗?
所以上面的mouseClicked方法中的代码可以写成一下的:
所以上面的mouseClicked方法中的代码可以写成一下的:
v2.add((String)jl1.getModel().getElementAt(jl1.getSelectedIndex()));
jl2.updateUI();
#8
我是要弄这么个东西,给我说说怎么弄合适啊哥们
#9
#10
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
public class TestJList extends JFrame implements ActionListener {
private JList jl1;
private Vector<String> v1;
private JList jl2;
private Vector<String> v2;
public TestJList() {
this.setTitle("JList测试");
this.setSize(800, 600);
v1 = new Vector<String>();
v1.add("liuming");
v1.add("wang");
v1.add("zhang");
v1.add("sun");
jl1 = new JList(v1);
JButton jb1 = new JButton("->");
jb1.addActionListener(this);
JButton jb2 = new JButton("->>");
jb2.addActionListener(this);
JButton jb3 = new JButton("<-");
jb3.addActionListener(this);
JButton jb4 = new JButton("<<-");
jb4.addActionListener(this);
this.setLayout(new GridLayout(1,3));
v2 = new Vector<String>();
v2.add("JList2");
jl2 = new JList(v2);
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
jp3.setLayout(new FlowLayout());
jp3.add(jb1);
jp3.add(jb2);
jp3.add(jb3);
jp3.add(jb4);
jp1.add(jl1);
this.add(jp1);
jp2.add(jl2);
this.add(jp3);
this.add(jp2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJList();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("->")) {
if(jl1.getSelectedIndex() >= 0) {
v2.add(v1.get(jl1.getSelectedIndex()));
v1.remove(jl1.getSelectedIndex());
jl1.updateUI();
jl2.updateUI();
}
} else if(e.getActionCommand().equals("->>")) {
for(int i=0; i<v1.size(); i++) {
v2.add(v1.get(i));
}
v1.clear();
jl1.updateUI();
jl2.updateUI();
} else if(e.getActionCommand().equals("<-")) {
if(jl2.getSelectedIndex() >= 0) {
v1.add(v2.get(jl2.getSelectedIndex()));
v2.remove(jl2.getSelectedIndex());
jl1.updateUI();
jl2.updateUI();
}
} else if(e.getActionCommand().equals("<<-")) {
for(int i=0; i<v2.size(); i++) {
v1.add(v2.get(i));
}
v2.clear();
jl1.updateUI();
jl2.updateUI();
}
}
}
#11
这样就行了,我上面的布局不好!代码写的乱,见笑了!
#12
楼主,我也写了一个,自检情况下没有出错
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
public class List1_2 extends JFrame {
private Vector<String> v1;
private Vector<String> v2;
private JList list1;
private JList list2;
private JScrollPane scr1;
private JScrollPane scr2;
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btnOk;
private JButton btnCancel;
public List1_2() {
super();
this.setSize(400,320);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
Font font = new Font("Dialog",Font.PLAIN,12);
UIManager.put("Button.font",font);
UIManager.put("List.font",font);
Container cont = getContentPane();
cont.setLayout(null);
v1 = new Vector<String>(0);
v1.add("1");
v1.add("2");
v1.add("3");
v1.add("4");
v2 = new Vector<String>(0);
list1 = new JList(v1);
list2 = new JList(v2);
scr1 = new JScrollPane(list1);
scr2 = new JScrollPane(list2);
btn1 = new JButton("-->");
btn2 = new JButton("->>");
btn3 = new JButton("<--");
btn4 = new JButton("<<-");
btnOk = new JButton("确定");
btnCancel = new JButton("取消");
scr1.setBounds(50, 30, 100, 200);
scr2.setBounds(250, 30, 100, 200);
btn1.setBounds(170, 50, 60, 20);
btn2.setBounds(170, 80, 60, 20);
btn3.setBounds(170, 140, 60, 20);
btn4.setBounds(170, 170, 60, 20);
btnOk.setBounds(200, 250, 60, 20);
btnCancel.setBounds(280, 250, 60, 20);
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int index = list1.getSelectedIndex();
if (index < 0 || index >= v1.size()) {
return;
}
v2.add(v1.get(index));
v1.remove(index);
list1.updateUI();
list2.updateUI();
}
});
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
v2.addAll(v1);
v1.clear();
list1.updateUI();
list2.updateUI();
}
});
btn3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int index = list2.getSelectedIndex();
if (index < 0 || index >= v2.size()) {
return;
}
v1.add(v2.get(index));
v2.remove(index);
list1.updateUI();
list2.updateUI();
}
});
btn4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
v1.addAll(v2);
v2.clear();
list1.updateUI();
list2.updateUI();
}
});
cont.add(scr1);
cont.add(scr2);
cont.add(btn1);
cont.add(btn2);
cont.add(btn3);
cont.add(btn4);
cont.add(btnOk);
cont.add(btnCancel);
}
public static void main(String[] args) {
List1_2 list12 = new List1_2();
list12.setVisible(true);
}
}
#13
谢谢了哥们,我试试去
#14
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class JListTest extends javax.swing.JFrame implements ActionListener {
private JList jl1;
private JButton jb2;
private JButton jb4;
private JButton jb3;
private JButton jb1;
private JPanel jp1;
private JList jl2;
private Vector<String> v1;
private Vector<String> v2;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JListTest inst = new JListTest();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
inst.setResizable(false);
}
});
}
public JListTest() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
v1 = new Vector<String>();
v1.add("liu");
v1.add("ming");
v1.add("song");
v1.add("wang");
jl1 = new JList(v1);
FlowLayout jl1Layout = new FlowLayout();
jl1.setLayout(jl1Layout);
getContentPane().add(jl1, BorderLayout.WEST);
jl1.setPreferredSize(new java.awt.Dimension(200, 366));
}
{
v2 = new Vector<String>();
jl2 = new JList(v2);
FlowLayout jl2Layout = new FlowLayout();
jl2.setLayout(null);
getContentPane().add(jl2, BorderLayout.EAST);
jl2.setPreferredSize(new java.awt.Dimension(246, 366));
}
{
jp1 = new JPanel();
FlowLayout jp1Layout = new FlowLayout();
jp1.setLayout(jp1Layout);
getContentPane().add(jp1, BorderLayout.CENTER);
jp1.setPreferredSize(new java.awt.Dimension(87, 366));
{
jb1 = new JButton();
jp1.add(jb1);
jb1.setText("->");
jb1.addActionListener(this);
}
{
jb2 = new JButton();
jp1.add(jb2);
jb2.setText("->>");
jb2.addActionListener(this);
}
{
jb3 = new JButton();
jp1.add(jb3);
jb3.setText("<-");
jb3.addActionListener(this);
}
{
jb4 = new JButton();
jp1.add(jb4);
jb4.setText("<<-");
jb4.addActionListener(this);
}
}
pack();
setSize(500, 400);
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("->")) {
int index = jl1.getSelectedIndex();
if(index >= 0) {
v2.add(v1.get(index));
v1.remove(index);
jl1.updateUI();
jl2.updateUI();
} else {
JOptionPane.showMessageDialog(null, "请先选中一项");
}
} else if(e.getActionCommand().equals("->>")) {
for(int i=0; i<v1.size(); i++) {
v2.add(v1.get(i));
}
v1.clear();
jl1.updateUI();
jl2.updateUI();
} else if(e.getActionCommand().equals("<-")) {
int index = jl2.getSelectedIndex();
if(index >= 0) {
v1.add(v2.get(index));
v2.remove(index);
jl1.updateUI();
jl2.updateUI();
} else {
JOptionPane.showMessageDialog(null, "请先选中一项");
}
} else if(e.getActionCommand().equals("<<-")) {
for(int i=0; i<v2.size(); i++) {
v1.add(v2.get(i));
}
v2.clear();
jl1.updateUI();
jl2.updateUI();
}
}
}
有弄了个,界面看着好看些的!
#15
楼上的真负责……
不过要做的像楼主给的那张图一样,
还是null布局最方便
我写的那个就差不多 -_-||
不过要做的像楼主给的那张图一样,
还是null布局最方便
我写的那个就差不多 -_-||
#1
把它转换成ArrayList 类型 通过get(int index)就可以取出指定位置的元素了
#2
jList2。add(jList1。get(0))
#3
其实这里有个问题
就是楼主把这个元素从list1移到list2之后
还要不要在list1上显示?
就是楼主把这个元素从list1移到list2之后
还要不要在list1上显示?
#4
jList1能转成ArrayList?
怎么转啊
#5
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
public class TestJList extends JFrame {
private JList jl1;
private Vector<String> v1;
private JList jl2;
private Vector<String> v2;
public TestJList() {
this.setTitle("JList测试");
this.setSize(800, 600);
v1 = new Vector<String>();
v1.add("liuming");
v1.add("wang");
v1.add("zhang");
v1.add("sun");
jl1 = new JList(v1);
jl1.addMouseListener(new MouseAdapter() {
//在JList1中单击一次鼠标,就把选中的项加入到JList2
@Override
public void mouseClicked(MouseEvent e) {
v2.add(v1.get(jl1.getSelectedIndex()));
jl2.updateUI();
}
});
this.setLayout(new GridLayout(1,2));
v2 = new Vector<String>();
v2.add("JList2");
jl2 = new JList(v2);
this.add(jl1);
this.add(jl2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJList();
}
}
#6
转成ArrayList干吗,看看JList里面的方法。
getModel
public ListModel getModel()返回保存由 JList 组件显示的项列表的数据模型。
返回:
提供显示的项列表的 ListModel
#7
楼主啊,建议多看看API,这个API中都有的!JList有个getModel()方法,返回 ListModel, ListModel接口中有相应的方法getElementAt(int index),这不就结了吗?
所以上面的mouseClicked方法中的代码可以写成一下的:
所以上面的mouseClicked方法中的代码可以写成一下的:
v2.add((String)jl1.getModel().getElementAt(jl1.getSelectedIndex()));
jl2.updateUI();
#8
我是要弄这么个东西,给我说说怎么弄合适啊哥们
#9
#10
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
public class TestJList extends JFrame implements ActionListener {
private JList jl1;
private Vector<String> v1;
private JList jl2;
private Vector<String> v2;
public TestJList() {
this.setTitle("JList测试");
this.setSize(800, 600);
v1 = new Vector<String>();
v1.add("liuming");
v1.add("wang");
v1.add("zhang");
v1.add("sun");
jl1 = new JList(v1);
JButton jb1 = new JButton("->");
jb1.addActionListener(this);
JButton jb2 = new JButton("->>");
jb2.addActionListener(this);
JButton jb3 = new JButton("<-");
jb3.addActionListener(this);
JButton jb4 = new JButton("<<-");
jb4.addActionListener(this);
this.setLayout(new GridLayout(1,3));
v2 = new Vector<String>();
v2.add("JList2");
jl2 = new JList(v2);
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
jp3.setLayout(new FlowLayout());
jp3.add(jb1);
jp3.add(jb2);
jp3.add(jb3);
jp3.add(jb4);
jp1.add(jl1);
this.add(jp1);
jp2.add(jl2);
this.add(jp3);
this.add(jp2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJList();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("->")) {
if(jl1.getSelectedIndex() >= 0) {
v2.add(v1.get(jl1.getSelectedIndex()));
v1.remove(jl1.getSelectedIndex());
jl1.updateUI();
jl2.updateUI();
}
} else if(e.getActionCommand().equals("->>")) {
for(int i=0; i<v1.size(); i++) {
v2.add(v1.get(i));
}
v1.clear();
jl1.updateUI();
jl2.updateUI();
} else if(e.getActionCommand().equals("<-")) {
if(jl2.getSelectedIndex() >= 0) {
v1.add(v2.get(jl2.getSelectedIndex()));
v2.remove(jl2.getSelectedIndex());
jl1.updateUI();
jl2.updateUI();
}
} else if(e.getActionCommand().equals("<<-")) {
for(int i=0; i<v2.size(); i++) {
v1.add(v2.get(i));
}
v2.clear();
jl1.updateUI();
jl2.updateUI();
}
}
}
#11
这样就行了,我上面的布局不好!代码写的乱,见笑了!
#12
楼主,我也写了一个,自检情况下没有出错
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
public class List1_2 extends JFrame {
private Vector<String> v1;
private Vector<String> v2;
private JList list1;
private JList list2;
private JScrollPane scr1;
private JScrollPane scr2;
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btnOk;
private JButton btnCancel;
public List1_2() {
super();
this.setSize(400,320);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
Font font = new Font("Dialog",Font.PLAIN,12);
UIManager.put("Button.font",font);
UIManager.put("List.font",font);
Container cont = getContentPane();
cont.setLayout(null);
v1 = new Vector<String>(0);
v1.add("1");
v1.add("2");
v1.add("3");
v1.add("4");
v2 = new Vector<String>(0);
list1 = new JList(v1);
list2 = new JList(v2);
scr1 = new JScrollPane(list1);
scr2 = new JScrollPane(list2);
btn1 = new JButton("-->");
btn2 = new JButton("->>");
btn3 = new JButton("<--");
btn4 = new JButton("<<-");
btnOk = new JButton("确定");
btnCancel = new JButton("取消");
scr1.setBounds(50, 30, 100, 200);
scr2.setBounds(250, 30, 100, 200);
btn1.setBounds(170, 50, 60, 20);
btn2.setBounds(170, 80, 60, 20);
btn3.setBounds(170, 140, 60, 20);
btn4.setBounds(170, 170, 60, 20);
btnOk.setBounds(200, 250, 60, 20);
btnCancel.setBounds(280, 250, 60, 20);
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int index = list1.getSelectedIndex();
if (index < 0 || index >= v1.size()) {
return;
}
v2.add(v1.get(index));
v1.remove(index);
list1.updateUI();
list2.updateUI();
}
});
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
v2.addAll(v1);
v1.clear();
list1.updateUI();
list2.updateUI();
}
});
btn3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int index = list2.getSelectedIndex();
if (index < 0 || index >= v2.size()) {
return;
}
v1.add(v2.get(index));
v2.remove(index);
list1.updateUI();
list2.updateUI();
}
});
btn4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
v1.addAll(v2);
v2.clear();
list1.updateUI();
list2.updateUI();
}
});
cont.add(scr1);
cont.add(scr2);
cont.add(btn1);
cont.add(btn2);
cont.add(btn3);
cont.add(btn4);
cont.add(btnOk);
cont.add(btnCancel);
}
public static void main(String[] args) {
List1_2 list12 = new List1_2();
list12.setVisible(true);
}
}
#13
谢谢了哥们,我试试去
#14
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class JListTest extends javax.swing.JFrame implements ActionListener {
private JList jl1;
private JButton jb2;
private JButton jb4;
private JButton jb3;
private JButton jb1;
private JPanel jp1;
private JList jl2;
private Vector<String> v1;
private Vector<String> v2;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JListTest inst = new JListTest();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
inst.setResizable(false);
}
});
}
public JListTest() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
v1 = new Vector<String>();
v1.add("liu");
v1.add("ming");
v1.add("song");
v1.add("wang");
jl1 = new JList(v1);
FlowLayout jl1Layout = new FlowLayout();
jl1.setLayout(jl1Layout);
getContentPane().add(jl1, BorderLayout.WEST);
jl1.setPreferredSize(new java.awt.Dimension(200, 366));
}
{
v2 = new Vector<String>();
jl2 = new JList(v2);
FlowLayout jl2Layout = new FlowLayout();
jl2.setLayout(null);
getContentPane().add(jl2, BorderLayout.EAST);
jl2.setPreferredSize(new java.awt.Dimension(246, 366));
}
{
jp1 = new JPanel();
FlowLayout jp1Layout = new FlowLayout();
jp1.setLayout(jp1Layout);
getContentPane().add(jp1, BorderLayout.CENTER);
jp1.setPreferredSize(new java.awt.Dimension(87, 366));
{
jb1 = new JButton();
jp1.add(jb1);
jb1.setText("->");
jb1.addActionListener(this);
}
{
jb2 = new JButton();
jp1.add(jb2);
jb2.setText("->>");
jb2.addActionListener(this);
}
{
jb3 = new JButton();
jp1.add(jb3);
jb3.setText("<-");
jb3.addActionListener(this);
}
{
jb4 = new JButton();
jp1.add(jb4);
jb4.setText("<<-");
jb4.addActionListener(this);
}
}
pack();
setSize(500, 400);
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("->")) {
int index = jl1.getSelectedIndex();
if(index >= 0) {
v2.add(v1.get(index));
v1.remove(index);
jl1.updateUI();
jl2.updateUI();
} else {
JOptionPane.showMessageDialog(null, "请先选中一项");
}
} else if(e.getActionCommand().equals("->>")) {
for(int i=0; i<v1.size(); i++) {
v2.add(v1.get(i));
}
v1.clear();
jl1.updateUI();
jl2.updateUI();
} else if(e.getActionCommand().equals("<-")) {
int index = jl2.getSelectedIndex();
if(index >= 0) {
v1.add(v2.get(index));
v2.remove(index);
jl1.updateUI();
jl2.updateUI();
} else {
JOptionPane.showMessageDialog(null, "请先选中一项");
}
} else if(e.getActionCommand().equals("<<-")) {
for(int i=0; i<v2.size(); i++) {
v1.add(v2.get(i));
}
v2.clear();
jl1.updateUI();
jl2.updateUI();
}
}
}
有弄了个,界面看着好看些的!
#15
楼上的真负责……
不过要做的像楼主给的那张图一样,
还是null布局最方便
我写的那个就差不多 -_-||
不过要做的像楼主给的那张图一样,
还是null布局最方便
我写的那个就差不多 -_-||