求 JTABLE / JLIST 在applet中应用的例程

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
       
    public class JTableExample extends JFrame implements ActionListener
    {
        public static void main(String[] argv)
        {
            JTableExample mainApp = new JTableExample();
        }
        
        public JTableExample()
        {
            super("JTable Example");
            setBounds(0, 0, 450, 350);
            getContentPane().setLayout(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
                   
            table = new JTable(10, 3);
            table.setBounds(10, 10, 420, 200);   
            
            tableHandler = new TableHandler();
            table.setModel(tableHandler);
                   
            // Create three JTextField...
            textfield1 = new JTextField(15);
            textfield1.setLocation(10, 220);
            textfield1.setSize(textfield1.getPreferredSize());
            
            textfield2 = new JTextField(15);
            textfield2.setLocation(10, 250);
            textfield2.setSize(textfield2.getPreferredSize());
            
            textfield3 = new JTextField(15);
            textfield3.setLocation(10, 280);
            textfield3.setSize(textfield3.getPreferredSize());
            
            // Create the button...
            addButton = new JButton("Add Data to Table");
            addButton.setLocation(200, 220);
            addButton.setSize(addButton.getPreferredSize());
            
            // Add the action listeners...
            addButton.addActionListener(this);
                                       
            // Add the objects to the content pane...
            getContentPane().add(table);
            getContentPane().add(textfield1);
            getContentPane().add(textfield2);
            getContentPane().add(textfield3);
            getContentPane().add(addButton);
            
            setVisible(true);
        }
        
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == addButton)
            {
                // Check there is text in the 'textfield'
                if(textfield1.getText().compareTo("") != 0 &&
                   textfield2.getText().compareTo("") != 0 &&
                   textfield3.getText().compareTo("") != 0)
                {
                    // Then add the three fields to the JTable
                    tableHandler.addRowToTable(textfield1.getText(),
                                               textfield2.getText(),
                                               textfield3.getText());
                    
                    // Clear the textfields...
                    textfield1.setText("");
                    textfield2.setText("");
                    textfield3.setText("");
                }
            }
        }
        
        JTable table;
        TableHandler tableHandler;
        JTextField textfield1;
        JTextField textfield2;
        JTextField textfield3;
        JButton addButton;
    }
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
       
    public class JListExample extends JFrame implements ActionListener
    {
        public static void main(String[] argv)
        {
            JListExample mainApp = new JListExample();
        }
        
        public JListExample()
        {
            super("JList Example");
            setBounds(0, 0, 450, 350);
            getContentPane().setLayout(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
                   
            // Create the list object...
            list = new JList();
            list.setBounds(10, 10, 420, 200);
            listData = new Vector();
            
            // Create a JTextField...
            textfield = new JTextField(15);
            textfield.setLocation(10, 220);
            textfield.setSize(textfield.getPreferredSize());
            
            // Create the two buttons...
            addButton = new JButton("Add Text to List");
            addButton.setLocation(200, 220);
            addButton.setSize(addButton.getPreferredSize());
            
            removeButton = new JButton("Remove Selected from List");
            removeButton.setLocation(200, 250);
            removeButton.setSize(removeButton.getPreferredSize());
            
            // Add the action listeners...
            addButton.addActionListener(this);
            removeButton.addActionListener(this);
                                       
            // Add the objects to the content pane...
            getContentPane().add(list);
            getContentPane().add(textfield);
            getContentPane().add(addButton);
            getContentPane().add(removeButton);
            
            setVisible(true);
        }
        
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == addButton)
            {
                // Check there is text in the 'textfield'
                if(textfield.getText().compareTo("") != 0)
                {
                    // Then add it to the JList object
                    listData.add(textfield.getText());
                    list.setListData(listData);
                    
                    // Clear the textfield...
                    textfield.setText("");
                }
            }
            else if(e.getSource() == removeButton)
            {
                // Check there is a list item selected
                if(list.getSelectedValue() != null)
                {
                    // Then add it to the JList object
                    listData.remove(list.getSelectedValue());
                    list.setListData(listData);
                }
            }
        }
        
        JList list;
        Vector listData;
        JTextField textfield;
        JButton addButton;
        JButton removeButton;
    }
      

  3.   

    import javax.swing.table.*;
    import java.util.*;
       
    class TableHandler extends AbstractTableModel
    {
        public TableHandler()
        {
            dataRows = new Vector(); // set up the vector 
        }
        
        public void addRowToTable(String a, String b, String c)
        {
            String[] rowData = new String[3];
                               
            rowData[0] = a;
            rowData[1] = b;
            rowData[2] = c;
            
            dataRows.addElement(rowData);   // Add the data to a vector
                
            fireTableChanged(null); // Tell the table there is new data
        }
        
        public int getColumnCount()
        {
            return 3;
        }
        
        public int getRowCount()
        {
            if(dataRows != null)
            {
                return dataRows.size();
            }
            else
            {
                return -1;
            }
        }
       
        public Object getValueAt(int row, int column)
        {
            if(dataRows != null)
            {
                return ((String[])(dataRows.elementAt(row)))[column];
            }
            else
            {
                return null;
            }
        }
           
        private Vector dataRows;   // Vector to contain the rows of data
    }