简体   繁体   English

如何使jtextarea响应另一个jtextarea更改

[英]How to make a jtextarea respond to another jtextarea change

    JComboBox cBox = new JComboBox();
    cBox.addItem("Food"); String x = "Food";
    cBox.addItem("Shirt");
    cBox.addItem("Computer");
    cBox.setSelectedItem(null);
    cBox.setBounds(56, 127, 336, 27);
    contentPane.add(cBox);

    tPName = new JTextArea();
    tPName.setBounds(38, 227, 130, 26);
    contentPane.add(tPName);
    tPName.setColumns(10);

    tSName = new JTextArea();
    tSName.setBounds(262, 227, 130, 26);
    contentPane.add(tSName);
    tSName.setColumns(10);

    JButton btn = new JButton("Further Details");
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String name = (String) cBox.getSelectedItem(); //gets value of combo box
            tPName.setText(name); //text area displays value
            String x = (String) tPName.getText();
            name = "Food"; tSName.setText(F);
            name = "Shirt"; tSName.setText(S);
            name ="Computer"; tSName.setText(C);

        }
    });
    btn.setBounds(162, 166, 117, 29);
    contentPane.add(btn);

how would I make tSName display a text corresponding to a specific value of tPName which is copied from a combobox where F, S and C strings stand for those corresponding values I want to use. 我如何使tSName显示与tPName特定值相对应的文本,该文本是从组合框中复制的,其中F,S和C字符串代表我要使用的那些对应值。

I'm not sure if I understand you correctly. 我不确定我是否正确理解您。 You try to make one textarea display something, when the text of another textarea changes, am I right? 您尝试使一个文本区域显示某些内容,而当另一文本区域的文本更改时,对吗? If yes, you should look for an Event of the first Textbox (something like tPNameKeyTyped ) in this Event you can check the tPName.getText() simply with 如果是,则应在此事件中查找第一个文本框的事件(类似于tPNameKeyTyped ),您可以使用以下tPName.getText()简单地检查tPName.getText()

if(tPName.getText().equals("whatever")){
   tSName.setText("whatever u want");
}

Is that what you are looking for? 那是您要找的东西吗?

I right now dont have pc so I cant check errors but. 我现在没有电脑,所以我无法检查错误。 this should work 这应该工作

 tPName.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tSName.setText(tPName.getText()); // put the value of tPName to tSName.
            // i think this is all you want. i actually cant get your question.
           // you can put a switch case to put coreesponding values in tSName. like: 
     switch(tPName.getText()){
            case "a" : tSName.setText("1");
                         break;
            //..... so on
             default:
                      tSName.setText("stack overflow is great");
                         break;

         }
      }
    });

EDIT:- Onece you do this try to refresh and validate the textareas. 编辑:-Onece,您可以尝试刷新并验证textareas。

tPName.validate();
tSName.validate();
tPName.repaint(); // refresh  still not sure as i dont have pc if this not works try <your jframe>.repaint(); this should work

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM