2012-03-02 81 views
1
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 
    } 
} 
} 

问题是玩家没有出现在JScrollPane中。滚动窗格就在那里,里面没有任何东西。我有一堆代表播放器文件夹中的播放器的空白.txt文件。他们只是为了测试。 weid的事情是,当JList不在JScrollPane中时,它工作正常。当我将它添加到JScrollPane中时,内部的东西不会显示出来。JList不显示在JScrollPane里面

+1

而我们在它:考虑到提高代码的可读性 - (包括格式,下面的Java命名约定,相关线路合理分组,偶尔空行作为空白...) - 以及...加快阅读 – kleopatra 2012-03-02 14:28:12

回答

8
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 
    } 
} 
} 
+0

@PicklishDoorknob:明智地使用你的新力量。 – trashgod 2012-03-02 19:28:41

6

误差

scroll.add(players) 
+0

+1击败我29秒。 ;) – 2012-03-02 14:22:56

+0

好的。谢谢。 :)安德鲁汤普森::)如此接近 – Doorknob 2012-03-02 14:25:09

+0

@安德鲁·汤普森懒惰我遗漏了复制代码:-) – kleopatra 2012-03-02 14:25:13