import java.awt.BorderLayout;
public class GridTest extends JFrame
{
private JPanel schachPanel;
private JPanel topPanel,leftPanel,schachbrettPanel;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
GridTest frame = new GridTest();
frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GridTest()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 410, 408);
schachPanel = new JPanel();
schachPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1, 8, 0, 0));
topPanel.add(createLabel("A"));
topPanel.add(createLabel("B"));
topPanel.add(createLabel("C"));
topPanel.add(createLabel("D"));
topPanel.add(createLabel("E"));
topPanel.add(createLabel("F"));
topPanel.add(createLabel("G"));
topPanel.add(createLabel("H"));
leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(8, 1, 0, 0));
leftPanel.add(createLabel("1"));
leftPanel.add(createLabel("2"));
leftPanel.add(createLabel("3"));
leftPanel.add(createLabel("4"));
leftPanel.add(createLabel("5"));
leftPanel.add(createLabel("6"));
leftPanel.add(createLabel("7"));
leftPanel.add(createLabel("8"));
schachbrettPanel=new JPanel()
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Rectangle bounds=this.getBounds();
Graphics2D g2=(Graphics2D)g;
int boardSize=Math.min(bounds.width, bounds.height);
int fieldLength=boardSize/8;
g2.setColor(Color.black);
g2.fillRect(0, 0, fieldLength*8, fieldLength*8);
g2.setColor(Color.white);
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if((j+i)%2==0)
{
g2.fillRect(i*fieldLength, j*fieldLength, fieldLength, fieldLength);
}
}
}
}
};
schachbrettPanel.setPreferredSize(new Dimension(400,400));
getContentPane().add(schachPanel,BorderLayout.CENTER);
GroupLayout groupLayout = new GroupLayout(schachPanel);
groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
.addComponent(leftPanel,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createParallelGroup()
.addComponent(topPanel,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE,Short.MAX_VALUE)
.addComponent(schachbrettPanel,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE,Short.MAX_VALUE)
)
.addContainerGap()
);
groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
.addComponent(topPanel,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createParallelGroup()
.addComponent(leftPanel,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE,Short.MAX_VALUE)
.addComponent(schachbrettPanel,GroupLayout.PREFERRED_SIZE,GroupLayout.PREFERRED_SIZE,Short.MAX_VALUE)
)
.addContainerGap()
);
schachPanel.setLayout(groupLayout);
pack();
}
public static JLabel createLabel(String text)
{
JLabel label = new JLabel(text);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.CENTER);
return label;
}
}