对于我的学校项目,我需要创建一个银行模拟器。所以作为第一步,我应该在键盘上点击按钮时显示屏幕键盘,该值应该转到文本框。我完全是空白。我试过我可以创建键盘。它在一个不同的frame.so我不知道如何在点击按钮时从它检索数据。请帮助人。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class BankSimulator extends JFrame implements ActionListener {
JTextField username = new JTextField(10);
JTextField password = new JTextField(10);
JTextField name = new JTextField(10);
JTextField setPassword = new JTextField(5);
JLabel userlbl = new JLabel("Username");
JLabel passlbl = new JLabel("Password");
JRadioButton option1 = new JRadioButton("Basic Account");
JRadioButton option2 = new JRadioButton("Saver Account");
JRadioButton option3 = new JRadioButton("Super Account");
JButton logbtn = new JButton("Log In");
JButton regbtn = new JButton("Register");
JButton showkeyboard = new JButton("showkeyboard");
JButton createbtn = new JButton("Create Account");
JButton[] btn1 = new JButton[10];
JButton[] btn2 = new JButton[10];
JButton[] btn3 = new JButton[9];
JButton[] btn4 = new JButton[7];
public static void main(String[] args) {
BankSimulator bm = new BankSimulator();
}
public BankSimulator() {
setLayout(null);
setSize(850, 700);
setTitle("Bank Simulator");
username.setBounds(350, 150, 130, 20);
add(username);
password.setBounds(350, 180, 130, 20);
add(password);
option1.setBounds(440, 180, 130, 20);
option2.setBounds(440, 210, 130, 20);
option3.setBounds(440, 240, 130, 20);
add(option1);
add(option2);
add(option3);
option1.setVisible(false);
option2.setVisible(false);
option3.setVisible(false);
name.setBounds(440, 150, 130, 20);
add(name);
name.setVisible(false);
setPassword.setBounds(440, 123, 130, 20);
add(setPassword);
setPassword.setVisible(false);
userlbl.setBounds(288, 150, 130, 20);
add(userlbl);
passlbl.setBounds(288, 180, 130, 20);
add(passlbl);
logbtn.setBounds(341, 220, 150, 30);
add(logbtn);
createbtn.setBounds(435, 270, 150, 30);
add(createbtn);
createbtn.setVisible(false);
logbtn.addActionListener(this);
regbtn.setBounds(341, 260, 150, 30);
add(regbtn);
regbtn.addActionListener(this);
showkeyboard.setBounds(341, 270, 150, 30);
add(showkeyboard);
showkeyboard.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showkeyboard();
}
});
setVisible(true);
}
public void showkeyboard() {
String nos = "1234567890";
String alphabet = "abcdefghij";
String alphabet1 = "klmnopqrs";
String alphabet2 = "tuvwxyz";
JFrame myFrame = new JFrame();
myFrame.setSize(586, 205);
JPanel myPanel = new JPanel();
JPanel myPanel1 = new JPanel();
JPanel myPanel2 = new JPanel();
JPanel myPanel3 = new JPanel();
for (int i = 0; i < nos.length(); i++) {
btn1[i] = new JButton(nos.substring(i, i + 1));
myPanel.add(btn1[i]);
btn1[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
keyboardButtonPressed();
}
});
for (int i = 0; i < alphabet.length(); i++) {
btn2[i] = new JButton(alphabet.substring(i, i + 1));
myPanel1.add(btn2[i]);
}
for (int i = 0; i < alphabet1.length(); i++) {
btn3[i] = new JButton(alphabet1.substring(i, i + 1));
myPanel2.add(btn3[i]);
}
for (int i = 0; i < alphabet2.length(); i++) {
btn4[i] = new JButton(alphabet2.substring(i, i + 1));
myPanel3.add(btn4[i]);
}
JPanel outer = new JPanel();
outer.add(myPanel);
outer.add(myPanel1);
outer.add(myPanel2);
outer.add(myPanel3);
myFrame.add(outer);
myFrame.setVisible(true);
}
}
public String keyboardButtonPressed(char a) {
String alreadyDisplayed = username.getText();
String toDisplay = alreadyDisplayed + btn1[i].getText();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == regbtn) {
username.setVisible(false);
password.setVisible(false);
logbtn.setVisible(false);
userlbl.setVisible(false);
passlbl.setVisible(false);
option1.setVisible(true);
option2.setVisible(true);
option3.setVisible(true);
name.setVisible(true);
setPassword.setVisible(true);
regbtn.setVisible(false);
createbtn.setVisible(true);
}
}
}
btn [i] .getText()无效,说'i'无法解析为变量。
答案 0 :(得分:1)
所以,你的基本问题是上下文......
for (int i = 0; i < nos.length(); i++) {
//...
for (int i = 0; i < alphabet.length(); i++) {
//...
}
for (int i = 0; i < alphabet1.length(); i++) {
//...
}
for (int i = 0; i < alphabet2.length(); i++) {
//...
}
基本上,i
已在外部循环中定义,因此您无法重复使用