javaでtextFieldに文字を打つと
textAreaに数値を表示しようとしています
例えばtextFieldに”パン”と打ったらtextAreaに1を表示
"ごはん”と打ったら2を表示のような
JButtonを経由すれば出来たのですが
JButtonを経由せずtextFieldに"パン”と打ったらtextAreaに1を表示するにはどうすればよいでしょうか?
因みに今はこんな感じです
import javax.swing.✳︎;
import java.awt.event.✳︎;
public class pan{
static JTextField textF = new JTextField();
static JButton btn = new JButton(”OK”);
static JTextArea textA = new JTextArea();
static EI EI = new EI();
public static void main(String[] args) {
myframe.setBounds(100, 100, 640, 480);
myframe.setVisible(true);
myframe.setLayout(null);
myframe.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
myframe.add(mytextfield);
mytextfield.setBounds(10, 10, 500, 50);
myframe.add(mytextarea);
mytextarea.setBounds(10, 70,600,360);
myframe.add(mybutton);
mybutton.setBounds(520, 10, 100, 50);
mybutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
mytextarea.setText(
myEl. hanasu(mytextfield.getText()));
}
}
);
}
}
class El{
public String hanasu(String str1){
if(str1.equals("パン"))return "1";
if(str1.equals("ごはん"))return "2";
return "3";
}
}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。