下面是我写的程序,其中x.image是X图片,o.image是O图片,
但程序显示的结果总是很单一,总是一个X和一个O,没有别的了
调试几步后,又说找不到源文件,不知道为什么package ex12;
import javax.swing.*;
import java.awt.*;public class Ex12_07 extends JFrame{
private ImageIcon OIcon=new ImageIcon("E:/学习资料/book/image/o.gif");
private ImageIcon XIcon=new ImageIcon("E:/学习资料/book/image/x.gif");
public Ex12_07(){
JLabel XLabel=new JLabel(XIcon);
JLabel OLabel=new JLabel(OIcon);
setLayout(new GridLayout(3,3,5,5));
for(int i=0;i<9;i++){
int temp=(int)Math.random()%3;
if(temp==0){
add(XLabel);
}
if(temp==1){
add(OLabel);
}
if(temp==2){
add(new JLabel());
}
}
}
public static void main(String[] args) {
// TODO 自动生成方法存根
JFrame frame=new Ex12_07();
frame.setTitle("Tictactoe");
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}
但程序显示的结果总是很单一,总是一个X和一个O,没有别的了
调试几步后,又说找不到源文件,不知道为什么package ex12;
import javax.swing.*;
import java.awt.*;public class Ex12_07 extends JFrame{
private ImageIcon OIcon=new ImageIcon("E:/学习资料/book/image/o.gif");
private ImageIcon XIcon=new ImageIcon("E:/学习资料/book/image/x.gif");
public Ex12_07(){
JLabel XLabel=new JLabel(XIcon);
JLabel OLabel=new JLabel(OIcon);
setLayout(new GridLayout(3,3,5,5));
for(int i=0;i<9;i++){
int temp=(int)Math.random()%3;
if(temp==0){
add(XLabel);
}
if(temp==1){
add(OLabel);
}
if(temp==2){
add(new JLabel());
}
}
}
public static void main(String[] args) {
// TODO 自动生成方法存根
JFrame frame=new Ex12_07();
frame.setTitle("Tictactoe");
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}
JLabel OLabel=new JLabel(OIcon);
放到循环里,不然总是两个相同的JLabel被加来加去。
Math.random()不是0-1之间的实数吗?这句一直是0吧。
int temp=(int)Math.random()*3