简体   繁体   中英

JList is not Showing Up

private static void initGUI(char[][] code){

    int width = (int) (code[0].length * new JLabel().getFont().getSize2D());
    int height = (int) (code.length * new JLabel().getFont().getSize2D()) + 200;

    gridgui = new JLabel[code.length][code[0].length];
    for(int x = 0; x < code.length; ++x){
        for(int y = 0; y < code[0].length; ++y){
            gridgui[x][y] = new JLabel();
            gridgui[x][y].setText(""+code[x][y]);
            gridgui[x][y].setBackground(Color.WHITE);
            gridgui[x][y].setOpaque(true);
        }
    }
    listModel = new DefaultListModel<Integer>();
    listModel.addElement(5);

    //Create the list and put it in a scroll pane.
    stacklist = new JList<Integer>(listModel);
    JScrollPane listScrollPane = new JScrollPane(stacklist);
    listScrollPane.getViewport().setView(stacklist);

    frame = new JFrame("Befunge 93! Wow!");

    gridpanel = new JPanel();
    gridpanel.setLayout(new GridLayout(gridgui.length, gridgui[0].length));
    gridpanel.setPreferredSize(new Dimension(width, height));

    for(int x = 0; x < gridgui.length; ++x){
        for(int y = 0; y < gridgui[0].length; ++y){
            gridpanel.add(gridgui[x][y]);
        }
    }

    stackpanel = new JPanel();
    stackpanel.setLayout(new GridLayout(100, height));
    stackpanel.setPreferredSize(new Dimension(width, height));
    stackpanel.add(listScrollPane);


    JPanel container = new JPanel();
    container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
    container.add(gridpanel);
    container.add(stackpanel);

    frame.add(container);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

I would have put less and more compact code, but I honestly have no clue where the problem could be.

The issue is that the items in the JList stacklist don't show up. It just looks like this: JList的
(source: mediafire.com )
(with the arrow pointing to the JList)

I've been troubleshooting this for hours and I fear it's something painfully obvious that will cause my to quit programming forever.

I think you need to understand how GridLayout works...

stackpanel.setLayout(new GridLayout(100, height));

Basic says, create me a grid which has 100 rows and height number of columns...This is reserving space for each cell...

If I change it to...

stackpanel.setLayout(new GridLayout(code.length, 1));

I get...
代码

But I'm only guess at what it is you're trying to achieve...

You should also avoid using setPreferredSize where you can

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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