Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JList不显示在JScrollPane内部_Java_Swing_Jscrollpane_Jlist - Fatal编程技术网

Java JList不显示在JScrollPane内部

Java JList不显示在JScrollPane内部,java,swing,jscrollpane,jlist,Java,Swing,Jscrollpane,Jlist,问题是玩家不会出现在JScrollPane中。滚动窗格就在那里,里面什么也没有。我在players文件夹中有一堆代表玩家的空白.txt文件。它们只是用来测试的。值得注意的是,当JList不在JScrollPane中时,它可以正常工作。当我将它添加到JScrollPane中时,里面的内容不会显示。错误是 import java.awt.event.*; import java.io.File; import javax.swing.*; public class Launch extends J

问题是玩家不会出现在JScrollPane中。滚动窗格就在那里,里面什么也没有。我在players文件夹中有一堆代表玩家的空白.txt文件。它们只是用来测试的。值得注意的是,当JList不在JScrollPane中时,它可以正常工作。当我将它添加到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
    }
}
}
错误是

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
    }
}
}

@克利奥帕特拉你的回答直截了当。我只是忍不住把代码发回@AndrewThompson,包括它糟糕的风格。。。露齿一笑:——@kleopatra你的回答直截了当。我只是忍不住把代码发回@AndrewThompson,包括它糟糕的风格。。。笑嘻嘻:-当我们在这方面:考虑提高您的代码的可读取性,包括格式化,遵循java命名约定,合理的相关行分组,偶尔空行作为空白…好吧。。。加快阅读速度,我们正在考虑:提高您的代码的可读取性,包括格式化,遵循java命名约定,合理的相关线分组,偶尔空行作为空白…好吧。。。加快reading@PicklishDoorknob:明智地使用你的新权力。@PicklishDoorKnop:明智地使用你的新权力。
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
    }
}
}