- import java.awt.BorderLayout;
- import java.awt.Component;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- public class JButtonTextField {
- public static void main(String[] args) {
- JFrame frame = new JFrame("JButtonTextField");
- JTextField field = new JTextField(30);
- JButton button = new JButton("Button");
- field.setLayout(new BorderLayout());
- field.add(button,BorderLayout.EAST);
- field.setText("JButtonTextField");
- frame.getContentPane().add(field);
- frame.pack();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- button.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent event){
- Component parent = ((JButton)event.getSource()).getParent();
- ((JTextField)parent).setText("Button Pressed");
- }
- });
- }
- }
Resultado:
0 comentários :
Postar um comentário