2

我有以下内容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
4

3 回答 3

1

((JTextComponent) currentComponents.get(...)).setText(...)

于 2012-04-23T16:45:03.403 回答
1

显而易见的答案是使用演员表,我会说:摆脱地图。只需将文本字段传递给您的ActionListener(作为参数,或使用最终变量,...)并删除该映射。由于您的侦听器只更新一个组件,因此请使用 instanceof 和静态映射将其传递给侦听器 iso。

此外,您的

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();
          } else if(one.equals("b")) {
          } else {            
          }
        }
      });

代码有一些问题

  1. 我没有setActionCommand在您的按钮上看到对的调用,所以我担心getActionCommand不会返回预期的结果
  2. 如果要使用按钮的文本更新字段中的文本,则不需要巨大的构造。将按钮的文本设置为操作命令,并将该文本附加到字段中。比编写 26 个 if-else 语句要容易得多
于 2012-04-23T16:51:54.810 回答
0

您可以声明:

private static Map<String, JTextField> currentComponents 
    = new HashMap<String, JTextField>();

既然JComponent没有办法setText(...)。在您的示例中,您仅JTextField在此地图中使用。如果您打算将其他组件也放入此地图,请按照其他人的建议使用演员表。

于 2012-04-23T16:42:56.080 回答