永发信息网

谁能给我个java源代码

答案:3  悬赏:70  手机版
解决时间 2021-04-05 11:59
只要能弄出图形化的界面就行,不需要多复杂,我只是想试一下我的开发环境是否正确,刚开始学,所以不会写,谢谢了,最好直接贴上来
最佳答案
import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;
import java.awt.event.*;public class TicTacToe extends JApplet
{
private char whoseTurn='X';
private Cell[][] cell=new Cell[3][3];
private JLabel jlblStatus=new JLabel("X's turn to play");
public void init()
{
JPanel p=new JPanel();
p.setLayout(new GridLayout(3,3,0,0));
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
p.add(cell[i][j]=new Cell()); p.setBorder(new LineBorder(Color.red,1));
jlblStatus.setBorder(new LineBorder(Color.yellow,1));
this.getContentPane().add(p,BorderLayout.CENTER);
this.getContentPane().add(jlblStatus,BorderLayout.SOUTH);
}
public void start()
{}
public static void main(String[] args)
{
JFrame frame=new JFrame("TicTacToe");
TicTacToe applet=new TicTacToe();
frame.getContentPane().add(applet,BorderLayout.CENTER);
applet.init();
applet.start();
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation(
(screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2); frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public boolean isFull()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(cell[i][j].getToken()==' ')
return false;
return true;
}
public boolean isWin(char token)
{
for(int i=0;i<3;i++)
if((cell[i][0].getToken()==token)
&&(cell[i][1].getToken()==token)
&&(cell[i][2].getToken()==token))
{
return true;
}
for(int j=0;j<3;j++)
if((cell[0][j].getToken()==token)
&&(cell[1][j].getToken()==token)
&&(cell[2][j].getToken()==token))
{
return true;
}
if((cell[0][0].getToken()==token)
&&(cell[1][1].getToken()==token)
&&(cell[2][2].getToken()==token))
{
return true;
}
if((cell[0][2].getToken()==token)
&&(cell[1][1].getToken()==token)
&&(cell[2][0].getToken()==token))
{
return true;
}
return false;
}
public class Cell extends JPanel implements MouseListener
{
private char token=' ';
public Cell()
{
setBorder(new LineBorder(Color.black,1));
addMouseListener(this);
}
public char getToken()
{
return token;
}
public void setToken(char c)
{
token=c;
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(token=='X')
{
g.drawLine(10,10,getSize().width-10,getSize().height-10);
g.drawLine(getSize().width-10,10,10,getSize().height-10);
}
else if(token=='O')
{
g.drawOval(10,10,getSize().width-20,getSize().height-20);
}
}
public void mouseClicked(MouseEvent e)
{
if(token==' ')
{
if(whoseTurn=='X')
{
setToken('X');
whoseTurn='O';
jlblStatus.setText("O's turn");
if(isWin('X'))
{
JOptionPane.showMessageDialog(this,"X won! ","提示",
JOptionPane.YES_NO_CANCEL_OPTION);
jlblStatus.setText("X won! The game is over");
}
else if(isFull())
{
JOptionPane.showMessageDialog(this,"The game is over","提示",
JOptionPane.INFORMATION_MESSAGE);
jlblStatus.setText("Draw! The game is over");
}
}
else if(whoseTurn=='O')
{
setToken('O');
whoseTurn='X';
jlblStatus.setText("X's turn");
if(isWin('O'))
{
JOptionPane.showMessageDialog(this, "Y won! ","提示",
JOptionPane.INFORMATION_MESSAGE);
jlblStatus.setText("O won! The game is over");
}
else if(isFull())
{
JOptionPane.showMessageDialog(this,"The game is over","提示",
JOptionPane.INFORMATION_MESSAGE);
jlblStatus.setText("Draw! The game is over");
}
}
}
}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
}
}//一个简单JAVA #字三关 的游戏代码
全部回答
图形化得界面?直接贴上来你可用不了。得把整个工程给你你才能用。我这里还没有源码。你去网上搜一下,JAVA方面CS结构的源码就有。

import javax.swing.*; import java.awt.*; import javax.swing.border.lineborder; import java.awt.event.*;

public class tictactoe extends japplet { private char whoseturn='x'; private cell[][] cell=new cell[3][3]; private jlabel jlblstatus=new jlabel("x's turn to play"); public void init() { jpanel p=new jpanel(); p.setlayout(new gridlayout(3,3,0,0)); for(int i=0;i<3;i++) for(int j=0;j<3;j++) p.add(cell[i][j]=new cell());

p.setborder(new lineborder(color.red,1)); jlblstatus.setborder(new lineborder(color.yellow,1)); this.getcontentpane().add(p,borderlayout.center); this.getcontentpane().add(jlblstatus,borderlayout.south); } public void start() {} public static void main(string[] args) { jframe frame=new jframe("tictactoe"); tictactoe applet=new tictactoe(); frame.getcontentpane().add(applet,borderlayout.center); applet.init(); applet.start(); //center the window dimension screensize = toolkit.getdefaulttoolkit().getscreensize(); dimension framesize = frame.getsize(); if (framesize.height > screensize.height) { framesize.height = screensize.height; } if (framesize.width > screensize.width) { framesize.width = screensize.width; } frame.setlocation( (screensize.width - framesize.width) / 2, (screensize.height - framesize.height) / 2);

frame.setsize(300,300); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } public boolean isfull() { for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(cell[i][j].gettoken()==' ') return false; return true; } public boolean iswin(char token) { for(int i=0;i<3;i++) if((cell[i][0].gettoken()==token) &&(cell[i][1].gettoken()==token) &&(cell[i][2].gettoken()==token)) { return true; } for(int j=0;j<3;j++) if((cell[0][j].gettoken()==token) &&(cell[1][j].gettoken()==token) &&(cell[2][j].gettoken()==token)) { return true; } if((cell[0][0].gettoken()==token) &&(cell[1][1].gettoken()==token) &&(cell[2][2].gettoken()==token)) { return true; } if((cell[0][2].gettoken()==token) &&(cell[1][1].gettoken()==token) &&(cell[2][0].gettoken()==token)) { return true; } return false; } public class cell extends jpanel implements mouselistener { private char token=' '; public cell() { setborder(new lineborder(color.black,1)); addmouselistener(this); } public char gettoken() { return token; } public void settoken(char c) { token=c; repaint(); } public void paintcomponent(graphics g) { super.paintcomponent(g); if(token=='x') { g.drawline(10,10,getsize().width-10,getsize().height-10); g.drawline(getsize().width-10,10,10,getsize().height-10); } else if(token=='o') { g.drawoval(10,10,getsize().width-20,getsize().height-20); } } public void mouseclicked(mouseevent e) { if(token==' ') { if(whoseturn=='x') { settoken('x'); whoseturn='o'; jlblstatus.settext("o's turn"); if(iswin('x')) { joptionpane.showmessagedialog(this,"x won! ","提示", joptionpane.yes_no_cancel_option); jlblstatus.settext("x won! the game is over"); } else if(isfull()) { joptionpane.showmessagedialog(this,"the game is over","提示", joptionpane.information_message); jlblstatus.settext("draw! the game is over"); } } else if(whoseturn=='o') { settoken('o'); whoseturn='x'; jlblstatus.settext("x's turn"); if(iswin('o')) { joptionpane.showmessagedialog(this, "y won! ","提示", joptionpane.information_message); jlblstatus.settext("o won! the game is over"); } else if(isfull()) { joptionpane.showmessagedialog(this,"the game is over","提示", joptionpane.information_message); jlblstatus.settext("draw! the game is over"); } } } } public void mousepressed(mouseevent e) {} public void mousereleased(mouseevent e) {} public void mouseentered(mouseevent e) {} public void mouseexited(mouseevent e) {} }

}//一个简单java #字三关 的游戏代码

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
剑网3和服务器断开连接
渝C,001A7车牌号码如何
C语言,某if条件句,条件满足,却不执行,是
9.“Oh, Peter, you are running a high fev
心安定下来的句子,如何在忙碌的学习中让自己
直肠癌不到两年转移肺癌,半年转移到脑,还能
我想买只薮猫,不知道中国有没有卖,在哪里可以
有什么元素之最?如最硬的金属是硌等
被人给撸了,下面有点痛,怎么办?
房子出租后房客不肯搬走怎么办
一艘轮船由a港到b港顺流需3h
Mike gets up at seven .换成一般疑问句怎么
李峙东的画如何?有没有升值可能?
在换一个思路中的偏让我的意思
苏州有哪些果园可以让游客采摘
推荐资讯
猥亵女童最高判几年
哪个人睡了好多女明星
If the American government the expert’s s
请问潮州有没有规模点的茶叶批发市场?具体位
新福克斯夜间行车近光灯怎么开的?是不是那个
小米1手机相机故障
为什么有不少中老年人,老年人。活在21世纪的
在以后的朝代里,无论是盛世还是乱世,隐者的
解答题野生马铃薯品种的植株具有较强的抗病、
猜猜是一句话?
windows7好还是windows10好
4x-13-n=1-x-n那么x等于多少
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?