java swing JList用法執行個體

來源:互聯網
上載者:User

package so;

import java.text.*;
import java.util.*;
import java.util.ArrayList;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import util.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.XYConstraints;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author By SamZheng
 * @version 1.0
 */

public class PMMShipingSpace
    extends JFrame {
  int cycle = 0;
  private boolean trigTable = true;
  JPanel contentPane;
  JPanel jpnlTop2 = new JPanel();
  XYLayout xYLayout1 = new XYLayout();
  JPanel jPanel1 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  BorderLayout borderLayout2 = new BorderLayout();
  ButtonGroup buttonGroup = new ButtonGroup();
  DefaultListModel listMode1 = new DefaultListModel();
  JList jList1 = new JList(listMode1);
  JScrollPane jscrollList1 = new JScrollPane(jList1);
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  JButton jButton4 = new JButton();
  DefaultListModel listMode2 = new DefaultListModel();
  JList jList2 = new JList(listMode2);
  JScrollPane jscrollList2 = new JScrollPane(jList2);

  public PMMShipingSpace() {
    try {
      jbInit();
      this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-this.getWidth())/2,
                     (Toolkit.getDefaultToolkit().getScreenSize().height-this.getHeight())/2);
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    getContentPane().setLayout(borderLayout1);
    contentPane = (JPanel) getContentPane();
    contentPane.setLayout(borderLayout2);
    jpnlTop2.setLayout(xYLayout1); //
    jPanel1.setLayout(borderLayout1);
    jPanel1.setMaximumSize(new Dimension(32767, 32767));
    jPanel1.setMinimumSize(new Dimension(706, 350));
    jPanel1.setPreferredSize(new Dimension(700, 220));
    jpnlTop2.setMinimumSize(new Dimension(690, 350));
    jpnlTop2.setPreferredSize(new Dimension(130, 160));
    contentPane.setMinimumSize(new Dimension(706, 662));
    jButton1.setMaximumSize(new Dimension(49, 25));
    jButton1.setMinimumSize(new Dimension(49, 25));
    jButton1.setPreferredSize(new Dimension(57, 25));
    jButton1.setText("->");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jButton2.setPreferredSize(new Dimension(57, 25));
    jButton2.setText("->>");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton2_actionPerformed(e);
      }
    });
    jButton3.setMaximumSize(new Dimension(49, 25));
    jButton3.setMinimumSize(new Dimension(49, 25));
    jButton3.setPreferredSize(new Dimension(57, 25));
    jButton3.setText("<-");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton3_actionPerformed(e);
      }
    });
    jButton4.setPreferredSize(new Dimension(57, 25));
    jButton4.setHorizontalAlignment(SwingConstants.CENTER);
    jButton4.setText("<<-");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton4_actionPerformed(e);
      }
    }); //載入資料
    contentPane.add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jpnlTop2, BorderLayout.CENTER);
    jpnlTop2.add(jscrollList1, new XYConstraints(43, 22, 132, 193));
    jpnlTop2.add(jButton4, new XYConstraints(178, 125, -1, 26));
    jpnlTop2.add(jButton3, new XYConstraints(178, 92, -1, 26));
    jpnlTop2.add(jButton2, new XYConstraints(177, 58, -1, 26));
    jpnlTop2.add(jButton1, new XYConstraints(176, 25, -1, 26));
    jpnlTop2.add(jscrollList2, new XYConstraints(235, 23, 129, 193));
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(480, 320);
    this.setTitle("JListTest"); //訂艙資訊維護//save//close
    setupList();
  }

  private void setupList() {
    listMode1.addElement("aaa");
    listMode1.addElement("bbb");
    listMode1.addElement("ccc");
    listMode1.addElement("ddd");
    listMode1.addElement("111");
    listMode1.addElement("222");
    listMode1.addElement("444");
    listMode1.addElement("555");
    listMode1.addElement("000");
    listMode1.addElement("888");
  }

  void jButton1_actionPerformed(ActionEvent e) {
    if (jList1.getSelectedIndices().length > 1) {
      int temp = -1;
      int t[] = jList1.getSelectedIndices();
      jList1.clearSelection();
      for (int i = 0; i < t.length; i++) {
        temp++;
        jList1.setSelectedIndex(t[i] - temp);
        listMode2.addElement(jList1.getSelectedValue().toString());
        listMode1.remove(t[i] - temp);
      }
      //jList sort
      listModelSort(listMode2);
      if (listMode1.size() > 0) {
        jList1.setSelectedIndex(0);
      }
      return;
    }
    if (jList1.getSelectedIndex() > -1) {
//      if (jList1.getSelectedIndex() > listMode2.size()) listMode2.setSize(
//          jList1.getSelectedIndex());
      listMode2.addElement(jList1.getSelectedValue().toString().trim());
      listMode1.remove(jList1.getSelectedIndex());
      //listMode1.removeElement(jList1.getSelectedValue().toString().trim());
    }
    //jList sort
    listModelSort(listMode2);
    if (listMode1.size() > 0) {
      jList1.setSelectedIndex(0);
    }
  }

  void jButton3_actionPerformed(ActionEvent e) {
    if (jList2.getSelectedIndices().length > 1) {
      int temp = -1;
      int t[] = jList2.getSelectedIndices();
      jList2.clearSelection();
      for (int i = 0; i < t.length; i++) {
        temp++;
        jList2.setSelectedIndex(t[i] - temp);
        listMode1.addElement(jList2.getSelectedValue().toString());
        listMode2.remove(t[i] - temp);
      }
      //jList sort
      listModelSort(listMode1);
      if (listMode2.size() > 0) {
        jList2.setSelectedIndex(0);
      }
      return;
    }
    if (jList2.getSelectedIndex() > -1) {
//      if (jList2.getSelectedIndex() > listMode1.size()) listMode1.setSize(
//          jList2.getSelectedIndex());
      listMode1.addElement(jList2.getSelectedValue().toString().trim());
      listMode2.remove(jList2.getSelectedIndex());
      //listMode1.removeElement(jList1.getSelectedValue().toString().trim());
    }
    //jList sort
    listModelSort(listMode1);
    if (listMode2.size() > 0) {
      jList2.setSelectedIndex(0);
    }
  }

  void jButton2_actionPerformed(ActionEvent e) {
    if (listMode1.size() > 0) {
      while (listMode1.size() > 0) {
        //for (int i = 0; i < listMode1.size(); i++) {
        jList1.setSelectedIndex(0);
        listMode2.addElement(jList1.getSelectedValue().toString().trim());
        listMode1.remove(jList1.getSelectedIndex());
      }
      //jList sort
      listModelSort(listMode2);
    }
  }

  void jButton4_actionPerformed(ActionEvent e) {
    if (listMode2.size() > 0) {
      while (listMode2.size() > 0) {
        //for (int i = 0; i < listMode1.size(); i++) {
        jList2.setSelectedIndex(0);
        listMode1.addElement(jList2.getSelectedValue().toString().trim());
        listMode2.remove(jList2.getSelectedIndex());
      }
      //jList sort
      listModelSort(listMode1);
    }
  }

  public void listModelSort(DefaultListModel defaultListModel) {
    java.util.ArrayList list = new java.util.ArrayList(Arrays.asList(
        defaultListModel.toArray()));
    for (int i = 0; i < list.size(); i++) {
      if (list.get(i) == null) {
        list.set(i, "");
      }
    }
    Collections.sort(list);
    defaultListModel.removeAllElements();
    for (int i = 0; i < list.size(); i++) {
      defaultListModel.addElement(list.get(i).toString());
    }
  }

  public static void main(String[] args) {
    PMMShipingSpace test = new PMMShipingSpace();
    test.show();
  }
}
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.