简体   繁体   English

JList不在JScrollPane中显示

[英]JList doesn't show inside JScrollPane

import java.awt.event.*;
import java.io.File;
import javax.swing.*;

public class Launch extends JFrame implements ActionListener {
private static final long serialVersionUID = 5291490384908841627L;
JButton OK, create;
JList<String> players;
File player;
public static void main(String[] args) {
    new Launch();
}
private Launch() {
    this.setSize(600, 600);
    this.setLocationRelativeTo(null);
    this.setTitle("A Word Game");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    Box box = Box.createVerticalBox();
    OK = new JButton("OK");
    create = new JButton("Create new player");
    OK.addActionListener(this);
    create.addActionListener(this);
    String[] playerList = getPlayers();
    players = new JList<String>(playerList);
    players.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane scroll = new JScrollPane(players, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scroll.add(players);
    final JLabel choosePrompt = new JLabel("Choose a player.");
    box.add(Box.createVerticalStrut(20));
    box.add(choosePrompt);
    box.add(Box.createVerticalStrut(20));
    box.add(scroll);
    box.add(Box.createVerticalStrut(20));
    box.add(OK);
    box.add(Box.createVerticalStrut(20));
    box.add(create);
    box.add(Box.createVerticalStrut(20));
    this.add(box);
    this.setVisible(true);
}
private String[] getPlayers() {
    File playerDirectory = new File("players");
    File[] playersInFiles = playerDirectory.listFiles();
    String[] players = new String[playersInFiles.length];
    for (int counter = 0; counter < playersInFiles.length; counter ++) {
        players[counter] = trimTXT(playersInFiles[counter].getName());
    }
    return players;
}
private String trimTXT(String original) {
    return original.substring(0, original.length() - 4);
}
@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(OK)) {
        String name = players.getSelectedValue();
        if (name == null) {
            return;
        }
        player = new File(name + ".txt");
    } else if (e.getSource().equals(create)) {
        //create a new character, all that what-not
    }
}
}

The problem is that the players don't show up in the JScrollPane. 问题是播放器没有出现在JScrollPane中。 The scroll pane is just there without anything inside it. 滚动窗格就在其中,里面没有任何东西。 I have a bunch of blank .txt files representing players in the players folder. 我在玩家文件夹中有一堆代表玩家的空白.txt文件。 They are just for testing. 它们仅用于测试。 The weid thing is that when the JList is not in the JScrollPane, it works fine. 奇怪的是,当JList不在JScrollPane中时,它可以正常工作。 When I add it to a JScrollPane, then the stuff inside doesn't show up. 当我将其添加到JScrollPane时,其中的内容不会显示。

import java.awt.event.*;
import java.io.File;
import javax.swing.*;

public class Launch extends JFrame implements ActionListener {
private static final long serialVersionUID = 5291490384908841627L;
JButton OK, create;
JList players;
File player;
public static void main(String[] args) {
    new Launch();
}
private Launch() {
    // don't do this
    this.setSize(600, 600);
    this.setLocationRelativeTo(null);
    this.setTitle("A Word Game");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    Box box = Box.createVerticalBox();
    OK = new JButton("OK");
    create = new JButton("Create new player");
    OK.addActionListener(this);
    create.addActionListener(this);
    String[] playerList = getPlayers();
    players = new JList(playerList);
    players.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane scroll = new JScrollPane(players, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    //scroll.add(players);
    final JLabel choosePrompt = new JLabel("Choose a player.");
    box.add(Box.createVerticalStrut(20));
    box.add(choosePrompt);
    box.add(Box.createVerticalStrut(20));
    box.add(scroll);
    box.add(Box.createVerticalStrut(20));
    box.add(OK);
    box.add(Box.createVerticalStrut(20));
    box.add(create);
    box.add(Box.createVerticalStrut(20));
    this.add(box);
    pack();
    this.setVisible(true);
}
private String[] getPlayers() {
    /*File playerDirectory = new File("players");
    File[] playersInFiles = playerDirectory.listFiles();
    String[] players = new String[playersInFiles.length];
    for (int counter = 0; counter < playersInFiles.length; counter ++) {
        players[counter] = trimTXT(playersInFiles[counter].getName());
    }*/
    String[] players = {"Bob", "Jane"};
    return players;
}
private String trimTXT(String original) {
    return original.substring(0, original.length() - 4);
}
@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(OK)) {
        String name = (String)players.getSelectedValue();
        if (name == null) {
            return;
        }
        player = new File(name + ".txt");
    } else if (e.getSource().equals(create)) {
        //create a new character, all that what-not
    }
}
}

错误是

 scroll.add(players)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM