簡體   English   中英

GUI Jlist ItemListener

[英]GUI Jlist ItemListener

我試圖用某些元素創建一個JList,當用戶選擇一個元素時,另一個JList將出現在窗口中。 然后,如果用戶選擇另一個列表的元素,則文本區域將出現在窗口中。 到目前為止,這是我所做的:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class ProductsList extends JFrame implements ItemListener {


private JLabel availableDev;
private JComboBox avDevBox
private JTextArea itemDetails;
private JList Items;
private JList devicesForSale;
private JList imandSJList;
private JList applJList;
private JList gamJList;

public ProductsList() {

    String[] avDevicesListItems = {"Image and Sound", "Appliance", "Gaming"};
    ArrayList<imageAndSound> iasList = new ArrayList<imageAndSound>;
    ArrayList<Appliance> applianceList = new ArrayList<Appliance>;
    ArrayList<Gaming> gamingList = new ArrayList<Gaming>;

    //construct components
    availableDev = new JLabel ("Available Devices");
    avDevicesList = new JList (avDevicesListItems);
    itemDetails = new JTextArea (5, 5);
    avDevBox = new JComboBox (avDevicesListItems);
    devicesForSale = new JList(devList);
    imandSJList = new JList(iasList);
    applJList = new JList(applianceList);
    gamJList = new JList(gamingList);

    avDevBox.addItemListener(this);

    //adjust size and set layout
    setPreferredSize (new Dimension (944, 574));
    setLayout (null);

    //add components
    add (availableDev);
    add (avDevicesList);
    add (itemDetails);
    add (avDevBox);
    add(devicesForSale);

    //set component bounds
    availableDev.setBounds (35, 0, 100, 25);
    avDevBOx.setBounds (25, 30, 120, 25);
    itemDetails.setBounds (245, 225, 265, 215);

    public void itemStateChanged(ItemEvent event) {
        int choice = avDevBox.getSelectedIndex();
        if (choice = 0) {
            add(imandSJList);
            imandSJList.addItemListener(this);
        }
        else if (choice = 1){
            add(applJList);
            applJList.addItemListener(this);
        }
        else {
            add(gamJList);
            gamJList.addItemListener(this);
        }
    }   


public static void main (String[] args) {
    JFrame frame = new JFrame ("Products List");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add (new MyPanel());
    frame.pack();
    frame.setVisible (true);
    TV tv1 = new TV("LCD","28","720p","HDMI/DVI","11235","AT142","2015","SONY",500,5); //Creates object of class TV
    TV tv2 = new TV("LED","32","1080p","HDMI/DVI","15394","AT168","2016","SAMSUNG",1000,0); //Creates object of class TV
    bluerayDVD dvd = new bluerayDVD("DVD","720p","DVD-RW","15642","TT172","2015","SONY",400,100); //Creates object of class bluerayDVD
    bluerayDVD blueray = new bluerayDVD("blueray","1080p","BD-R","18412","TT100","2015","SONY",500,1000); //Creates object of class bluerayDVD
    Camera cam1 = new Camera("DSLR","50","stable","x5","2","19785","TC137","2016","SONY",600,50); //Creates object of class Camera
    Camera cam2 = new Camera("compact,","40","stable","x7","1","16783","TC108","2016","SONY",700,70); //Creates object of class Camera
    Console c1 = new Console("PS4","RGEN","1080p","Dolby","1 TB","15641","TG142","2016","SONY",400,80); //Creates object of class Console
    Console c2 = new Console("XBOX","RGEN2","1080p","Dolby Digital","2 TB","13424","TG123","2016","MICROSOFT",400,10); //Creates object of class Console
    Refrigerator f1 = new Refrigerator("Single door","C++","5kg","2kg","28756","TF357","2016","BOSS",1500,10); //Creates object of class Refrigerator
    Refrigerator f2 = new Refrigerator("Double door","C++","8kg","4kg","26756","TF382","2016","SIEMENS",500,5); //Creates object of class Refrigerator
    WashMachines wM1 = new WashMachines("C++","2kg","200rs","49356","TW364","2016","SIEMENS",3000,10); //Creates object of class WashMachines
    WashMachines wM2 = new WashMachines("C++","4kg","250rs","49579","TW376","2016","BOSS",5000,10); //Creates object of class WashMachines
    imandSJList.add(tv1);
    imandSJList.add(tv2);
    imandSJList.add(dvd);
    imandSJList.add(blueray);
    imandSJList.add(cam1);
    imandSJList.add(cam2);
    gamJList.add(c1);
    gamJList.add(c2);
    applJList.add(f1);
    applJList.add(f2);
    applJList.add(wM1);
    applJList.add(wM2);
   }
}

因此,如果有人可以提出更好的方法,我將非常高興
謝謝

同樣,您可以繼續將偵聽器直接添加到每個列表

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JListDemo extends JFrame {

    public JListDemo() {

        setSize(new Dimension(300, 300));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        final JLabel label = new JLabel("Update");
        String[] data = { "one", "two", "three", "four" };
        final JList dataList = new JList(data);

        dataList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent arg0) {
                if (!arg0.getValueIsAdjusting()) {
                  label.setText(dataList.getSelectedValue().toString());
                }
            }
        });
        add(dataList);
        add(label);

        setVisible(true);

    }

    public static void main(String args[]) {
        new JListDemo();
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM