JPanel中的JList没有显示在我的JFrame上

时间:2015-12-08 03:22:51

标签: java swing jlist classcastexception

我无法通过assetListing()方法在我的JFrame上显示我的JList。出于某种原因,它甚至不会出现在我的MainFrame JFrame上。编译器似乎看到了它,因为我在运行时遇到了ClassCastException错误。如果有人能帮我解决这个问题,它会给我带来很多麻烦,现在已经有一段时间了......

package wealthmanager;


import InheritanceDesign.Asset;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
;

public class WealthManagerForm extends JFrame implements ActionListener
{
    private JFrame MainFrame = new JFrame("Wealth Manager");
    private JFrame assetEntry1 = new JFrame("Bank Account");
    private JFrame assetEntry2 = new JFrame("Stocks");
    private JFrame assetEntry3 = new JFrame("Car");
    private JFrame assetEntry4 = new JFrame("House");
    private JMenuBar assetMenu = new JMenuBar();
    private JMenu selectAsset = new JMenu("New");
    private JMenuItem bank = new JMenuItem("Bank Account");
    private JMenuItem stock = new JMenuItem("Stocks");
    private JMenuItem car = new JMenuItem("Car");
    private JMenuItem house = new JMenuItem("House");
    private JList assetListDisplay;
    private JPanel assetData;
    private JScrollPane assetScroll;

    private Vector assetList = new Vector();



    public WealthManagerForm()
    {
        gui();
    }

    public void gui()
    {
        MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MainFrame.setSize(1200, 800);
        MainFrame.setVisible(true);


        JLabel heading = new JLabel("Assets");

        assetEntry1.setSize(450, 200);
        assetEntry2.setSize(450, 250);
        assetEntry3.setSize(450, 275);
        assetEntry4.setSize(450, 270);        


        assetMenu.add(selectAsset);
        selectAsset.setFont(new Font("SansSerif", Font.PLAIN, 17));
        MainFrame.setJMenuBar(assetMenu);

        newMenu();
        assetListing();
    }

    public void assetListing()
    {
    JPanel listPanel = new JPanel(new BorderLayout());
    listPanel.setPreferredSize(new Dimension(400, 150));
    MainFrame.setLayout(new BorderLayout());
    MainFrame.add(listPanel);
    setLayout(new GridLayout());
    assetListDisplay = new JList (assetList);
    //assetListDisplay.addListSelectionListener((ListSelectionListener) this);

    listPanel.add(assetListDisplay);

     listPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()));
     pack();


    }
     public void valueChanged(ListSelectionEvent event)
    {
       if(event.getSource() == assetListDisplay && !event.getValueIsAdjusting())
       {
         String stringValue = (String)assetListDisplay.getSelectedValue();
         //if(stringValue != null)
             //
            // assetData.setText(stringValue);
       }
    }

    public void newMenu()
    {
        bankMenu();
        stockMenu();
        carMenu();
        houseMenu();

        MainFrame.setJMenuBar(assetMenu);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
    }

    public void bankMenu()
    {
        selectAsset.add(bank);
        bank.setFont(new Font("SansSerif", Font.PLAIN, 20));
        selectAsset.addSeparator();
        bank.addActionListener(new ActionListener() 
        {

            @Override
            public void actionPerformed(ActionEvent ae) {
                assetEntry1.setVisible(true);

            }
        });

                JPanel panel2 = new JPanel();
                panel2.setPreferredSize(new Dimension(400, 120));
                assetEntry1.setLayout(new GridBagLayout());
                assetEntry1.add(panel2);
                setLayout(new GridLayout()); 
                GridBagConstraints constraints = new GridBagConstraints();
                constraints.anchor = GridBagConstraints.WEST;
                constraints.insets = new Insets(100, 100, 100, 100);
                constraints.gridx = 0;
                constraints.gridy = 0;
                JLabel bankLabel1 = new JLabel("Name    ");
                JLabel bankLabel2 = new JLabel("Balance");
                JTextArea bankText1 = new JTextArea(2, 30);
                JTextArea bankText2 = new JTextArea(2, 30);
                JButton bankButton1 = new JButton("Cancel");
                JButton bankButton2 = new JButton("Save");
                panel2.add(bankLabel1, constraints);
                constraints.gridx = 1;
                panel2.add(bankText1, constraints);
                constraints.gridx = 0;
                constraints.gridy = 1;
                panel2.add(bankLabel2, constraints);
                constraints.gridx = 1;
                panel2.add(bankText2, constraints);
                constraints.anchor = GridBagConstraints.CENTER;
                panel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()));
                pack();
                setLocationRelativeTo(null);         
                panel2.add(bankButton1, "South");
                panel2.add(bankButton2, "South");
    }

    public void stockMenu()
    {
        selectAsset.add(stock);
        stock.setFont(new Font("SansSerif", Font.PLAIN, 20));
        selectAsset.addSeparator();
        stock.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                assetEntry2.setVisible(true);
                //setVisible(true);
            }
        });
                JPanel panel = new JPanel();
                panel.setPreferredSize(new Dimension(400, 150));
                assetEntry2.setLayout(new GridBagLayout());
                assetEntry2.add(panel);
                setLayout(new GridLayout()); 
                GridBagConstraints constraints = new GridBagConstraints();
                constraints.anchor = GridBagConstraints.WEST;
                constraints.insets = new Insets(100, 100, 100, 100);
                constraints.gridx = 0;
                constraints.gridy = 0;
                JLabel stockLabel1 = new JLabel("Ticker   ");
                JLabel stockLabel2 = new JLabel("Quantity");
                JLabel stockLabel3 = new JLabel("Price      ");
                JTextArea stockText1 = new JTextArea(2, 30);
                JTextArea stockText2 = new JTextArea(2, 30);
                JTextArea stockText3 = new JTextArea(2, 30);
                JButton stockButton1 = new JButton("Cancel");
                JButton stockButton2 = new JButton("Save");
                panel.add(stockLabel1, constraints);
                constraints.gridx = 1;
                panel.add(stockText1, constraints);
                constraints.gridx = 0;
                constraints.gridy = 1;
                panel.add(stockLabel2, constraints);
                constraints.gridx = 1;
                panel.add(stockText2, constraints);
                constraints.gridx = 0;
                constraints.gridy = 2;
                panel.add(stockLabel3, constraints);
                constraints.gridx = 1;
                panel.add(stockText3, constraints);
                constraints.anchor = GridBagConstraints.CENTER;
                panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()));
                pack();
                setLocationRelativeTo(null);         
                panel.add(stockButton1, "South");
                panel.add(stockButton2, "South");


    }

    public void carMenu()
    {
            selectAsset.add(car);
        car.setFont(new Font("SansSerif", Font.PLAIN, 20));
        car.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                assetEntry3.setVisible(true);
            }
        });

                JPanel panel3 = new JPanel();
                panel3.setPreferredSize(new Dimension(400, 200));
                assetEntry3.setLayout(new GridBagLayout());
                assetEntry3.add(panel3);
                setLayout(new GridLayout()); 
                GridBagConstraints constraints = new GridBagConstraints();
                constraints.anchor = GridBagConstraints.WEST;
                constraints.insets = new Insets(100, 100, 100, 100);
                constraints.gridx = 0;
                constraints.gridy = 0;
                JLabel carLabel1 = new JLabel("Type   ");
                JLabel carLabel2 = new JLabel("Name");
                JLabel carLabel3 = new JLabel("Value");
                JLabel carLabel4 = new JLabel("Debt  ");
                JTextArea carText1 = new JTextArea(2, 30);
                JTextArea carText2 = new JTextArea(2, 30);
                JTextArea carText3 = new JTextArea(2, 30);
                JTextArea carText4 = new JTextArea(2, 30);
                JButton carButton1 = new JButton("Cancel");
                JButton carButton2 = new JButton("Save");
                panel3.add(carLabel1, constraints);
                constraints.gridx = 1;
                panel3.add(carText1, constraints);
                constraints.gridx = 0;
                constraints.gridy = 1;
                panel3.add(carLabel2, constraints);
                constraints.gridx = 1;
                panel3.add(carText2, constraints);
                constraints.gridx = 0;
                constraints.gridy = 2;
                panel3.add(carLabel3, constraints);
                constraints.gridx = 1;
                panel3.add(carText3, constraints);
                constraints.gridx = 0;
                constraints.gridy = 3;
                panel3.add(carLabel4, constraints);
                constraints.gridx = 1;
                panel3.add(carText4, constraints);

                constraints.anchor = GridBagConstraints.CENTER;
                panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()));
                pack();
                setLocationRelativeTo(null);         
                panel3.add(carButton1, "South");
                panel3.add(carButton2, "South");

    }

    public void houseMenu()
    {
          selectAsset.add(house);
        house.setFont(new Font("SansSerif", Font.PLAIN, 20));
        selectAsset.addSeparator();
        house.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                assetEntry4.setVisible(true);
            }
        });   

                JPanel panel4 = new JPanel();
                panel4.setPreferredSize(new Dimension(400, 200));
                assetEntry4.setLayout(new GridBagLayout());
                assetEntry4.add(panel4);
                setLayout(new GridLayout()); 
                GridBagConstraints constraints = new GridBagConstraints();
                constraints.anchor = GridBagConstraints.WEST;
                constraints.insets = new Insets(100, 100, 100, 100);
                constraints.gridx = 0;
                constraints.gridy = 0;
                JLabel houseLabel1 = new JLabel("Type   ");
                JLabel houseLabel2 = new JLabel("Name");
                JLabel houseLabel3 = new JLabel("Value");
                JLabel houseLabel4 = new JLabel("Debt  ");
                JTextArea houseText1 = new JTextArea(2, 30);
                JTextArea houseText2 = new JTextArea(2, 30);
                JTextArea houseText3 = new JTextArea(2, 30);
                JTextArea houseText4 = new JTextArea(2, 30);
                JButton houseButton1 = new JButton("Cancel");
                JButton houseButton2 = new JButton("Save");
                panel4.add(houseLabel1, constraints);
                constraints.gridx = 1;
                panel4.add(houseText1, constraints);
                constraints.gridx = 0;
                constraints.gridy = 1;
                panel4.add(houseLabel2, constraints);
                constraints.gridx = 1;
                panel4.add(houseText2, constraints);
                constraints.gridx = 0;
                constraints.gridy = 2;
                panel4.add(houseLabel3, constraints);
                constraints.gridx = 1;
                panel4.add(houseText3, constraints);
                constraints.gridx = 0;
                constraints.gridy = 3;
                panel4.add(houseLabel4, constraints);
                constraints.gridx = 1;
                panel4.add(houseText4, constraints);

                constraints.anchor = GridBagConstraints.CENTER;
                panel4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()));
                pack();
                setLocationRelativeTo(null);         
                panel4.add(houseButton1, "South");
                panel4.add(houseButton2, "South");

    }
}

0 个答案:

没有答案