import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class MyWindow extends JFrame implements ActionListener { JButton theButton; JTextField inputField; public MyWindow(){ setTitle("MyWindow"); setSize(300,300); setLayout(new GridLayout(3,1)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new JLabel("This is the Label")); theButton= new JButton("OK"); theButton.addActionListener(this); add(theButton); inputField=new JTextField(); inputField.addActionListener(this); add(inputField); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==theButton){ System.out.println("OK"); } else if(e.getSource()==inputField){ System.out.println(inputField.getText()); } inputField.setText(""); } public static void main(String[] args){ MyWindow aWindow=new MyWindow(); } }