如何通过单击JButton清除JTextField

时间:2011-03-16 17:06:22

标签: java swing reset jbutton jtextfield

  

可能重复:
  JButton needs to change JTextfield text

如果点击JTextField时如何清除JButton

1 个答案:

答案 0 :(得分:23)

正在寻找EventHandlingActionListener

或代码?

JButton b = new JButton("Clear");
b.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        textfield.setText("");
        //textfield.setText(null); //or use this
    }
});

另见
How to Use Buttons