gpt4 book ai didi

java - 如何将 MouseListener 添加到 JList 项目?

转载 作者:行者123 更新时间:2023-12-01 18:46:07 25 4
gpt4 key购买 nike

所以,我想要的是当选择 JList 项时,运行另一段代码。但我的问题是,如何为特定的 JList 项目实现 MouseListeners

这是我的代码:

public void launchFrame() {
String selection1 = "";
String[]mainContents = {"Vehicles","Bikes/Bicycles","Boats","Houses","Businesses","Objects","Jobs","Ranks","Licenses"};
String[]selectionVehicles = {};
String[]selectionBikesBicycles = {};
String[]selectionBoats = {};
String[]selectionHouses = {};
String[]selectionBusinesses = {};
String[]selectionObjects = {};
String[]selectionJobs = {};
String[]selectionRanks = {};
String[]selectionLicenses = {};
JFrame f = new JFrame("MTG SAMP Server Reference");
JList listAll = new JList(mainContents);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.pack();
f.setSize(1200, 800);
f.setLocationRelativeTo(null);
f.add(listAll);
listAll.setSize(250, 250);
listAll.setLocation(50, 50);
switch (selection1) {
case "Vehicles":
JList listVehicles = new JList(mainContents);
f.add(listVehicles);
listVehicles.setSize(250, 250);
listVehicles.setLocation(350, 50);
f.repaint();
f.revalidate();
break;
case "Bikes/Bicycles":
JList listBikesBicycles = new JList(mainContents);
f.add(listBikesBicycles);
listBikesBicycles.setSize(250, 250);
listBikesBicycles.setLocation(350, 50);
f.repaint();
f.revalidate();
break;
case "Boats":
JList listBoats = new JList(mainContents);
f.add(listBoats);
listBoats.setSize(250, 250);
listBoats.setLocation(350, 50);
f.repaint();
f.revalidate();
break;
case "Houses":
JList listHouses = new JList(mainContents);
f.add(listHouses);
listHouses.setSize(250, 250);
listHouses.setLocation(350, 50);
f.repaint();
f.revalidate();
break;
case "Businesses":
JList listBusinesses = new JList(mainContents);
f.add(listBusinesses);
listBusinesses.setSize(250, 250);
listBusinesses.setLocation(350, 50);
f.repaint();
f.revalidate();
break;
case "Objects":
JList listObjects = new JList(mainContents);
f.add(listObjects);
listObjects.setSize(250, 250);
listObjects.setLocation(350, 50);
f.repaint();
f.revalidate();
break;
case "Jobs":
JList listJobs = new JList(mainContents);
f.add(listJobs);
listJobs.setSize(250, 250);
listJobs.setLocation(350, 50);
f.repaint();
f.revalidate();
break;
case "Ranks":
JList listRanks = new JList(mainContents);
f.add(listRanks);
listRanks.setSize(250, 250);
listRanks.setLocation(350, 50);
f.repaint();
f.revalidate();
break;
case "Licenses":
JList listLicenses = new JList(mainContents);
f.add(listLicenses);
listLicenses.setSize(250, 250);
listLicenses.setLocation(350, 50);
f.repaint();
f.revalidate();
break;
}
Container contentPaneMain = f.getContentPane();
contentPaneMain.add(new MTGSAMPServerReference());
}

如何将 MouseListeners 添加到 JList 项目“车辆”、“自行车/自行车”、“船”、“房屋”、“企业” 、“对象”、“工作”、“等级”和“许可证”?

最佳答案

如果您希望在选择更改时发生某些事情,您不需要 MouseListener,而是 ListSelectionListener。它不仅是正确的抽象,而且请记住,无需使用鼠标即可更改选择。如今大多数计算机都有键盘。

Swing tutorial 中介绍了如何使用它。 .

在监听器内,获取选定的元素,并采取相应的操作。

关于java - 如何将 MouseListener 添加到 JList 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17772166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com