我在用Java构建ui时遇到问题

时间:2018-11-05 15:55:32

标签: java swing user-interface panel frame

我想要一个JFrame并在其上添加一个与JFrame大小相同的JPanel“ mainPanel”,然后我想在mainPanel上添加另一个JPanel“ gamePanel”,这是一个GridLayout,大小为500x500 Pixels。但是,如果我将gamePanel的颜色设置为蓝色,则一切都是蓝色的。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javafx.scene.layout.Border;

public class GameViewImpl extends JFrame implements GameView {  

    JPanel mainPanel;
    JFrame mainFrame;
    JPanel gamePanel;

    @Override
    public void setController(GameController controller) {
        // TODO Auto-generated method stub
    }

    @Override
    public void show(int c, int r, boolean isHouse, int houseSize, int houseIndex) {
        // TODO Auto-generated method stub      
    }

    public void createAndShowThread() {                 
        mainFrame = new JFrame();
        mainFrame.setSize(1000,500);
        mainPanel = new JPanel(new BorderLayout());
        mainFrame.setVisible(true);
        mainPanel.setSize(1000,500);
        mainFrame.add(mainPanel);
        mainFrame.setResizable(false);
        gamePanel = new JPanel(new GridLayout(10,10));
        gamePanel.setSize(500,500);
        gamePanel.setBackground(Color.blue);
        mainPanel.add(gamePanel);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }
}

更新的代码:

import javax.swing.SwingUtilities;

public class Main {

    static public void main(String Args[]) {

        GameModelImpl gameModel = new GameModelImpl(10,10);
        GameViewImpl gameView = new GameViewImpl();
        GameControllerImpl gameController = new GameControllerImpl();

        gameModel.setView(gameView);
        gameView.setController(gameController);
        gameController.setModel(gameModel);
        gameView.createAndShowThread();
        SwingUtilities.invokeLater(()->gameView.createAndShowThread());
    }
}

0 个答案:

没有答案