几分钟前我发布了一个问题,有人要求我发布一个“SSCCE”来证明我的问题。我已经大大压缩了我的程序,所以如果一些构造没有意义或者看起来比新手的工作效率更低,那应该只是那个操作的产物。
现在,我的程序主要用于将一组俄语动词变位复制到一个集中的数组oneRay中。在此处的设置中,一个提示显示在一个空行上方,在该空行中,一个是键入适当的共轭。按“提交”按钮应该检查答案与数组的答案,但由于我自己的错误或缺乏理解,即使直接复制我知道的正确答案,我也会看到消息“失败”。通过这一行,我得到了输入:
build1 = new StringBuilder(ssfield1.getText());
有了这个我检查它的元素:
if(build1.equals(oneRay[pick][1].toLowerCase().replaceAll("\\s","")))ssfield1.setText("CORRECT");
else{ssfield1.setText("FAIL");}
我觉得这可能是一个简单的问题,即针对错误的输入文本字段检查错误的元素,但它可能不是,所以这里是我精简的“SSCCE”的所有代码:
我希望不会太久,而且肯定是独立的(而且也很简单):让我知道您是否需要其他任何东西,或者这些问题是否为时已晚!
package wbh;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SwingImplementation_RUS extends JFrame{
String [] [] oneRay;Random random = new Random();
JPanel panel; JTextField field,sfield1,sfield2,ssfield1,ssfield2; JButton buton;
int pick, hold;StringBuilder build1,build2;
public SwingImplementation_RUS(final int subj){
super("To be updated");
final String RUS_1[][]={
{"знать","знаю","знаешь","знает","знает","знает","знаем","знаете","знают",}};
new Thread(new Runnable() {
public void run() {
buton = new JButton("Submit");
setLayout(new GridLayout(3,1,3,3));
panel = new JPanel();panel.setLayout(new GridLayout(5,2,3,3));
field = new JTextField();
sfield1 = new JTextField("Я");sfield2 = new JTextField("Ты");
ssfield1 = new JTextField("");ssfield2 = new JTextField("");
buton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent m) {
System.out.println("CHECKED");
build1 = new StringBuilder(ssfield1.getText());
build2 = new StringBuilder(ssfield2.getText());
called2();}});
Font f = new Font("Arial", Font.BOLD, 75);
field.setFont(f);
add(field);
add(panel);
panel.add(sfield1);panel.add(ssfield1);
panel.add(sfield2);panel.add(ssfield2);
add(buton);
setLocation(500,0);setSize(865, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);
}}).start();
oneRay = new String[RUS_1.length][9];
for(int i = 0; (RUS_1.length) > i; i++){
oneRay[i][0] = RUS_1[i][0];oneRay[i][1] = RUS_1[i][1];
oneRay[i][2] = RUS_1[i][2];}
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
hold = oneRay.length;pick = random.nextInt(hold);
field.setText(" "+oneRay[pick][0]);
}
private void called2(){
if(build1.equals(oneRay[pick][1].toLowerCase().replaceAll("\\s","")))ssfield1.setText("CORRECT");
else{ssfield1.setText("FAIL");}
if(build2.equals(oneRay[pick][2].toLowerCase().replaceAll("\\s","")))ssfield2.setText("CORRECT");
else{ssfield2.setText("FAIL");}
}
public static void main (String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new SwingImplementation_RUS(1);
}
});
}
}