I have a grid which is place in firstPanel and I would like this to fit the ext size of the current, and then the second panel rightSidePanel to be placed beside that. However the firstPanel extends further than the grid meaning that the rightSidePanel appears further away from the grid. Which I do not want. How would I change the size of firstPanel I do not care for the re sizing of components - as given by layout managers - as the screen is not re sizable.

enter image description here

package com.aqagame.harrykitchener;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;

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


public class Game 
{

    private JButton moveC1But, moveC2But, rollDiceButton;

    public Game()
    {

        JFrame window = new JFrame ("Main Game");
        final JPanel firstPanel = new JPanel(new GridLayout(3,1))
        {
            public void paintComponent(Graphics g)
            {   
                Graphics2D g2d = (Graphics2D) g;

                super.paintComponent(g2d);

                int width = getWidth() / 4;
                int height = getHeight() / 11;

                for(int i = 0; i<4; i++)
                {
                    g.drawLine(i * width, 0, i * width, 600);
                }

                for(int i = 0; i < 11; i++)
                {
                    g.drawLine(0, i * height, 455, i * height);
                }

                g.drawOval(170, 515, 50, 50);
                g.drawOval(240, 515, 50, 50);
                g.drawOval(320, 515, 50, 50);
                g.drawOval(390, 515, 50, 50);
            }
        };

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(firstPanel, BorderLayout.CENTER);

        JPanel rightSidePanel = new JPanel(new BorderLayout());
        moveC1But = new JButton("Move Counter 1");
        moveC2But = new JButton("Move Counter 2");
        rollDiceButton = new JButton("Roll Dice");
        rightSidePanel.add(moveC1But, BorderLayout.NORTH);
        rightSidePanel.add(rollDiceButton, BorderLayout.SOUTH);

        mainPanel.add(rightSidePanel, BorderLayout.EAST);

        rightSidePanel.setBackground(Color.cyan);
        firstPanel.setBackground(Color.red);

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(mainPanel);
        window.setSize(800, 600);
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        window.setResizable(false);

    }


    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new Game();
            }
        });

    }

}
有帮助吗?

解决方案

It will work fine if JFrame is re-sizable also.

for (int i = 0; i < 4; i++) {
    g.drawLine(i * width, 0, i * width, getHeight());
}

for (int i = 0; i < 11; i++) {
    g.drawLine(0, i * height, getWidth(), i * height);
}

--EDIT--

complete code to set the oval also in the center of last row

            int ovalSize = 0;
            if (width < height) {
                ovalSize = width;
            } else {
                ovalSize = height;
            }

            int center = (width - ovalSize) / 2;

            for (int i = 0; i < 4; i++) {
                g.drawLine(i * width, 0, i * width, getHeight());
                g.drawOval(i * width + center, height * 10, ovalSize, ovalSize);
            }

            for (int i = 0; i < 11; i++) {
                g.drawLine(0, i * height, getWidth(), i * height);
            }

screenshot:

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top