JListTest.java
上传用户:xinkaifrp
上传日期:2021-05-08
资源大小:372k
文件大小:3k
源码类别:

家庭/个人应用

开发平台:

Java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. /*
  6.  * JListTest.java
  7.  *
  8.  * Created on 2009-6-26, 11:20:27
  9.  */
  10. package test;
  11. import javax.swing.DefaultListModel;
  12. import javax.swing.ListModel;
  13. /**
  14.  *
  15.  * @author Administrator
  16.  */
  17. public class JListTest extends javax.swing.JFrame {
  18.     /** Creates new form JListTest */
  19.     public JListTest() {
  20.         initComponents();
  21.     }
  22.     public void setValue(){
  23.      DefaultListModel model=new DefaultListModelImpl();
  24.      model.addElement("hello");
  25.      model.addElement("hi");
  26.      jList1.setModel(model);
  27.     }
  28.     @SuppressWarnings("unchecked")
  29.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  30.     private void initComponents() {
  31.         jScrollPane1 = new javax.swing.JScrollPane();
  32.         jList1 = new javax.swing.JList();
  33.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  34.         jList1.setModel(new javax.swing.AbstractListModel() {
  35.             String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
  36.             public int getSize() { return strings.length; }
  37.             public Object getElementAt(int i) { return strings[i]; }
  38.         });
  39.         jScrollPane1.setViewportView(jList1);
  40.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  41.         getContentPane().setLayout(layout);
  42.         layout.setHorizontalGroup(
  43.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  44.             .addGroup(layout.createSequentialGroup()
  45.                 .addContainerGap()
  46.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  47.                 .addContainerGap(45, Short.MAX_VALUE))
  48.         );
  49.         layout.setVerticalGroup(
  50.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  51.             .addGroup(layout.createSequentialGroup()
  52.                 .addContainerGap()
  53.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  54.                 .addContainerGap(45, Short.MAX_VALUE))
  55.         );
  56.         pack();
  57.     }// </editor-fold>//GEN-END:initComponents
  58.  public static void main(String args[]) {
  59.         java.awt.EventQueue.invokeLater(new Runnable() {
  60.             public void run() {
  61.                JListTest t=new JListTest();
  62.                 t.setValue();
  63.                 t.setVisible(true);
  64.             }
  65.         });
  66.     }
  67.     // Variables declaration - do not modify//GEN-BEGIN:variables
  68.     private javax.swing.JList jList1;
  69.     private javax.swing.JScrollPane jScrollPane1;
  70.     // End of variables declaration//GEN-END:variables
  71.     private static class DefaultListModelImpl extends DefaultListModel {
  72.         public DefaultListModelImpl() {
  73.         }
  74.     }
  75. }