Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 摆位问题_Java_Swing_Jtextarea_Flicker - Fatal编程技术网

Java 摆位问题

Java 摆位问题,java,swing,jtextarea,flicker,Java,Swing,Jtextarea,Flicker,我正在编写一个小游戏,使用Swing进行GUI时遇到了一个问题。我发布了一个截图,这样你就能更好地了解情况 起初,我只有一块我使用了GridLayout的板 但是,我需要添加一个显示分数的面板,因此我放弃了GridLayout,而选择了gridbagloayout。我为分数正确定位了新的JPanel,并在其中添加了2个JTextArea,用于显示结果 问题是,每次一个玩家移动(因此棋盘会重新绘制以反映其新状态),文本区域就会闪烁,并被棋盘部分覆盖(见屏幕截图)。知道问题是什么吗?我已经设置了所

我正在编写一个小游戏,使用
Swing
进行
GUI
时遇到了一个问题。我发布了一个截图,这样你就能更好地了解情况

起初,我只有一块我使用了
GridLayout
的板

但是,我需要添加一个显示分数的面板,因此我放弃了
GridLayout
,而选择了
gridbagloayout
。我为分数正确定位了新的
JPanel
,并在其中添加了2个
JTextArea
,用于显示结果

问题是,每次一个玩家移动(因此棋盘会重新绘制以反映其新状态),文本区域就会闪烁,并被棋盘部分覆盖(见屏幕截图)。知道问题是什么吗?我已经设置了所有组件的首选大小,并尝试使用
setOpaque
,但没有效果

主布局支架的代码

public class BoardLayout extends JFrame implements ModelObserver {

    /**
     * {@value}
     */
    private static final long serialVersionUID = 5834762299789973250L;

    /**
     * {@value}
     */
    private static final int RESULTS_PANEL_HEIGHT = 100;

    private final BoardEventsListener eventsListener;

    private final ResultsLayout resultsLayout;

    private class CellMouseListener implements MouseListener {

        private final int cellIndex;

        public CellMouseListener(final int index) {
            cellIndex = index;
        }

        @Override
        public void mouseClicked(final MouseEvent event) {
            eventsListener.onCellSelected(cellIndex);
        }

        @Override
        public void mouseEntered(final MouseEvent event) {
            // blank
        }

        @Override
        public void mouseExited(final MouseEvent event) {
            // blank
        }

        @Override
        public void mousePressed(final MouseEvent event) {
            // blank
        }

        @Override
        public void mouseReleased(final MouseEvent event) {
            // blank
        }

    }

    public BoardLayout(final BoardEventsListener listener) throws HeadlessException {
        this(listener, "", null);
    }

    public BoardLayout(GraphicsConfiguration graphicsConfiguration) {
        this(null, "", graphicsConfiguration);
    }

    public BoardLayout(final BoardEventsListener listener, String title,
            GraphicsConfiguration graphicsConfiguration) {
        super(title, graphicsConfiguration);

        eventsListener = listener;

        setLayout(new GridBagLayout());
        setBoardSize();
        populateCells();
        resultsLayout = attachResultsLayout();

        resultsLayout.setOpaque(true);

        setVisible(true);
    }

    private ResultsLayout attachResultsLayout() {
        final ResultsLayout resultsLayout = new ResultsLayout(Game.BOARD_COLUMN_COUNT
                * BoardCellLayout.WIDTH_BOARD_CELL, RESULTS_PANEL_HEIGHT);
        final GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.gridheight = GridBagConstraints.REMAINDER;
        constraints.gridx = 0;
        constraints.gridy = 8;
        getContentPane().add(resultsLayout, constraints, 64);
        return resultsLayout;
    }

    private void setBoardSize() {
        final Dimension boardDimension = getBoardDimension();
        setMinimumSize(boardDimension);
        setSize(boardDimension);
        setResizable(false);
    }

    private void populateCells() {
        final Container container = getContentPane();
        for (int i = 0; i < Game.BOARD_ROW_COUNT; ++i) {
            for (int j = 0; j < Game.BOARD_COLUMN_COUNT; ++j) {
                final BoardCellLayout currentCell = new BoardCellLayout();
                final int cellIndex = i * Game.BOARD_COLUMN_COUNT + j;
                final GridBagConstraints constraints = new GridBagConstraints();
                constraints.gridx = j;
                constraints.gridy = i;
                container.add(currentCell, constraints, cellIndex);
                currentCell.setOpaque(true);
                currentCell.addMouseListener(new CellMouseListener(cellIndex));
            }
        }
    }

    private Dimension getBoardDimension() {
        final Dimension boardDimension = new Dimension(Game.BOARD_COLUMN_COUNT
                * BoardCellLayout.WIDTH_BOARD_CELL, (Game.BOARD_ROW_COUNT + 1)
                * BoardCellLayout.HEIGHT_BOARD_CELL);
        return boardDimension;
    }

    public BoardLayout(String title) throws HeadlessException {
        this(null, title, null);
    }

    @Override
    public void setVisible(final boolean isVisible) {
        super.setVisible(isVisible);
        setBackground(Color.WHITE);
        pack();
    }

    @Override
    public void onModelChanged(Collection<Cell> changedCells, final int whiteDiscsCount,
            final int blackDiscsCount) {
        for (final Cell cell : changedCells) {
            final BoardCellLayout boardCell = getCellAt(cell.getIndex());
            boardCell.take(cell.getOwner());
        }
        resultsLayout.onResultChanged(whiteDiscsCount, blackDiscsCount);

    }

    @Override
    public void onNextMovesAcquired(Collection<Cell> nextMoves) {
        clearCellHighlight();
        for (final Cell cell : nextMoves) {
            final BoardCellLayout boardCell = getCellAt(cell.getIndex());
            boardCell.highlight();
        }
    }

    private void clearCellHighlight() {
        final Container container = getContentPane();
        for (int i = 0; i < container.getComponentCount() - 1; ++i) {
            final BoardCellLayout boardCellLayout = (BoardCellLayout) container.getComponent(i);
            boardCellLayout.clearHighlight();
        }
    }

    private BoardCellLayout getCellAt(final int index) {
        final Container container = getContentPane();
        return (BoardCellLayout) container.getComponent(index);
    }
}
JTextArea的代码,保存结果:

public class ResultsTextView extends JTextArea {

    /**
     * {@value}
     */
    private static final long serialVersionUID = -6354085779793155063L;

    /**
     * {@value}
     */
    public static final String BACKGROUND_COLOR = "#066f02";

    private static final int START_DISCS_NUMBER = 2;

    private final Player player;

    public ResultsTextView(final Player player) {
        super();
        setEditable(false);

        setBackground(Color.decode(BACKGROUND_COLOR));

        this.player = player;
        setText(player.toString() + " result is: " + START_DISCS_NUMBER);

        final Dimension dimension = new Dimension(100, 20);
        setPreferredSize(dimension);
        setMinimumSize(dimension);
        setMaximumSize(dimension);
    }

    public void setDiscCount(final int discCount) {
        setText(player.toString() + " result is:" + discCount);
    }

}

添加一个得分面板让我想到一种模式,其中游戏板和得分都是听同一个游戏模型的视图。一个被引用的例子是,还有一个基于网格的游戏,可能会提供一些关于设计和布局的见解。特别是,在
BorderLayout.NORTH
中的
RCStatus
视图和在
BorderLayout.SOUTH
中的
RCInfo
视图都可以收听
RCModel
。游戏面板占据
BorderLayout.CENTER

关于您提供的片段的一些注释:

  • 让主视图扩展
    JPanel
    ,并将其添加到
    JFrame
    实例中

  • 不要试图用一个布局来解决所有问题

  • 考虑
    将MouseAdapter
    扩展到
    实现MouseListener

  • 重新考虑


问题是我没有使用
SwingUtilities.invokeLater
更新GUI,这导致了闪烁。我开始使用它,现在一切都好了:)

很多,但是如果没有你正在使用的代码,没有一个对你有用。编辑我的答案:)谢谢!为了更快地获得更好的帮助,发布一个。同意,将您的板面板与底部面板分开。让他们各自担任小组成员;使用
GridLayout
板,并使用
BorderLayout.CENTER
BorderLayout.SOUTH
将底部面板的首选尺寸设置为
新尺寸(0,首选高度)@paranoidandroid:很好的观点;谢谢你的评论。引用的示例直接渲染网格;另一种选择是使用s
GridLayout
按钮。谢谢!我开始使用BorderLayout,尽管问题在别处:)这是唯一确定的方法。
public class ResultsTextView extends JTextArea {

    /**
     * {@value}
     */
    private static final long serialVersionUID = -6354085779793155063L;

    /**
     * {@value}
     */
    public static final String BACKGROUND_COLOR = "#066f02";

    private static final int START_DISCS_NUMBER = 2;

    private final Player player;

    public ResultsTextView(final Player player) {
        super();
        setEditable(false);

        setBackground(Color.decode(BACKGROUND_COLOR));

        this.player = player;
        setText(player.toString() + " result is: " + START_DISCS_NUMBER);

        final Dimension dimension = new Dimension(100, 20);
        setPreferredSize(dimension);
        setMinimumSize(dimension);
        setMaximumSize(dimension);
    }

    public void setDiscCount(final int discCount) {
        setText(player.toString() + " result is:" + discCount);
    }

}