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);
我如何使tSName显示与tPName特定值相对应的文本,该文本是从组合框中复制的,其中F,S和C字符串代表我要使用的那些对应值。
答案 0 :(得分:1)
我不确定我是否理解正确。您尝试使一个文本区域显示某些内容,而当另一文本区域的文本更改时,对吗?如果是,则应在此事件中查找第一个文本框的事件(类似tPNameKeyTyped
),只需使用
tPName.getText()
if(tPName.getText().equals("whatever")){
tSName.setText("whatever u want");
}
您正在寻找什么吗?
答案 1 :(得分:0)
我现在没有电脑,所以我不能检查错误。 这应该工作
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;
}
}
});
编辑:-您一次尝试刷新并验证文本区域。
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