我有以下内容SSCCE
,我无法重新调用currentComponents.get(status)
拥有 .setText() 方法。如何使用 Map 方法设置文本?
按下按钮 a 后,文本字段仍为空
测试.java:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import layout.SpringUtilities;
public class Test {
private static String status = null;
private static Map<String, JTextField> currentComponents = new HashMap<String, JTextField>();
private static JPanel dialog;
public static void main(String args[]){
JFrame frame = new JFrame();
final JLabel label = new JLabel();
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DialogLoginsetup();
frame.add(dialog);
frame.pack();
frame.setVisible(true);
}
/* Dialog part 1 */
private static void DialogLoginsetup() {
JPanel configPanel = new JPanel();
configPanel.setLayout(new GridLayout(5, 2, 2, 2));
try {
Loginsetup(configPanel);
} catch (IOException io) {
io.printStackTrace();
}
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
topPanel.add(new JLabel("Setup: "));
dialog = new JPanel();
dialog.setLayout(new BorderLayout());
dialog.add(topPanel, BorderLayout.NORTH);
dialog.add(new JPanel(), BorderLayout.SOUTH);
dialog.add(configPanel, BorderLayout.CENTER);
}
/* Dialog part 2 */
private static JTextField serverTextField;
public static void Loginsetup(JPanel configPanel) throws IOException {
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new SpringLayout());
currentComponents.put("serverTextField", new JTextField());
// server
JLabel serverLabel = new JLabel("Username: ");
serverTextField= new JTextField();
serverLabel.setLabelFor(serverTextField);
loginPanel.add(serverLabel, BorderLayout.WEST);
loginPanel.add(serverTextField, BorderLayout.CENTER);
serverTextField.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent fe) {
status = "serverTextField";
}
public void focusLost(FocusEvent fe) {
}
});
//Layout the panel.
//SpringUtilities.makeCompactGrid(loginPanel,
//1, 2, //rows, cols
//6, 6, //initX, initY
//6, 6);
configPanel.setLayout(new GridLayout(0,1));
configPanel.add(loginPanel);
configPanel.add(loadAZ());
}
/* Dialog part 3 */
public static JPanel loadAZ() {
JPanel az = new JPanel();
az.setBorder(BorderFactory.createEmptyBorder(11, 11, 11, 11));
az.setLayout(new GridLayout(0,12));
String[] alphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j" , "k" , "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "-", "_"
};
JButton[] ka = new JButton[28];
for (int i = 0 ; i < 28; i++) {
ka[i] = new JButton(alphabet[i]);
ka[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String one = e.getActionCommand();
System.out.println("Something is pressed, which should be added to: " + status);
if (one.equals("a")) {
//currentComponents.get(status).setText();
JComponent component = currentComponents.get(status);
((JTextComponent) component).setText("works"); // Did not worked
} else if(one.equals("b")) {
} else {
}
}
});
az.add(ka[i]);
}
return az;
}
}
跟进:
尝试1:[失败]
private static Map<String, JTextField> currentComponents = new HashMap<String, JTextField>();
currentComponents.put("serverTextField", new JTextField());
JTextField goal = currentComponents.get("serverTextField");
goal.setText("1");
尝试 2:[失败]
JTextField goal = (JTextField) (JTextComponent) (Object) "serverTextField";
goal.setText("1");
尝试 3:[失败]
JTextField goal = new JTextField();
goal.setName("serverTextField");
goal.setText("1");
尝试 4:[确定]
private static List<JTextField> list = new ArrayList<JTextField>();
list.add(serverTextField); // #define JTextField serverTextField = new JTextField();
JTextField goal = list.get(0);
goal.setText("1"); // Works