exercise11_7.java
来自「java程序设计 机械工业出版社 书籍代码」· Java 代码 · 共 59 行
JAVA
59 行
// Exercise11_7.java:
import javax.swing.*;
import java.awt.*;
public class Exercise11_7 extends JFrame {
/** Default constructor */
public Exercise11_7() {
// Set the window title
setTitle("Exercise11_7");
// Add buttons to the frame
getContentPane().add(new CheckerBoard());
}
/** Main method */
public static void main(String[] args) {
Exercise11_7 frame = new Exercise11_7();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 80);
frame.setVisible(true);
}
}
class CheckerBoard extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth() / 8;
int height = getHeight() / 8;
int x = 0;
int y = 0;
boolean isWhite = true;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (isWhite) {
g.setColor(Color.white);
isWhite = false;
}
else {
g.setColor(Color.black);
isWhite = true;
}
g.fillRect(x, y, width, height);
x += width;
}
if (i % 2 == 0)
isWhite = false;
else
isWhite = true;
x = 0;
y += height;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?