JScrollPane - 内容和滚动条不呈现
JScrollPane - content and Scrollbar not rendering
我正在创建一个小型 Game of Life 应用程序。我为我的所有单元格(在我的项目中命名为 Tiles)使用 'dynamic universe'。但出于某种原因,我的 JScrollPane
和 JButtons
没有渲染到框架中。我刚得到一个空的 JFrame。控制器正在返回值,按钮正在构建并添加到面板中。只是 jsp.setViewportView(p);
似乎没有更新 UI.
主要:
GOLController controller = new GOLController();
controller.run();
SwingUtilities.invokeLater(() -> {
GameOfLifeFrame frame = new GameOfLifeFrame(controller);
frame.init();
});
UI class:
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);
}
}
我可能忽略了一些非常愚蠢的事情,我们将不胜感激。
这:jsp.removeAll()
会出现问题,因为它可能会删除视口和 JScrollBar
s,这也不是必需的,因为设置 viewportView
会做同样的事情无论如何
请记住,JScrollPane
是一个特殊组件,由一个 JViewPort
和两个 JScrollBar
组成,实际内容位于 JViewport
,而不是 JScrollPane
我正在创建一个小型 Game of Life 应用程序。我为我的所有单元格(在我的项目中命名为 Tiles)使用 'dynamic universe'。但出于某种原因,我的 JScrollPane
和 JButtons
没有渲染到框架中。我刚得到一个空的 JFrame。控制器正在返回值,按钮正在构建并添加到面板中。只是 jsp.setViewportView(p);
似乎没有更新 UI.
主要:
GOLController controller = new GOLController();
controller.run();
SwingUtilities.invokeLater(() -> {
GameOfLifeFrame frame = new GameOfLifeFrame(controller);
frame.init();
});
UI class:
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);
}
}
我可能忽略了一些非常愚蠢的事情,我们将不胜感激。
这:jsp.removeAll()
会出现问题,因为它可能会删除视口和 JScrollBar
s,这也不是必需的,因为设置 viewportView
会做同样的事情无论如何
请记住,JScrollPane
是一个特殊组件,由一个 JViewPort
和两个 JScrollBar
组成,实际内容位于 JViewport
,而不是 JScrollPane