我试图在 NetBeans 上制作一个简单的两人井字游戏。
我创建了 6 个文本字段、一个“通过”按钮、一个“新游戏”按钮和一个退出按钮。
“通过”按钮具有代码中的所有功能。代替游戏中的 X 和 O,我使用任意两个整数,例如 1 代表 X,2 代表 O。
这是我的“通过”按钮代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a,b,c,d,e,f,g,h,i;
a=Integer.parseInt((jTextField1.getText()).trim());
b=Integer.parseInt((jTextField2.getText()).trim());
c=Integer.parseInt((jTextField3.getText()).trim());
d=Integer.parseInt((jTextField4.getText()).trim());
e=Integer.parseInt((jTextField5.getText()).trim());
f=Integer.parseInt((jTextField6.getText()).trim());
g=Integer.parseInt((jTextField7.getText()).trim());
h=Integer.parseInt((jTextField8.getText()).trim());
i=Integer.parseInt((jTextField9.getText()).trim());
if
(
// if the grid is empty
((((jTextField1.getText()).trim()).isEmpty())== true)
&& ((((jTextField2.getText()).trim()).isEmpty())== true)
&& ((((jTextField3.getText()).trim()).isEmpty())== true)
&& ((((jTextField4.getText()).trim()).isEmpty())== true)
&& ((((jTextField5.getText()).trim()).isEmpty())== true)
&& ((((jTextField6.getText()).trim()).isEmpty())== true)
&& ((((jTextField7.getText()).trim()).isEmpty())== true)
&& ((((jTextField8.getText()).trim()).isEmpty())== true)
&& ((((jTextField9.getText()).trim()).isEmpty())== true)
)
{
jLabel1.setText("Let's Begin!");
}
else if // if it isn't empty
( //if partially full
((((jTextField1.getText()).trim()).isEmpty())== true)
|| ((((jTextField2.getText()).trim()).isEmpty())== true)
|| ((((jTextField3.getText()).trim()).isEmpty())== true)
|| ((((jTextField4.getText()).trim()).isEmpty())== true)
|| ((((jTextField5.getText()).trim()).isEmpty())== true)
|| ((((jTextField6.getText()).trim()).isEmpty())== true)
|| ((((jTextField7.getText()).trim()).isEmpty())== true)
|| ((((jTextField8.getText()).trim()).isEmpty())== true)
|| ((((jTextField9.getText()).trim()).isEmpty())== true)
)
{
if
(
((a==b)&&(b==c))
||((d==e)&&(e==f))
||((g==h)&&(h==i))
||((a==d)&&(d==g))
||((b==e)&&(d==h))
||((c==f)&&(f==i))
||((a==e)&&(e==f))
||((c==e)&&(e==g))
)
{
jLabel1.setText("We have a winner!");
}
else
{
jLabel1.setText("Okay, Next Move!");
}
}
else if
//it is completely full
(
((((jTextField1.getText()).trim()).isEmpty())== false)
&& ((((jTextField2.getText()).trim()).isEmpty())== false)
&& ((((jTextField3.getText()).trim()).isEmpty())== false)
&& ((((jTextField4.getText()).trim()).isEmpty())== false)
&& ((((jTextField5.getText()).trim()).isEmpty())== false)
&& ((((jTextField6.getText()).trim()).isEmpty())== false)
&& ((((jTextField7.getText()).trim()).isEmpty())== false)
&& ((((jTextField8.getText()).trim()).isEmpty())== false)
&& ((((jTextField9.getText()).trim()).isEmpty())== false)
)
{
if
(
((a==b)&&(b==c))
||((d==e)&&(e==f))
||((g==h)&&(h==i))
||((a==d)&&(d==g))
||((b==e)&&(d==h))
||((c==f)&&(f==i))
||((a==e)&&(e==f))
||((c==e)&&(e==g))
)
{
jLabel1.setText("We have a winner!");
}
else
{
jLabel1.setText("Nobody wins. :(");
}
}
}
如果你觉得这很愚蠢,请告诉我!我只是个业余爱好者。
这是我得到的错误,
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at projet.pys.TEST.jButton1ActionPerformed(TEST.java:181)
at projet.pys.TEST.access$000(TEST.java:11)
at projet.pys.TEST$1.actionPerformed(TEST.java:50)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
如果你们完成上述所有内容并帮助我,我将不胜感激。