检查这个,在这个程序中,我有列表“城市列表”,我在其中添加了我的所有元素,之后我有列表“第二个列表”,IA 在其中移动我的元素,这个程序还反转了,意味着它移动元素,从第二个列表到城市列表。为了更好地理解,请运行此程序...
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
* Combo BOX pROBLEM
*/
public class ComboBoxProblem extends JFrame
{
// made them static , so I can use them over & over
static JLabel citiesLabel = new JLabel();
static JList citiesList = new JList();
static JScrollPane citiesScrollPane = new JScrollPane();
static JLabel SecondLabel = new JLabel();
static JList SecondList = new JList();
static JScrollPane SecondScrollPane = new JScrollPane();
// these are button to move the elements
static JButton AssignButton = new JButton();
static JButton RemoveButton = new JButton();
static JList made_list = new JList();
public static void main(String args[])
{
new ComboBoxProblem().show();
}
public ComboBoxProblem()
{
// create frame
setTitle("Flight Planner");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gridConstraints;
citiesLabel.setText("Destination City");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
gridConstraints.insets = new Insets(10, 0, 0, 0);
getContentPane().add(citiesLabel, gridConstraints);
citiesScrollPane.setPreferredSize(new Dimension(150, 100));
citiesScrollPane.setViewportView(citiesList);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 2;
gridConstraints.insets = new Insets(10, 10, 10, 10);
getContentPane().add(citiesScrollPane, gridConstraints);
final DefaultListModel List1= new DefaultListModel();
List1.addElement("San Diego");
List1.addElement("Los Angeles");
List1.addElement("Orange County");
List1.addElement("Ontario");
List1.addElement("Bakersfield");
citiesList.setModel(List1);
SecondLabel.setText("Moved");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 1;
gridConstraints.insets = new Insets(10, 0, 0, 0);
getContentPane().add(SecondLabel, gridConstraints);
SecondScrollPane.setPreferredSize(new Dimension(150, 100));
SecondScrollPane.setViewportView(SecondList);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 2;
gridConstraints.insets = new Insets(10, 10, 10, 10);
getContentPane().add(SecondScrollPane, gridConstraints);
final DefaultListModel List2 = new DefaultListModel();
SecondList.setModel(List2);
AssignButton.setText("Move");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 3;
gridConstraints.insets = new Insets(0, 0, 10, 0);
getContentPane().add(AssignButton, gridConstraints);
AssignButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i < List1.getSize(); i++)
{
if(citiesList.isSelectedIndex(i))
{
List2.addElement(List1.getElementAt(i));
List1.removeElementAt(i);
i--;
}
}
}
});
RemoveButton.setText("Reverse");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 3;
gridConstraints.insets = new Insets(0, 0, 10, 0);
getContentPane().add(RemoveButton, gridConstraints);
RemoveButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i < List2.getSize(); i++)
{
if(SecondList.isSelectedIndex(i))
{
List1.addElement(List2.getElementAt(i));
List2.removeElementAt(i);
i--;
}
}
}
});
pack();
}
}