import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField;
public class ShowButton implements ActionListener{ private JTextField textField = null; private JButton button = null; private JFrame frame = null; public ShowButton(){ frame = new JFrame("显示按钮文字"); button = new JButton("3"); textField = new JTextField("",20); frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.add(textField); button.addActionListener(this); frame.add(button); frame.setSize(400, 100); frame.setResizable(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { textField.setText(""); textField.setText(button.getActionCommand()); System.out.println(button.getActionCommand()+"==="); } public static void main(String[] args) { new ShowButton(); }
}
|