简体   繁体   English

JList显示String []文本被截断

[英]JList displaying String[] text cut off

I'm trying to populate a JList with items from an ArrayList<String[]> . 我正在尝试使用ArrayList<String[]>项目填充JList Each String[] is of the form ["I","am","an","example"] and there's nothing I can do about the input form - its from a third party. 每个String[]的格式为[“ I”,“ am”,“ an”,“ example”],对于输入格式,我无能为力-它来自第三方。 What I'd like is simply a JList with each String[] expanded out on a different line. 我想要的只是一个JList ,每个String[]扩展到另一行。 When I use the following code, though, the first few characters are cut off the left side of the JList - its cutting off mid character so its an issue with pixels not characters. 但是,当我使用以下代码时,前几个字符从JList的左侧截去-它的截断中间字符,因此像素而不是字符的问题。

The class below is set as the content pane on a JFrame elsewhere in the program, I didn't think it was necessary to copy that over here but if it would be useful then I can trim it down and put it up for viewing. 下面的类被设置为程序其他地方JFrame上的内容窗格,我认为没有必要在此处复制该类,但是如果有用,那么我可以将其修整并放置以供查看。

public class BookScreen extends JPanel{
ListSelectionModel lsm;
ArrayList <String> atList;
JList atBox;
MainForm mf;

public BookScreen (MainForm mf){
    //I'm aware this bit is clunky, it was a quick and dirty to test it displays 
    //properly before I cleaned it up
    ArrayList<String[]> books= mf.getWorld().getBooks();
    atList=new ArrayList();
    for (String[] s:books){
        atList.add(Arrays.toString(s));
    }
    //end clunky
    atBox = new JList(atList.toArray());
    lsm = atBox.getSelectionModel();
    lsm.addListSelectionListener(new BookScreen.AtListSelectionHandler());
    atBox.setVisibleRowCount(-1);
    atBox.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    atBox.setLocation(0, 0);
    atBox.setVisible(true);
    this.add(atBox);
    this.setVisible(true);
}
class AtListSelectionHandler implements ListSelectionListener{
    @Override
    public void valueChanged(ListSelectionEvent e){
    }
}
}

Screenshot of problem: 问题截图: 屏幕截图

The problem is that you don't set a layout manager on the panel, which means that the default FlowLayout will be used. 问题是您没有在面板上设置布局管理器,这意味着将使用默认的FlowLayout If there's only one component this layout places it centered on the container; 如果只有一个组件,则此布局将其放置在容器的中心; if the component is wider than the container its edges are trimmed. 如果组件比容器宽,则会修剪其边缘。

To solve the problem simply set a different layout manager, for example BorderLayout : 要解决此问题,只需设置其他布局管理器,例如BorderLayout

this.setLayout(new BorderLayout());
this.add(atBox);

More information: Creating a GUI With JFC/Swing: Using layout managers . 详细信息: 使用JFC / Swing创建GUI:使用布局管理器

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

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