Welcome to JAVA!之displaying a checkerboard

原创 2015年11月18日 12:38:27

原题:Write a program that displays a checkerboard in which each whit and black cell is a JButton with a background black or white.

代码如下:

package com.ytu.gui;

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class CheckerBoard extends JFrame {
	public CheckerBoard() {
		super();
		JButton[] jbts = new JButton[64];
		setSize(600, 600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new GridLayout(8, 8));
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				jbts[i] = new JButton();
				if ((i + j) % 2 == 0)
					jbts[i].setBackground(Color.white);
				else
					jbts[i].setBackground(Color.black);
				add(jbts[i]);
			}
		}
		setVisible(true);
	}

	public static void main(String[] args) {
		new CheckerBoard();
	}
}
运行结果:



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Welcome to JAVA!(第8章课后习题)

8.1Following the example of the Circle class in &8.2,design a class named Rectangle to represent a r...

Welcome to JAVA!(第二课课后练习)

2.9 Show the output. public class Main { public static void main(String[] args) { System.out.pr...

Welcome to JAVA!(第3章课后习题)

3.1

Welcome to JAVA!(第4章课后习题)

4.1* Write a program that reads an unspecified number of integers,determines how many postive and ne...

Welcome to JAVA!(第一课课后练习)

1.16 The following program is wrong.Reorder the lines so that the program displays mornig followed b...

Welcome to JAVA!之Swing common features

12.8 Display a frame that contains six labels.Set the background of the labels to white.Set the fore...

welcome to here

  • 2014-09-05 13:37
  • 5.19MB
  • 下载

Displaying Graphics with OpenGL ES (六)——Responding to Touch Events

响应触摸事件制作对象移动,根据预先设定的程序,如旋转的三角形,但是如果你想用户与OpenGL ES的图形交互?OpenGL ES应用程序的触摸交互的关键是在GLSurfaceView的onTouchE...

Welcome to jquery-confirm

  • 2016-01-15 15:07
  • 558KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)