如何从actionlistener保存输入,然后在Java中与另一个进行比较?(How to save input from actionlistener, and then compare to another in Java?)
我正在制作一个摇滚纸剪刀游戏,其中2个玩家必须选择是否要使用摇滚,纸张或剪刀。 然后我的程序被用来比较选择。 但是我将选择保存为变量时遇到了问题。 我可以打印出选项,但我不知道如何在actionlisteneres之外使用答案。
所以这是我的代码(有些名字是挪威语,所以可能有点令人困惑,但希望你能理解):
package oblig8; import java.awt.BorderLayout; import java.ActionEvent; import java.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; @SuppressWarnings("serial") public class Oblig8 extends JFrame{ public static void main(String[] args) { // TODO Auto-generated method stub JFrame Frame = new JFrame("Stein - Saks - Papir"); Frame.setDefaultCloseOperation(JFrame.EXIT_O_CLOSE); Frame.setSize(500, 500); Frame.setVisible(true); JButton Spiller1 = new JButton("Spiller 1"); JButton Spiller2 = new JButton("Spiller 2"); Frame.getContentPane().add(Spiller1, BorderLayout.WEST); Frame.getContentPane().add(Spiller2, BorderLayout.EAST); String[] muligheter = new String[]; muligheter[0]="Stein"; muligheter[1]="Saks"; muligheter[2]="Papir"; Spiller1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { int valg1 = JOptionPane.showOptionDialog(Frame, "Spiller 1 har trykket", "Stein - Saks - Papir", JOptionPane.YES_O_CACEL_OPTIO, JOptionPane.QUESTIO_MESSAGE, null, muligheter, muligheter[2]); println(valg1);}}); Spiller2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { int valg2 = JOptionPane.showOptionDialog(Frame, "Spiller 2 har trykket", "Stein - Saks - Papir", JOptionPane.YES_O_CACEL_OPTIO, JOptionPane.QUESTIO_MESSAGE, null, muligheter, muligheter[2]); println(valg2);}}); }}I am making a rock-paper-scissors game, where 2 players have to choose if they are going to use rock, paper or scissors. And then my program is suposed to compare the the choices. But I'm having problems saving the choices as a variable. I can print out the the choices, but I don't know how I can use the answers outside the actionlisteneres.
So this is the code I have(some of the names are in orwegian, so might be a bit confusing, but hopefully you understand):
package oblig8; import java.awt.BorderLayout; import java.ActionEvent; import java.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; @SuppressWarnings("serial") public class Oblig8 extends JFrame{ public static void main(String[] args) { // TODO Auto-generated method stub JFrame Frame = new JFrame("Stein - Saks - Papir"); Frame.setDefaultCloseOperation(JFrame.EXIT_O_CLOSE); Frame.setSize(500, 500); Frame.setVisible(true); JButton Spiller1 = new JButton("Spiller 1"); JButton Spiller2 = new JButton("Spiller 2"); Frame.getContentPane().add(Spiller1, BorderLayout.WEST); Frame.getContentPane().add(Spiller2, BorderLayout.EAST); String[] muligheter = new String[]; muligheter[0]="Stein"; muligheter[1]="Saks"; muligheter[2]="Papir"; Spiller1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { int valg1 = JOptionPane.showOptionDialog(Frame, "Spiller 1 har trykket", "Stein - Saks - Papir", JOptionPane.YES_O_CACEL_OPTIO, JOptionPane.QUESTIO_MESSAGE, null, muligheter, muligheter[2]); println(valg1);}}); Spiller2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { int valg2 = JOptionPane.showOptionDialog(Frame, "Spiller 2 har trykket", "Stein - Saks - Papir", JOptionPane.YES_O_CACEL_OPTIO, JOptionPane.QUESTIO_MESSAGE, null, muligheter, muligheter[2]); println(valg2);}}); }}最满意答案
您可以在类范围中声明静态int变量,并将答案存储在这些变量中,然后根据需要使用它
public class Oblig8 extends JFrame { static int valg1; static int valg2; public static void main(String[] args) { ... Spiller1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { valg1 = JOptionPane.showOptionDialog(Frame, "Spiller 1 har trykket", "Stein - Saks - Papir", JOptionPane.YES_O_CACEL_OPTIO, JOptionPane.QUESTIO_MESSAGE, null, muligheter, muligheter[2]); println(valg1); } }); ... } }You can declare static int variables in the class scope, and store the answer in these variable, then used it as you wish
public class Oblig8 extends JFrame { static int valg1; static int valg2; public static void main(String[] args) { ... Spiller1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { valg1 = JOptionPane.showOptionDialog(Frame, "Spiller 1 har trykket", "Stein - Saks - Papir", JOptionPane.YES_O_CACEL_OPTIO, JOptionPane.QUESTIO_MESSAGE, null, muligheter, muligheter[2]); println(valg1); } }); ... } }#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上一篇:奇怪的实体框架错误:用户登录失败(Weird Entity Framework error: login failed for user)
下一篇:在使用data :: dumper PERL打印时,不会遵守口音(Accents not respected in printing out with data::dumper PERL)
推荐阅读
留言与评论(共有 11 条评论) |
本站网友 莫比怎么抓 | 3分钟前 发表 |
and then compare to another in Java?) 我正在制作一个摇滚纸剪刀游戏 | |
本站网友 mobook | 11分钟前 发表 |
但是我将选择保存为变量时遇到了问题 | |
本站网友 武汉新东方 | 24分钟前 发表 |
JOptionPane.QUESTIO_MESSAGE | |
本站网友 糕点培训学校 | 20分钟前 发表 |
"Stein - Saks - Papir" | |
本站网友 如何增强记忆力 | 12分钟前 发表 |
muligheter[2]); println(valg1);}}); Spiller2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { int valg2 = JOptionPane.showOptionDialog(Frame | |
本站网友 世界十大汽车 | 22分钟前 发表 |
null | |
本站网友 否认是什么意思 | 18分钟前 发表 |
JOptionPane.QUESTIO_MESSAGE | |
本站网友 客语 | 12分钟前 发表 |
"Spiller 1 har trykket" | |
本站网友 染头发的危害 | 18分钟前 发表 |
纸张或剪刀 | |
本站网友 哈尔滨肯德基 | 6分钟前 发表 |
BorderLayout.WEST); Frame.getContentPane().add(Spiller2 |