Hello all,
I'm a new Java user and I'm trying out the ActionListener feature but I am not sure what is the best way to implement it. Below is my code where I've been testing out different Java features.
Based on my code below, what is the best way to add an ActionListener to the OK Button and the Close Button? Perhaps the OK Button has a MessageBox that says "OK Button Pressed" and the Close Button closes the application.
Thanks for the help!
VBGuy
package test; import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { //Hello(); //Calc(); ShowFrame(); } public static void Hello() { System.out.println("Hello World"); } public static void Calc() { int a, b, c; a = 1; b = 1; c = a + b; System.out.println(c); } public static void ShowFrame() { JFrame MyWindow = new JFrame("Hello World"); MyWindow.setSize(800, 600); MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyWindow.setLayout(null); //Text Field JTextField bbox = new JTextField(16); bbox.setBounds(10, 10, 100, 25); //Add Buttons JButton CloseButton = new JButton("Close"); CloseButton.setBounds(10, 100, 90, 40); JButton OkButton = new JButton("Ok"); OkButton.setBounds(100, 100, 90, 40); //Add to Frame MyWindow.add(CloseButton); MyWindow.add(bbox); MyWindow.add(OkButton); //Set Visible MyWindow.setVisible(true); bbox.setVisible(true); CloseButton.setVisible(true); OkButton.setVisible(true); //Set Action Listener CloseButton.addActionListener(null); } }