2

私は .setText()メソッドを持つためにSSCCE再呼び出しできないところにこれを持っています。currentComponents.get(status)Map メソッドを使用してテキストを設定するにはどうすればよいですか?

ボタンを押した後、テキストフィールドはまだ空です

ここに画像の説明を入力

テスト.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 を試す: [ OK ]

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

明らかな答えがキャストを使用することである場合、私はこう言います: マップを取り除きます。テキストフィールドをActionListener(パラメーターとして、または最終変数を使用して...)に渡し、そのマップを削除するだけです。リスナーは 1 つのコンポーネントのみを更新するため、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 に答える
1

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

于 2012-04-23T16:45:03.403 に答える
0

あなたは宣言することができます:

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

JComponentメソッドがないのでsetText(...)。あなたの例ではJTextField、このマップでのみ使用しています。他のコンポーネントもこのマップに配置する場合は、他の人が提案したようにキャストを使用してください。

于 2012-04-23T16:42:56.080 に答える