Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class Main extends JFrame { JTextField field = new JTextField(); public static void main(String[] args) { Main listner = new Main(); } public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Press me"); button.addActionListener(new ButtonListener()); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(button, BorderLayout.SOUTH); this.getContentPane().add(field, BorderLayout.NORTH); this.pack(); this.setVisible(true); } class ButtonListener implements ActionListener { boolean toggle = true; public void actionPerformed(ActionEvent e) { if (toggle) field.setText("Java Code"); else field.setText("Hello"); toggle = !toggle; } } } |
Output: