gpt4 book ai didi

java - 合并随机数游戏中按钮的 Action 监听器

转载 作者:行者123 更新时间:2023-12-01 11:36:28 24 4
gpt4 key购买 nike

我对 Java 比较陌生,我对我编写的游戏有疑问。这是一个随机数字游戏,您尝试以最少的猜测次数猜出 1 到 10 之间的一个数字。

我让程序可以运行,但代码看起来效率很低。目前,我为每个数字创建一个 JButton,然后为每个按钮创建一个单独的操作监听器,如下所示(为了简单起见,省略了很多内容,因为它与问题无关):

注意:int number = 程序选择的随机数,并且 intguessCounter = 跟踪猜测次数的数字

 public class random extends JFrame{

//Creating the Buttons

JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");

public static void main(String[] args){

//Creating Action Listeners

OneClass uno = new OneClass();
one.addActionListener(uno);

TwoClass dos = new TwoClass();
two.addActionListener(dos);

ThreeClass tres = new ThreeClass();
three.addActionListener(tres);

//Action Listener For Button 1

private class OneClass implements ActionListener{
public void actionPerformed(ActionEvent event){
if(number == 1){
guessCounter++;
JOptionPane.showMessageDialog(null,"Correct! You guessed the correct number in " + guessCounter + " guesses","Correct!", JOptionPane.PLAIN_MESSAGE);
}else if(number > 1){
guessCounter++;
JOptionPane.showMessageDialog(null,"Incorrect. Guess Higher. You have guessed " + guessCounter + " time(s).","Incorrect", JOptionPane.PLAIN_MESSAGE);
one.setEnabled(false);
}else if(number < 1){

}
}

}

//Action Listener For Button 2

private class TwoClass implements ActionListener{
public void actionPerformed(ActionEvent event){
if(number == 2){
guessCounter++;
JOptionPane.showMessageDialog(null,"Correct! You guessed the correct number in " + guessCounter + " guesses","Correct!", JOptionPane.PLAIN_MESSAGE);
}else if(number > 2){
guessCounter++;
JOptionPane.showMessageDialog(null,"Incorrect. Guess Higher. You have guessed " + guessCounter + " time(s).","Incorrect", JOptionPane.PLAIN_MESSAGE);
two.setEnabled(false);
}else if(number < 2){
guessCounter++;
JOptionPane.showMessageDialog(null, "Incorrect. Guess Lower. You have guessed " + guessCounter + " time(s)","Incorrect", JOptionPane.PLAIN_MESSAGE);
two.setEnabled(false);
}
}
}

//Action Listener For Button 3

private class ThreeClass implements ActionListener{
public void actionPerformed(ActionEvent event){
if(number == 3){
guessCounter++;
JOptionPane.showMessageDialog(null,"Correct! You guessed the correct number in " + guessCounter + " guesses","Correct!", JOptionPane.PLAIN_MESSAGE);
}else if(number > 3){
guessCounter++;
JOptionPane.showMessageDialog(null,"Incorrect. Guess Higher. You have guessed " + guessCounter + " time(s).","Incorrect", JOptionPane.PLAIN_MESSAGE);
three.setEnabled(false);
}else if(number < 3){
guessCounter++;
JOptionPane.showMessageDialog(null, "Incorrect. Guess Lower. You have guessed " + guessCounter + " time(s)","Incorrect", JOptionPane.PLAIN_MESSAGE);
three.setEnabled(false);
}
}

当每个 Action 监听器都这么长时,我们可以看到效率低下的情况如此普遍。 我的问题是,是否可以压缩它(即使每个按钮的操作监听器都要求将随机数与不同的数字进行比较)?这已经很长了,并且会变得非常长一旦我开始添加更困难的关卡和更多可供选择的数字。

最佳答案

是的,您可以编写一个由所有按钮共享的监听器。然后,您可以获得监听器的“操作命令”,这将是按钮的文本。然后,您可以使用 Integer.parseInt(...) 方法将 String 转换为实际数字。然后你可以调用你的一般逻辑。

以下是模拟计算器按键的共享监听器的示例:

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

public class CalculatorPanel extends JPanel
{
private JTextField display;

public CalculatorPanel()
{
Action numberAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
// display.setCaretPosition( display.getDocument().getLength() );
display.replaceSelection(e.getActionCommand());
}
};

setLayout( new BorderLayout() );

display = new JTextField();
display.setEditable( false );
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout(0, 5) );
add(buttonPanel, BorderLayout.CENTER);

for (int i = 0; i < 10; i++)
{
String text = String.valueOf(i);
JButton button = new JButton( text );
button.addActionListener( numberAction );
button.setBorder( new LineBorder(Color.BLACK) );
button.setPreferredSize( new Dimension(50, 50) );
buttonPanel.add( button );

InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke(text), text);
inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
button.getActionMap().put(text, numberAction);
}
}

private static void createAndShowUI()
{
// UIManager.put("Button.margin", new Insets(10, 10, 10, 10) );

JFrame frame = new JFrame("Calculator Panel");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( new CalculatorPanel() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

关于java - 合并随机数游戏中按钮的 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29932112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com