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);
}
我会放更少和更紧凑的代码,但老实说我不知道问题出在哪里。
问题是 JList stacklist
中的项目没有显示。它看起来像这样:
(来源:mediafire.com)
(箭头指向JList)
我已经为此排查了几个小时,我担心这是一件非常明显的事情,会导致我永远放弃编程。
最佳答案
我认为您需要了解 GridLayout
的工作原理...
stackpanel.setLayout(new GridLayout(100, height));
Basic 说,为我创建一个具有 100 行和 height
列数的网格...这是为每个单元格保留空间...
如果我把它改成...
stackpanel.setLayout(new GridLayout(code.length, 1));
我明白了...
但我只是猜测您要实现的目标...
您还应该尽可能避免使用 setPreferredSize
关于java - JList 没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213780/