次のコードでActionListener
2つに追加すると、問題が発生しました。JButtons
これらの2つのボタン(b1、b2)をクリックすると、1と2をそれぞれ印刷したい
しかし、私は1つか2つしか印刷できません。
この問題を解決するための解決策を教えてください。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class CCCc extends JFrame implements ActionListener{
JButton b1,b2;
JTextField f1;
CCCc(){
setSize(500,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
f1=new JTextField();
f1.setHorizontalAlignment(JTextField.RIGHT);
add("North",f1);
JPanel p1=new JPanel(new GridLayout(1,2));
add(p1);
b1=new JButton("1");
b1.addActionListener(this);
b2=new JButton("2");
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
setVisible(true);
}
public void actionPerformed(ActionEvent evt){
String s=f1.getText();
f1.setText(s+"1");
}
}