我在宣战下的 if 语句不起作用,我问了 chatgpt 15 次,他的所有答案都一样没用,代码没有错误,但是当我键入 3 然后 m 它不起作用时,我尝试编辑代码以多种方式,但它与民主框架下的 if 语句也不起作用
我正在使用 jframe 在 eclipse 中制作应用程序并且我有一个文本字段我正在使用动作侦听器检查文本字段中的某个输入但我不知道如何让动作侦听器检查是否if 语句中的语句
package hi;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class hdfb {
static int points = 100;
static int yearsleft = 8;
static int monthsleft = 0;
static int badthinsdone = 4;
static int hateofpeople = badthinsdone / 2;
static int minuspoints = points / hateofpeople;
static JFrame frame = new JFrame();
JPanel title_panel = new JPanel();
JPanel button_panel = new JPanel();
JLabel textfield = new JLabel();
JButton[] buttons = new JButton[9];
private static JTextField textField;
/**
* @wbp.parser.entryPoint
*/
public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
frame.getContentPane().setBackground(Color.WHITE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\HASSAN\\Pictures\\download.jpg"));
lblNewLabel.setBounds(418, 63, 344, 303);
frame.getContentPane().add(lblNewLabel);
JTextField textField = new JTextField();
textField.setBounds(0, 0, 784, 20);
textField.setForeground(new Color(0, 0, 0));
textField.setBackground(new Color(255, 255, 255));
frame.getContentPane().add(textField);
textField.setColumns(1);
JTextPane textPane = new JTextPane();
textPane.setBounds(0, 20, 784, 714);
frame.getContentPane().add(textPane);
frame.setVisible(true);
textPane.setText("You are Andrew jackson the 7th president. You have " + points + " presidential points. Don't make dumb choices. \n\nIt is March 4, 1829, your first day of presidency. What do you want to do? \n \n1. Lay the framework of democracy\n2. Pay off the national debt over 1 year\n3. Declare war\n4. Skip 2 months\n5. Make a new law or veto an already existing one\n6. Kick the natives out\n");
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (textField.getText().contains("1")){
points += 10;
textPane.setText("The people support you now even more +10 presidential points. You Now have " + points + " presidential points.\nWhat do you want to do?\n1. Go back\n2. Duel someone\n3. Skip 1 year\n4. Make yourself king of the constitution by doing everything you want.");
String input = textField.getText(); // get user input
if (input.equals("1")){
System.out.println("hi");
} else if (input.equals("2")){
textPane.setText("Who do you want to duel?");
} else if (input.equals("3")){
yearsleft--;
textPane.setText("You skipped 1 year. Now you have " + yearsleft + " years left and " + monthsleft + " months left.");
} else if (input.equals("4")){
textPane.setText("You are now the king of the constitution. Congratulations!");
}
} else if (textField.getText().contains("2")){
points -= 5;
textPane.setText("You had to raise taxes so the people got a little mad -5 presidential points. But it's fine, you're doing it for the good of the people. It will help in a year. You now have " + points + " presidential points.");
} else if (textField.getText().contains("3")) {
textPane.setText("What country do you want to declare war on? Here are some options, but remember you can pick any North or European country. \n1. Mexico\n2. Britain\n3. Spain\n4. France");
String input = textField.getText(); // get user input
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (textField.getText().contains("1")){
points += 10;
textPane.setText("The people support you now even more +10 presidential points. You Now have " + points + " presidential points.\nWhat do you want to do?\n1. Go back\n2. Duel someone\n3. Skip 1 year\n4. Make yourself king of the constitution by doing everything you want.");
String input = textField.getText(); // get user input
if (input.equals("1")){
System.out.println("hi");
} else if (input.equals("2")){
textPane.setText("Who do you want to duel?");
} else if (input.equals("3")){
yearsleft--;
textPane.setText("You skipped 1 year. Now you have " + yearsleft + " years left and " + monthsleft + " months left.");
} else if (input.equals("4")){
textPane.setText("You are now the king of the constitution. Congratulations!");
}
} else if (textField.getText().contains("2")){
points -= 5;
textPane.setText("You had to raise taxes so the people got a little mad -5 presidential points. But it's fine, you're doing it for the good of the people. It will help in a year. You now have " + points + " presidential points.");
} else if (textField.getText().contains("3")) {
textPane.setText("What country do you want to declare war on? Here are some options, but remember you can pick any North or European country. \n1. Mexico\n2. Britain\n3. Spain\n4. France");
String input = textField.getText(); // get user input
if (input.contains("m")) {
points -= 20;
textPane.setText("You declared war on Mexico. The people don't support that -20 presidential points. You now have " + points + " presidential points.");
} else if (input.contains("b")) {
points -= 30;
textPane.setText("You declared war on Britain. The people don't support that -30 presidential points. You now have " + points + " presidential points.");
} else if (input.contains("s")) {
points -= 25;
textPane.setText("You declared war on Spain. The people don't support that -25 presidential points. You now have " + points + " presidential points.");
} else if (input.contains("f")) {
points -= 35;
textPane.setText("You declared war on France. The people don't support that -35 presidential points. You now have " + points + " presidential points.");
} else if (input.contains("4")|| input.toLowerCase().contains("skip")) {
textPane.setText("Bad idea, but whatever. It is your second month as president! What is your next choice?");
}
}
}
});
}
}
});
}
}
addActionListener
函数不运行ActionListener#actionPerformed
,它只是向处理程序列表添加一个事件处理程序。当一个事件发生时,所有的事件监听器都会被一一执行。您可以附加调试器并自行检查。尝试在以下行设置断点
textPane.setText("What country do you want to declare war on? Here are some options, but remember you can pick any North or European country. \n1. Mexico\n2. Britain\n3. Spain\n4. France");
并在每次操作后检查
textField.getActionListeners()
的输出。
当字符串
s.contains(x)
有true
的子序列时,s
函数返回x
。
"abcd".contains("c") -> true
"abcd".contains("a") -> true
您的代码中的问题是您添加了太多的侦听器,并且它们会擦除彼此的结果。
想象一下,我想对西班牙宣战,那么流程就是
textPane.setText("You are Andrew jackson the 7th president. You have " + points + " presidential points. Don't make dumb choices. \n\nIt is March 4, 1829, your first day of presidency. What do you want to do? \n \n1. Lay the framework of democracy\n2. Pay off the national debt over 1 year\n3. Declare war\n4. Skip 2 months\n5. Make a new law or veto an already existing one\n6. Kick the natives out\n");
else if (textField.getText().contains("3")) { textPane.setText("What country do you want to declare war on? Here are some options, but remember you can pick any North or European country. \n1. Mexico\n2. Britain\n3. Spain\n4. France");
else if (input.contains("s")) {points -= 25; textPane.setText("You declared war on Spain. The people don't support that -25 presidential points. You now have " + points + " presidential points.");
What country do you want to declare war on? Here are some options, but remember you can pick any North or European country. \n1. Mexico\n2. Britain\n3. Spain\n4. France
您可以验证
points
变量实际上正在改变,这意味着您的代码运行,但消息被覆盖。
我建议您只创建一个
ActionListener
来处理所有情况。您可以引入一个新变量来跟踪当前状态,因此您将需要更少的嵌套if
s。您可能想使用 startsWith
而不是 contains
以避免歧义。