我想创建一个方法,我可以简单地从Swing Form中的文本字段中获取文本。但是试图通过我的一个if语句来识别它。最终会出现一个错误,告诉我它尽管被定义了,但仍无法找到符号。我也想创建更清晰的代码,因为我这样做的方式似乎相当重复和混乱;有没有办法缩短重复次数并将它们称为变量?
public class TextGrabber {
int [] grabText = {Integer.parseInt(DigitValue.getText())};
}
int digit1 = TextGrabber;
if (digit1 == 1) {
Digi1.setVisible(false);
Digi2.setVisible(false);
Digi3.setVisible(true);
Digi4.setVisible(false); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(false);
}
int digit2 = Integer.parseInt(DigitValue.getText());
if (digit2 == 2) {
Digi1.setVisible(true);
Digi2.setVisible(false);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(true);
Digi6.setVisible(false);
Digi7.setVisible(true);
}
int digit3 = Integer.parseInt(DigitValue.getText());
if (digit3 == 3) {
Digi1.setVisible(true);
Digi2.setVisible(false);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(true);
}
int digit4 = Integer.parseInt(DigitValue.getText());
if (digit4 == 4) {
Digi1.setVisible(false);
Digi2.setVisible(true);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(false);
}
int digit5 = Integer.parseInt(DigitValue.getText());
if (digit5 == 5) {
Digi1.setVisible(true);
Digi2.setVisible(true);
Digi3.setVisible(false);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(true);
}
int digit6 = Integer.parseInt(DigitValue.getText());
if (digit6 == 6) {
Digi1.setVisible(true);
Digi2.setVisible(true);
Digi3.setVisible(false);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(true);
Digi6.setVisible(true);
Digi7.setVisible(true);
}
int digit7 = Integer.parseInt(DigitValue.getText());
if (digit7 == 7) {
Digi1.setVisible(true);
Digi2.setVisible(false);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(false);
}
int digit8 = Integer.parseInt(DigitValue.getText());
if (digit8 == 8) {
Digi1.setVisible(true);
Digi2.setVisible(true);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(true);
Digi6.setVisible(true);
Digi7.setVisible(true);
}
int digit9 = Integer.parseInt(DigitValue.getText());
if (digit9 == 9) {
Digi1.setVisible(true);
Digi2.setVisible(true);
Digi3.setVisible(true);
Digi4.setVisible(true); //This textfield will be hidden
Digi5.setVisible(false);
Digi6.setVisible(true);
Digi7.setVisible(true);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int AddOneDigit = Integer.parseInt(DigitValue.getText());
AddOneDigit = AddOneDigit + 1;
String NewSet = Integer.toString (AddOneDigit);
DigitValue.setText(NewSet);
ShowDigitActionPerformed(evt);
int check = Integer.parseInt(DigitValue.getText());
if (check >= 10) {
check = 0;
String CheckS = Integer.toString (check);
DigitValue.setText(CheckS);
Digi1.setVisible(true);
Digi2.setVisible(true);
Digi3.setVisible(true);
Digi4.setVisible(false); //This textfield will be hidden
Digi5.setVisible(true);
Digi6.setVisible(true);
Digi7.setVisible(true);
}
答案 0 :(得分:1)
将蒙版存储为数组,然后使用类似
的内容构建它int digit = Integer.parseInt(DigitValue.getText());
boolean[][] mask = { { false, false, true, false, false, true, false },
{ true, false, true, true, true, false, true },
{ true, false, true, true, false, true, true },
{ false, true, true, true, false, true, false },
{ true, true, false, true, false, true, true },
{ true, true, false, true, true, true, true },
{ true, false, true, true, false, true, false },
{ true, true, true, true, true, true, true },
{ true, true, true, true, false, true, true } };
boolean[] digits = mask[digit - 1];
Digi1.setVisible(digits[0]);
Digi2.setVisible(digits[1]);
Digi3.setVisible(digits[2]);
Digi4.setVisible(digits[3]);
Digi5.setVisible(digits[4]);
Digi6.setVisible(digits[5]);
Digi7.setVisible(digits[6]);
我还注意到您没有数字'0'
的映射。
答案 1 :(得分:0)
我不确定这是否会对您有所帮助,但您可以通过创建方法来清理一些代码。您可以创建一个方法来声明' Digi 的可见性。像这样的方法可能看起来像下面的代码。
代码:
public void multipleVisibleSet(boolean 1Visible, boolean 2Visible, boolean 3Visible,
boolean 4Visible, boolean 5Visible, boolean 6Visible, boolean 7Visible){
Digi1.setVisible(1Visible); //Add as many as you need
Digi2.setVisible(2Visible);
Digi3.setVisible(3Visible);
Digi4.setVisible(4Visible);
Digi5.setVisible(5Visible);
Digi6.setVisible(6Visible);
Digi7.setVisible(7isible);
}