在Applet中显示JList

时间:2010-08-15 21:53:52

标签: java jlist

好的,每当我用我的代码打开一个新的上传窗口时... JList没有显示UNTIL我点击它...我怎么做它所以当我启动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) {
    }
}

2 个答案:

答案 0 :(得分:2)

你想要

this.setVisible(true);

而不是

list.setVisible(true);

答案 1 :(得分:1)

尝试在list.setVisible(true);之前设置list.addMouseListener(this);。除此之外,没有什么东西可以跳出来。