/* * ListFrame.java * * Created on 2006年4月14日, 下午4:21 */package testwithnb; import javax.swing.*;/** * * @author 肖磊 */ public class ListFrame extends javax.swing.JFrame { private DefaultListModel lm = new DefaultListModel(); /** Creates new form ListFrame */ public ListFrame() { initComponents(); for(int i=0; i<10; i++) lm.addElement("Item-"+i); }
/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc=" 生成的代码 "> private void initComponents() { jList1 = new javax.swing.JList(); jList1.setModel(lm); jPanel1 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().add(jList1, java.awt.BorderLayout.CENTER); jButton1.setText("Up"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jPanel1.add(jButton1); jButton2.setText("Down"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jPanel1.add(jButton2); getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300); } // </editor-fold> private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { int ind = jList1.getSelectedIndex(); if(ind>=0){ Object o = lm.remove(ind); if(ind<lm.getSize()) ind++; lm.insertElementAt(o, ind); jList1.setSelectedIndex(ind); } }
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO 将在此处添加您的处理代码: int ind = jList1.getSelectedIndex(); if(ind>=0){ Object o = lm.remove(ind); if(ind>0) ind--; lm.insertElementAt(o, ind); jList1.setSelectedIndex(ind); } }
/** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ListFrame().setVisible(true); } }); }
* ListFrame.java
*
* Created on 2006年4月14日, 下午4:21
*/package testwithnb;
import javax.swing.*;/**
*
* @author 肖磊
*/
public class ListFrame extends javax.swing.JFrame {
private DefaultListModel lm = new DefaultListModel();
/** Creates new form ListFrame */
public ListFrame() {
initComponents();
for(int i=0; i<10; i++)
lm.addElement("Item-"+i);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
private void initComponents() {
jList1 = new javax.swing.JList();
jList1.setModel(lm);
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(jList1, java.awt.BorderLayout.CENTER); jButton1.setText("Up");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
}); jPanel1.add(jButton1); jButton2.setText("Down");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
}); jPanel1.add(jButton2); getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
}
// </editor-fold> private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int ind = jList1.getSelectedIndex();
if(ind>=0){
Object o = lm.remove(ind);
if(ind<lm.getSize()) ind++;
lm.insertElementAt(o, ind);
jList1.setSelectedIndex(ind);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO 将在此处添加您的处理代码:
int ind = jList1.getSelectedIndex();
if(ind>=0){
Object o = lm.remove(ind);
if(ind>0) ind--;
lm.insertElementAt(o, ind);
jList1.setSelectedIndex(ind);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ListFrame().setVisible(true);
}
});
}
// 变量声明 - 不进行修改
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JList jList1;
private javax.swing.JPanel jPanel1;
// 变量声明结束
}
撒