[英]JScrollPane - content and Scrollbar not rendering
我正在創建一個小型的“生活游戲”應用程序。 我正在為所有單元格使用“動態宇宙”(在我的項目中命名為Tiles)。 但是由於某種原因,我的JScrollPane
和JButtons
沒有渲染到框架中。 我只是得到一個空的JFrame。 控制器正在返回值,並且正在構造按鈕並將其添加到面板。 只是jsp.setViewportView(p);
似乎沒有更新UI。
主要:
GOLController controller = new GOLController();
controller.run();
SwingUtilities.invokeLater(() -> {
GameOfLifeFrame frame = new GameOfLifeFrame(controller);
frame.init();
});
UI類:
package org.gameoflife.ui;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.gameoflife.controller.GOLController;
import org.gameoflife.model.Tile;
public class GameOfLifeFrame extends JFrame {
private final GOLController controller;
private JScrollPane jsp;
public GameOfLifeFrame(GOLController controller) throws HeadlessException {
super("Game of Life");
this.controller = controller;
}
public void init() {
jsp = new JScrollPane();
add(jsp);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
controller.setLock();
this.draw();
controller.releaseLock();
}
public void draw(){
List<List<Tile>> currentState = controller.getTiles();
GridLayout layout = new GridLayout(currentState.size(), currentState.get(0).size());
JPanel p = new JPanel(layout);
currentState.stream().forEach((currentTiles) -> {
currentTiles.stream().map((t) -> {
JButton b=new JButton(" ");
b.setBackground(t.isHasLife() ? Color.GREEN : Color.BLACK);
return b;
}).forEach((b) -> {
p.add(b);
});
});
jsp.removeAll();
jsp.setViewportView(p);
}
}
我可能忽略了一些非常愚蠢的東西,感謝您的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.