与JButton togather的JComponent

时间:2015-05-06 19:55:13

标签: java swing jbutton jcomponent

使用JComponent和JButton时遇到问题。

public class GameWindow extends javax.swing.JFrame{
    private javax.swing.JLabel freeCardLabel;
    private javax.swing.JPanel NewGameBody2;
    private Game game;

    public GameWindow (int rozmer, int numberOfPlayers, int numberOfTreasures){       
        /*create a mazeboard*/
        game = new Game();
        game.newGame(rozmer, numberOfPlayers, numberOfTreasures);



        /* the graphic rendering */
        JFrame gameWindow = new JFrame();
        gameWindow.setSize(1000, 730);


        /* create panel for button*/
        JPanel buttonPanel = new JPanel();
        /* create button */
        JButton button = new JButton("OK");   
        //button.setBorder(BorderFactory.createEmptyBorder());
        //button.setContentAreaFilled(false);
        button.setPreferredSize(new Dimension(40,40));
        buttonPanel.add(button);
        buttonPanel.setMinimumSize(new java.awt.Dimension(150, 700));
        gameWindow.setContentPane(buttonPanel);


        GameComponent GC = new GameComponent(rozmer, game);
        gameWindow.add(GC);


        gameWindow.setTitle("Labyrinth - The Game");
        gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameWindow.setVisible(true);
        gameWindow.setResizable(false);
        gameWindow.getContentPane().setBackground(Color.gray);


    }

这是我的GameWindow课程。我想有类似的东西:

my game

而GameComponent GC = new GameComponent(rozmer,game);将绘制迷宫。使用上面张贴的代码,按钮将只显示没有迷宫。

GameComponent正在使用:

@Override
    public void paintComponent(Graphics g){
        image = null;
        offset = calculateOffset(rozmer);
        super.paintComponent(g);


        /* draw the whole field */
        for (int i = 0; i < rozmer; i++){
            for (int j = 0; j < rozmer; j++){                
                selectImage(i, j);
                g.drawImage(image, offset+150+j*63+j*1, offset+63*i+i*1, this);
            }
        }
        /* draw free card */
        selectImage(-2, -2);
        g.drawImage(image, offset+270+(rozmer-1)*63, offset+63*(rozmer-1)-50, this);
    }
}

并扩展JComponent类。

我想在一个窗口中同时拥有按钮和我的Labyrint。有人可以帮我吗?非常感谢你。

编辑:在GameComponent中重写getPreferredSize()后,它会同时绘制两个,然后按下Labyrinth并且按钮位于右中:(

0 个答案:

没有答案