簡體   English   中英

輸出未正確顯示在 GUI 上

[英]output not being displayed on GUI properly

我正在嘗試連續顯示 4 個圖塊。 但是,GUI 上似乎只顯示了最后一個圖塊,我不知道為什么。 我環顧互聯網,找不到任何我能理解的解決方案。 這是我的代碼:

import java.awt.*;
import javax.swing.*;

// creates a GUI and displays the game tiles made in gameTile.java
public class testGameTileClass { 
    gameTile tile1;
    gameTile tile2;
    gameTile tile3;
    gameTile tile4;

    public testGameTileClass() {
        // sets up GUI
        JFrame frame = new JFrame("Game Tiles");
        JPanel panel = new JPanel();
        frame.setSize(1280, 720);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);   
        testConstructor1(frame);
        frame.setVisible(true);
    }    

    public void testConstructor1(JFrame frame) {
        tile1 = new gameTile(10, 10, 64, Color.RED.darker());
        frame.add(tile1);
        tile2 = new gameTile(84, 10, 64, Color.GREEN);
        frame.add(tile2);
        tile3 = new gameTile(148, 10, 64, Color.BLUE);
        frame.add(tile3);
        tile4 = new gameTile(212, 10, 64, Color.ORANGE);
        frame.add(tile4);
    }

    public static void main(String[] args) {
        new testGameTileClass();
    }
}

這是gameTile類代碼:

    import javax.swing.*;
import java.awt.*;

public class gameTile extends JPanel {
    private Color color;        //color of gameTile
    private int x, y, size;     // x and y coords, and size of gameTile
    public gameTile (int x_, int y_, int size_, Color color_) {
        x = x_;
        y = y_;
        size = size_;
        color = color_;        
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(2));
        g.setColor(color);
        g.fillRect(x, y, size, size);
        g2.setColor(Color.BLACK);
        g2.drawRect(x, y, size, size);
    }
}

這是我的結果。 有沒有什么辦法解決這一問題?

Oracle 有一個有用的教程,使用 Swing 創建 GUI 跳過使用 NetBeans IDE 學習搖擺部分。 密切關注執行自定義繪畫部分。

我繼續創建了您嘗試創建的 GUI。

在此處輸入圖像描述

創建 Swing 應用程序時,我使用模型-視圖-控制器(MVC) 模式。 這種模式使我能夠分離我的關注點並一次專注於應用程序的一小部分。

我使用了您的游戲磁貼創意並創建了一個普通的 Java getter/setter 類來保存磁貼。 在這個簡單的例子中,我只需要顏色和矩形信息。 在一個更復雜的示例中,您可以創建一個BufferedImage ,其中包含比僅一種顏色更詳細的圖像。

我在一個單獨的模型類中收集了四個GameTile實例。 模型類將在另一個單獨的普通 Java getter/setter 類中保存應用程序的所有信息。

創建模型后,編寫視圖很簡單。 您有一個繪圖JPanel ,您可以在其中繪制所有游戲圖塊。

在這個簡單的例子中,沒有控制器。 控制器由一個或多個ActionActionListener類組成。

這是完整的可運行代碼。 我將所有額外的類都做成了內部類,這樣我就可以將代碼作為一個塊發布。 您應該將這些類分隔到一個包中的不同文件中。 對於我的大多數 Swing 項目,我創建了單獨的模型、視圖和控制器包。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGameTileExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TestGameTileExample());
    }

    private final GameModel model;

    public TestGameTileExample() {
        this.model = new GameModel();
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Game Tiles");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        frame.add(new DrawingPanel(model), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        private final GameModel model;

        public DrawingPanel(GameModel model) {
            this.model = model;
            this.setPreferredSize(new Dimension(1200, 720));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            for (GameTile tile : model.getTiles()) {
                g.setColor(Color.BLACK);
                Rectangle r = tile.getTile();
                g.fillRect(r.x, r.y, r.height, r.width);
                g.setColor(tile.getColor());
                g.fillRect(r.x + 2, r.y + 2, r.height - 4, r.width - 4);
            }
        }
    }

    public class GameModel {

        private final GameTile[] tiles;

        public GameModel() {
            this.tiles = new GameTile[4];
            tiles[0] = new GameTile(10, 10, 64, 64, Color.RED.darker());
            tiles[1] = new GameTile(84, 10, 64, 64, Color.GREEN);
            tiles[2] = new GameTile(148, 10, 64, 64, Color.BLUE);
            tiles[3] = new GameTile(212, 10, 64, 64, Color.ORANGE);
        }

        public GameTile[] getTiles() {
            return tiles;
        }

    }

    public class GameTile {

        private final Color color;

        private final Rectangle tile;

        public GameTile(int x, int y, int width, int height, Color color) {
            this.tile = new Rectangle(x, y, width, height);
            this.color = color;
        }

        public Color getColor() {
            return color;
        }

        public Rectangle getTile() {
            return tile;
        }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM