资源简介
适用于初学者,文件是编写的井字棋小程序,内有源码。
代码片段和文件信息
package tiatactoe;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gui_TTT extends Jframe{
private JLabel labtitle;
private JButton[][] btncell=new JButton[3][3];
private JButton btnNewbtnBack;
private JDialog dialog;
private static int index=0;
/*private ImageIcon bb=new ImageIcon(“src/tiatactoe/blackboard.png“);
private ImageIcon wall=new ImageIcon(“src/tiatactoe/wall.png“);
private JLabel label1=new JLabel()label2=new JLabel();*/
public Gui_TTT() {
super(“TicTacToe“);
JPanel panel=(JPanel)getContentPane();
panel.setOpaque(false);
setSize(300500);
setVisible(true);
panel.setLayout(null);
Font font=new Font(“宋体“Font.BOLD24);
labtitle=new JLabel(“TicTacToe“);
labtitle.setFont(font);
labtitle.setForeground(Color.MAGENTA);
labtitle.setBounds(75 0 150 60);
panel.add(labtitle);
btnNew=new JButton(“new“);
btnNew.setFont(new Font(“宋体“Font.PLAIN10));
btnNew.setBounds(225 50 50 30);
btnNew.setFocusPainted(false);
btnNew.setContentAreaFilled(false);
btnBack=new JButton(“back“);
btnBack.setFont(new Font(“宋体“Font.PLAIN8));
btnBack.setBounds(25505030);
btnBack.setFocusPainted(false);
btnBack.setContentAreaFilled(false);
panel.add(btnBack);
panel.add(btnNew);
ActionListener cellClick=new ActionListener() {
public void actionPerformed(ActionEvent e) {
index++;
if(index%2==1) {
JButton btn=(JButton)e.getSource();
if(btn.getText()!=“√“&&btn.getText()!=“ד) {
btn.setFont(new Font(“宋体“Font.BOLD48));
btn.setForeground(Color.white);
btn.setText(“√“);
if(isOver()) {
dialog=new JDialog();
dialog.setVisible(true);
dialog.setBounds(0 0 100 100);
dialog.add(new JLabel(“The player1 wins!“));
}
else System.out.println(“Go on!“);
}
else {
System.out.println(“Here‘s ocuppied.“);
index--;
}
}
else {
JButton btn=(JButton)e.getSource();
if(btn.getText()!=“√“&&btn.getText()!=“ד) {
btn.setFont(new Font(“宋体“Font.BOLD48));
btn.setForeground(Color.white);
btn.setText(“ד);
if(isOver()) {
dialog=new JDialog();
dialog.setVisible(true);
dialog.setBounds(0 0 100 100);
dialog.add(new JLabel(“The player2 wins!“));
}
else System.out.println(“Go on!“);
}
else {
System.out.println(“Here‘s ocuppied.“);
index--;
}
}
}
};
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
btncell[i][j]=new JButton();
panel.add(btncell[i][j]);
btncell[i][j].setBounds(10+90*i 100+90*j 85 85);
btncell[i][j].addActionListener(cellClick);
btncell[i][j].setFocusPainted(false);
btncell[i][j].setContentAreaFilled(false);
}
}
ActionListener newClick=new ActionListener() {
public void actionPerformed(ActionEvent e) {
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-10 15:39 tiatactoe\
文件 12794 2017-12-10 15:09 tiatactoe\blackboard.png
文件 4705 2017-12-10 16:16 tiatactoe\Gui_TTT.java
文件 254566 2017-12-10 15:29 tiatactoe\wall.png
- 上一篇:电磁环岛相关资料.zip
- 下一篇:beijing_CSV格式天气数据.zip
评论
共有 条评论