如何使用JList

时间:2017-05-01 06:42:53

标签: java swing jlist

您好我正在尝试在Jlist上显示对象的ArrayList。在显示ArrayList时,我试图通过GUI向ArrayList添加或删除对象。 1)如何在Jlist上显示ArrayList对象? 2)如何通过在JList上选择对象来删除对象?

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.JList;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;

public class DriverFrame extends JFrame implements ItemListener{

private JPanel contentPane;
private JList driverlist;
private ArrayList drivers= new ArrayList<Driver>();
JTextField textFieldFirstName;
JTextField textFieldLastName;
JTextField textFieldTruckNumber;
JTextField textFieldTrailerNumber;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                DriverFrame frame = new DriverFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public DriverFrame() {

        Driver guy1= new Driver("Jeff","Blank","4125","1234",1400);
        Driver guy2= new Driver("Marcus","Geralds","0093","1203",1800);
        Driver guy3= new Driver("Steve","Wemmings","2010","2046",2100);
        Driver guy4= new Driver("Kyle","Patricks","8427","5625",900);
        Driver guy5= new Driver("Ficel","Metter","9124","4536",5500);


    setBackground(Color.WHITE);     
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 500, 500);
    setResizable(false);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBackground(Color.WHITE);
    tabbedPane.setBounds(5, 5, 495, 480);
    contentPane.add(tabbedPane);

    //Drivers TabbedPan ********
    JPanel Drivers = new JPanel();
    Drivers.setBackground(Color.WHITE);
    tabbedPane.addTab("Drivers", null, Drivers, null);
    Drivers.setLayout(null);

    //List to display drivers
    JList<Object> driverlist = new JList<>(drivers.toArray(new String[0]));
    driverlist.setBackground(Color.WHITE);
    driverlist.setVisibleRowCount(8);
    driverlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    Drivers.add(driverlist);
    driverlist.setBounds(221,6,247,234);


    JTextPane txtpnFirstName = new JTextPane();
    txtpnFirstName.setText("First name");
    txtpnFirstName.setBounds(5, 20, 140, 15);
    Drivers.add(txtpnFirstName);
    txtpnFirstName.setEditable(false);

    textFieldFirstName = new JTextField();
    textFieldFirstName.setBounds(5, 40, 140, 30);
    Drivers.add(textFieldFirstName);
    textFieldFirstName.setColumns(10);

    JTextPane txtpnLastName = new JTextPane();
    txtpnLastName.setText("Last Name");
    txtpnLastName.setBounds(5, 75, 140, 15);
    Drivers.add(txtpnLastName);
    txtpnLastName.setEditable(false);

    textFieldLastName = new JTextField();
    textFieldLastName.setBounds(5, 95, 140, 30);
    Drivers.add(textFieldLastName);
    textFieldLastName.setColumns(10);

    JTextPane txtpnTruckNumber = new JTextPane();
    txtpnTruckNumber.setText("Truck Number");
    txtpnTruckNumber.setBounds(5, 130, 140, 15);
    Drivers.add(txtpnTruckNumber);
    txtpnTruckNumber.setEditable(false);

    textFieldTruckNumber = new JTextField();
    textFieldTruckNumber.setBounds(5, 150, 140, 30);
    Drivers.add(textFieldTruckNumber);
    textFieldTruckNumber.setColumns(10);

    JTextPane txtpnTrailerNumber = new JTextPane();
    txtpnTrailerNumber.setText("Trailer Number");
    txtpnTrailerNumber.setBounds(5, 185, 140, 15);
    Drivers.add(txtpnTrailerNumber);
    txtpnTrailerNumber.setEditable(false);

    textFieldTrailerNumber = new JTextField();
    textFieldTrailerNumber.setBounds(5, 205, 140, 30);
    Drivers.add(textFieldTrailerNumber);
    textFieldTrailerNumber.setColumns(10);

    JButton btnAddDriver = new JButton("Add Driver");
    btnAddDriver.setBounds(5, 285, 117, 29);
    Drivers.add(btnAddDriver);

    JButton btnNewButton = new JButton("Delete Driver");
    btnNewButton.setBounds(302, 285, 117, 29);
    Drivers.add(btnNewButton);
    btnAddDriver.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Driver blah = new Driver();

            blah.setFirst(textFieldFirstName.getText());
            blah.setLast(textFieldLastName.getText());
            blah.setTnum(textFieldTruckNumber.getText());
            blah.setTrailer(textFieldTrailerNumber.getText());
            drivers.add(blah);
            System.out.println(blah.printDriver());
            System.out.println(drivers.size());

            //drivers.remove(example);
        }
    });

    //End of DriversPane******

    //New Expenses Tab*****
    JPanel NewExpense = new JPanel();
    tabbedPane.addTab("New Expense", null, NewExpense, null);
    JButton buttontest = new JButton();
    NewExpense.add(buttontest);
    //End of New Expense Tab****

    //Income Tab****
    JPanel Income = new JPanel();
    Income.setBackground(Color.WHITE);
    tabbedPane.addTab("Income", null, Income, null);
    Income.setLayout(null);

    JComboBox comboBox_3 = new JComboBox();
    comboBox_3.setBounds(6, 70, 138, 27);
    Income.add(comboBox_3);

    JTextPane txtpnDriver_3 = new JTextPane();
    txtpnDriver_3.setText("Driver:");
    txtpnDriver_3.setBounds(6, 42, 42, 16);
    Income.add(txtpnDriver_3);

    JTextPane txtpnLoadLocationstate = new JTextPane();
    txtpnLoadLocationstate.setText("Load Location(State): ");
    txtpnLoadLocationstate.setBounds(6, 161, 138, 16);
    Income.add(txtpnLoadLocationstate);

    textField_6 = new JTextField();
    textField_6.setBounds(6, 205, 130, 26);
    Income.add(textField_6);
    textField_6.setColumns(10);

    JTextPane txtpnLoadAmount = new JTextPane();
    txtpnLoadAmount.setText("Load Amount $$:");
    txtpnLoadAmount.setBounds(6, 299, 108, 16);
    Income.add(txtpnLoadAmount);

    textField_7 = new JTextField();
    textField_7.setBounds(6, 339, 130, 26);
    Income.add(textField_7);
    textField_7.setColumns(10);
    //End of Income Tab****

    //Results Tab***
    JPanel Results = new JPanel();
    Results.setBackground(Color.WHITE);
    tabbedPane.addTab("Result", null, Results, null);
    Results.setLayout(null);

    JComboBox comboBox_4 = new JComboBox();
    comboBox_4.setBounds(6, 69, 140, 27);
    Results.add(comboBox_4);

    JTextPane txtpnDriver_4 = new JTextPane();
    txtpnDriver_4.setText("Driver:");
    txtpnDriver_4.setBounds(6, 42, 140, 16);
    Results.add(txtpnDriver_4);

    JTextArea txtrHowMuchTo = new JTextArea();
    txtrHowMuchTo.setText("How much to pay driver. How much are the expenses of the driver");
    txtrHowMuchTo.setBounds(6, 124, 234, 304);
    Results.add(txtrHowMuchTo);

    JTextPane txtpnDriverInformation = new JTextPane();
    txtpnDriverInformation.setText("Driver Information");
    txtpnDriverInformation.setBounds(16, 108, 140, 16);
    Results.add(txtpnDriverInformation);

    JTextPane txtpnCompanyInfo = new JTextPane();
    txtpnCompanyInfo.setText("Company Info:");
    txtpnCompanyInfo.setBounds(414, 80, 178, 16);
    Results.add(txtpnCompanyInfo);

    JTextArea txtrIncome = new JTextArea();
    txtrIncome.setText("Income Expenses");
    txtrIncome.setBounds(390, 124, 294, 304);
    Results.add(txtrIncome);
    //End of Results Tab****

}

@Override
public void itemStateChanged(ItemEvent e) {
    // TODO Auto-generated method stub

}
}

0 个答案:

没有答案