Jlist返回值为null

问题描述 投票:0回答:0

I am supposed to make a JList that shows the list of the users who ordered shirts but it always show null, I'm still a student and really in need of help.

jlist 应该显示用户输入的名字、中间名和姓氏,我尝试为列表单独创建另一个 Jframe 但它变得更加混乱所以我停止了。

package mycode.commons;

import java.awt.EventQueue;
import java.util.ArrayList;

import mycode.views.Order;


public class Mymain {
    
    private static ArrayList<user> List;

    public static void main(String[] args) {
        
        List = new ArrayList<user>();
        
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Order frame = new Order(List);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

}

package mycode.views;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import mycode.commons.user;

import java.awt.Color;
import java.awt.Panel;
import java.awt.Label;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.awt.event.ItemEvent;
import javax.swing.JButton;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JList;
import java.awt.Toolkit;
import javax.swing.JLabel;

public class Order extends JFrame {

    private JPanel contentPane;
    private JTextField fname;
    private JTextField mname;
    private JTextField lname;
    private JTextField address;
    private JComboBox price;
    private JComboBox size;
    private final ButtonGroup btngrp_color = new ButtonGroup();
    private JButton btnorder;
    private JRadioButton rdbtnblack;
    private JRadioButton rdbtnblue;
    private JRadioButton rdbtngreen;
    String color = null;
    private JTextArea tapreview;
    private JButton viewusers;
    private JList<String> list;
    private ArrayList<user> List;
    DefaultListModel<String> dlm = new DefaultListModel<String>();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        
    }

    /**
     * Create the frame.
     */
    public Order(ArrayList<user> a) {
        this.List = a;
        setIconImage(Toolkit.getDefaultToolkit().getImage(Order.class.getResource("/mycode/resources/black.png")));
        
        initComponents();
        createEvents();
    }
        private void createEvents() {
              
            viewusers.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    
                    dlm.clear(); for(user p : List ){ dlm.addElement(p.toString()); }
                      list.setModel(dlm);
                    
                    List.add(new user(fname.getText(),mname.getText(),
                            lname.getText()));
                      dlm.clear(); for(user p : List ){ dlm.addElement(p.toString()); }
                      list.setModel(dlm);
                     
                }
            });
            
            size.addItemListener(new ItemListener() {
                    String[] XXL = {"", "1500 pesos"}; 
                    String[] XL = {"", "1200 pesos"}; 
                    String[] Large = {"", "900 pesos"};
                    String[] Medium = {"", "500 pesos"};
                    String[] Small = {"", "450 pesos"};
                    String[] Xtra_small = {"", "350 pesos"};
                    public void itemStateChanged(ItemEvent e) {
                    switch(size.getSelectedIndex()) {
                    case 0:
                        price.setEnabled(false);
                    break;
                    case 1:
                        price.setEnabled(true);
                        price.setModel(new DefaultComboBoxModel(XXL));
                    break;
                    case 2:
                        price.setEnabled(true);
                        price.setModel(new DefaultComboBoxModel(XL));
                        break;
                    case 3:
                        price.setEnabled(true);
                        price.setModel(new DefaultComboBoxModel(Large));
                        break;
                    case 4:
                        price.setEnabled(true);
                        price.setModel(new DefaultComboBoxModel(Medium));
                        break;
                    case 5:
                        price.setEnabled(true);
                        price.setModel(new DefaultComboBoxModel(Small));
                        break;
                    case 6:
                        price.setEnabled(true);
                        price.setModel(new DefaultComboBoxModel(Xtra_small));
                        break;
                    }
                }
            });
            
            btnorder.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    
                    JOptionPane.showMessageDialog(null,
                            "Your order is on the way ",
                            "Please wait patiently...",
                          JOptionPane.PLAIN_MESSAGE);
                    
                    if(rdbtnblack.isSelected()) {
                        color = "Black";
                    }else if(rdbtnblue.isSelected()){
                        color = "Blue";
                    }else if(rdbtngreen.isSelected()){
                        color = "Green";
                    }
                    tapreview.setText("Your Name is " + fname.getText() + " " + mname.getText() + " " + lname.getText() + 
                            "\nYour address is " + address.getText() +
                            "\nYour order is " + size.getSelectedItem() + " " + color + " shirt " + 
                            "\nfor " + price.getSelectedItem());
                }
            });
            
            price.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    rdbtnblack.setEnabled(true);
                    rdbtnblue.setEnabled(true);
                    rdbtngreen.setEnabled(true);
                }
            });
            
            rdbtnblack.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    btnorder.setEnabled(true);
                }
            });
            
            rdbtnblue.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    btnorder.setEnabled(true);
                }
            });
            
            rdbtngreen.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    btnorder.setEnabled(true);
                }
            });
            
    }

        private void initComponents() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 1340, 510);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

            setContentPane(contentPane);
            contentPane.setLayout(null);
            
            JPanel panel = new JPanel();
            panel.setBackground(new Color(64, 128, 128));
            panel.setBounds(0, 0, 1326, 473);
            contentPane.add(panel);
            panel.setLayout(null);
            
            Panel panel_1 = new Panel();
            panel_1.setLayout(null);
            panel_1.setBackground(new Color(13, 217, 171));
            panel_1.setBounds(10, 10, 976, 104);
            panel.add(panel_1);
            
            Label Firstname = new Label("Firstname");
            Firstname.setFont(new Font("Arial Narrow", Font.BOLD | Font.ITALIC, 15));
            Firstname.setBounds(127, 10, 120, 21);
            panel_1.add(Firstname);
            
            Label Middlename = new Label("Middlename");
            Middlename.setFont(new Font("Arial Narrow", Font.BOLD | Font.ITALIC, 15));
            Middlename.setBounds(433, 10, 136, 21);
            panel_1.add(Middlename);
            
            Label Lastname = new Label("Lastname");
            Lastname.setFont(new Font("Arial Narrow", Font.BOLD | Font.ITALIC, 15));
            Lastname.setBounds(765, 10, 109, 21);
            panel_1.add(Lastname);
            
            fname = new JTextField();
            fname.setColumns(10);
            fname.setBounds(26, 45, 290, 37);
            panel_1.add(fname);
            
            mname = new JTextField();
            mname.setColumns(10);
            mname.setBounds(342, 45, 290, 37);
            panel_1.add(mname);
            
            lname = new JTextField();
            lname.setColumns(10);
            lname.setBounds(658, 45, 290, 37);
            panel_1.add(lname);
            
            Panel panel_2 = new Panel();
            panel_2.setLayout(null);
            panel_2.setBackground(new Color(13, 217, 171));
            panel_2.setBounds(10, 134, 976, 93);
            panel.add(panel_2);
            
            Label Address = new Label("Address");
            Address.setFont(new Font("Arial Narrow", Font.BOLD | Font.ITALIC, 15));
            Address.setBounds(444, 10, 102, 21);
            panel_2.add(Address);
            
            address = new JTextField();
            address.setColumns(10);
            address.setBounds(10, 37, 956, 37);
            panel_2.add(address);
            
            Panel panel_3 = new Panel();
            panel_3.setLayout(null);
            panel_3.setBackground(new Color(13, 217, 171));
            panel_3.setBounds(10, 254, 203, 80);
            panel.add(panel_3);
            
            size = new JComboBox();
            size.setModel(new DefaultComboBoxModel(new String[] {"", "XXL", "XL", "Large", "Medium", "Small", "Xtra small"})); //size choices
            size.setBounds(27, 37, 148, 30);
            panel_3.add(size);
            
            Label Size = new Label("Size");
            Size.setFont(new Font("Arial Narrow", Font.BOLD | Font.ITALIC, 15));
            Size.setBounds(82, 10, 59, 21);
            panel_3.add(Size);
            
            Panel panel_4 = new Panel();
            panel_4.setLayout(null);
            panel_4.setBackground(new Color(13, 217, 171));
            panel_4.setBounds(283, 254, 203, 80);
            panel.add(panel_4);
            
            Label Price = new Label("Price");
            Price.setFont(new Font("Arial Narrow", Font.BOLD | Font.ITALIC, 15));
            Price.setBounds(77, 10, 75, 21);
            panel_4.add(Price);
            
            price = new JComboBox();
            price.setEnabled(false);
            price.setBounds(23, 37, 154, 30);
            panel_4.add(price);
            
            Panel panel_5 = new Panel();
            panel_5.setLayout(null);
            panel_5.setBackground(new Color(13, 217, 171));
            panel_5.setBounds(557, 254, 429, 80);
            panel.add(panel_5);
            
            Label color = new Label("Color");
            color.setFont(new Font("Arial Narrow", Font.BOLD | Font.ITALIC, 15));
            color.setBounds(184, 10, 82, 21);
            panel_5.add(color);
            
            rdbtnblack = new JRadioButton("Black");
            rdbtnblack.setEnabled(false);
            btngrp_color.add(rdbtnblack);
            rdbtnblack.setFont(new Font("Segoe Script", Font.BOLD, 12));
            rdbtnblack.setBounds(30, 37, 103, 30);
            panel_5.add(rdbtnblack);
            
            rdbtnblue = new JRadioButton("Blue");
            rdbtnblue.setEnabled(false);
            btngrp_color.add(rdbtnblue);
            rdbtnblue.setFont(new Font("Segoe Script", Font.BOLD, 12));
            rdbtnblue.setBounds(163, 37, 103, 30);
            panel_5.add(rdbtnblue);
            
            rdbtngreen = new JRadioButton("Green");
            rdbtngreen.setEnabled(false);
            btngrp_color.add(rdbtngreen);
            rdbtngreen.setFont(new Font("Segoe Script", Font.BOLD, 12));
            rdbtngreen.setBounds(296, 37, 103, 30);
            panel_5.add(rdbtngreen);
            
            btnorder = new JButton("Order");
            btnorder.setEnabled(false);
            btnorder.setFont(new Font("Segoe Script", Font.BOLD, 12));
            btnorder.setBounds(424, 378, 132, 64);
            panel.add(btnorder);
            
            tapreview = new JTextArea();
            tapreview.setBackground(Color.WHITE);
            tapreview.setBounds(1005, 41, 302, 144);
            panel.add(tapreview);
            
            list = new JList();
            list.setBounds(1005, 226, 311, 144);
            panel.add(list);
            
            viewusers = new JButton("View Users");
            viewusers.setFont(new Font("Segoe Script", Font.BOLD, 12));
            viewusers.setBounds(1099, 411, 123, 21);
            panel.add(viewusers);
            
            JLabel preview = new JLabel("Preview");
            preview.setForeground(new Color(255, 255, 255));
            preview.setBackground(new Color(255, 255, 255));
            preview.setFont(new Font("Segoe Script", Font.BOLD, 15));
            preview.setBounds(1124, 10, 80, 21);
            panel.add(preview);
        
    }       
}

java arraylist jlist
© www.soinside.com 2019 - 2024. All rights reserved.