栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

棋盘覆盖(java 动态)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

棋盘覆盖(java 动态)

这学期的算法课后作业,也是用java实现了动态的覆盖
代码不多

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.Jframe;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

public class chessBoard extends Jframe {

	private JPanel contentPane, panel;
	private JTextField textField;
	private Label[][] labels;
	private int size;
	public int r,c;
	private JTextField rText;
	private JTextField cText;

	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					chessBoard frame = new chessBoard();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	

	public static Color randomColor() {
		Random random = new Random();
		int r = random.nextInt(256);
		int g = random.nextInt(256);
		int b = random.nextInt(256);
		return new Color(r, g, b);
	}

	public chessBoard() {
		setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textField = new JTextField();
		textField.setBounds(77, 10, 60, 31);
		contentPane.add(textField);
		textField.setColumns(10);

		JLabel label = new JLabel("u89C4u683C");
		label.setFont(new Font("仿宋", Font.BOLD, 24));
		label.setBounds(10, 10, 70, 31);
		contentPane.add(label);

		JButton btnReady = new JButton("Ready");
		btnReady.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				size = Integer.valueOf(textField.getText());
				if ((size & size - 1) == 0) {
					r = Integer.valueOf(rText.getText());
					c = Integer.valueOf(cText.getText());
					if (r > size || r <= 0 || c > size || c <= 0) {
						JOptionPane.showMessageDialog(null, "输入行列值不合法!", "错误", JOptionPane.ERROR_MESSAGE);
						textField.setText("");
						rText.setText("");
						cText.setText("");
					} else {
						panel.removeAll();// 清空以前的
						panel.setLayout(new GridLayout(size, size, 4, 4));
						labels = new Label[size][];
						for (int i = 0; i < size; i++)
							labels[i] = new Label[size];

						for (int i = 0; i < size; i++) {
							for (int j = 0; j < size; j++) {
								labels[i][j] = new Label();
								labels[i][j].setBackground(Color.LIGHT_GRAY);
								panel.add(labels[i][j]);
							}
						}
						labels[r-1][c-1].setBackground(Color.DARK_GRAY);
						panel.updateUI();
					}
				} else {
					JOptionPane.showMessageDialog(null, "输入大小不合法!", "错误", JOptionPane.ERROR_MESSAGE);
					textField.setText("");
					rText.setText("");
					cText.setText("");
				}
			}

		});

		btnReady.setFont(new Font("仿宋", Font.BOLD, 24));
		btnReady.setBounds(347, 10, 102, 31);
		contentPane.add(btnReady);

		panel = new JPanel();
		panel.setBounds(10, 51, 566, 502);
		contentPane.add(panel);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JLabel lblNewLabel = new JLabel("u884C");
		lblNewLabel.setFont(new Font("宋体", Font.BOLD, 24));
		lblNewLabel.setBounds(144, 10, 31, 31);
		contentPane.add(lblNewLabel);

		rText = new JTextField();
		rText.setBounds(185, 10, 50, 31);
		contentPane.add(rText);
		rText.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("u5217");
		lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 24));
		lblNewLabel_1.setBounds(245, 10, 31, 31);
		contentPane.add(lblNewLabel_1);

		cText = new JTextField();
		cText.setBounds(286, 10, 50, 31);
		contentPane.add(cText);
		cText.setColumns(10);
		
		JButton btnNewButton = new JButton("Go");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Board(0,0,r-1,c-1,size);
				new Thread(
						new Runnable() {
							public void run() {
								panel.repaint();
								panel.updateUI();
							}
				}).start();
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.BOLD, 24));
		btnNewButton.setBounds(459, 10, 70, 31);
		contentPane.add(btnNewButton);
	}

	void Board(int tr, int tc, int dr, int dc, int size) {//dr dc 特殊
		
		if (size == 1)
			return;
	
		int s = size / 2;
		try {
			Thread.sleep(300);

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Color c = randomColor();
		while (c == Color.LIGHT_GRAY || c == Color.DARK_GRAY) {
			c = randomColor();
		}
		if(dr
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/760669.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号