java - GUI Jlist 项目监听器

标签 java swing user-interface jlist

我正在尝试创建一个包含一些元素的 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();
    }

}

关于java - GUI Jlist 项目监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37540238/

相关文章:

java - 将 Byte[] 读取为 unsigned short Java

java - 有没有办法打包jar依赖项以便在独立环境中运行java类

java - 如何用java检测系统托盘的位置?

javascript - 与 jQuery 一起使用的最佳 UI 库

java - 删除重复的项目java gui

java - 如何在 servlet 中识别调用来自 href 链接点击?

java - 使用 Apache POI 读取 Excel 文件中的数值时出现问题

java - JComboBox 具有 4 列和 4 行数据[][] 形式

java - 如何从 Jtable 中获取所有值并绘制它们的图表?

java - 如何从文档监听器更新 JComboBox 的列表?