gpt4 book ai didi

java - 在 Applet 中显示 JList

转载 作者:行者123 更新时间:2023-11-29 06:20:04 25 4
gpt4 key购买 nike

好吧,所以每当我用我的代码打开一个新的 appelt 窗口时...JList 不会显示,直到我单击它...我如何才能让它在我启动 applet 时显示所有值?

谢谢。

import java.awt.*;
import javax.swing.*;
import javax.swing.JList;
import java.awt.event.*;
import java.util.*;

public class inventory extends JApplet implements MouseListener {

public static String newline;
public static JList list;
int gold = 123;

public void init() {

ArrayList<String> arr = new ArrayList<String>();
arr.add("Hatchet");
arr.add("Sword");
arr.add("Shield");
arr.add(gold + " Gold");
System.out.println("You have " + arr.size() + " items in your inventory.");
showInventory(arr);

list = new JList(arr.toArray());

add(list);

list.addMouseListener(this);

list.setVisible(true);
}

public static void showInventory(ArrayList<String> theList) {
for (int i = 0; i < theList.size(); i++) {
System.out.println(theList.get(i));
}
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
Object index = list.getSelectedValue();
System.out.println("You have selected: " + index);
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void paint(Graphics g) {
}
}

最佳答案

你想要

this.setVisible(true);

而不是

list.setVisible(true);

关于java - 在 Applet 中显示 JList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3489432/

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