Hello all,
I have the following code. I would like to be able to set the text in the label (called label) to something else when a user click the close button.
I am guessing that my action class does not know what label is but I'm not sure how to reference since its in another class.
Thanks!!
VBGuy
package test; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main { public static void main(String[] args) { ShowFrame(); } public static void ShowFrame() { JFrame MyWindow = new JFrame("Hello World"); MyWindow.setSize(800, 600); MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyWindow.setLayout(null); //Label JLabel label = new JLabel("Hello World!"); label.setBounds(200, 10, 100, 25); //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); MyWindow.add(label); //Set Visible MyWindow.setVisible(true); bbox.setVisible(true); CloseButton.setVisible(true); OkButton.setVisible(true); //Set Action Listener CloseButton.addActionListener(new Action()); } }
package test; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Action implements ActionListener { public void actionPerformed(ActionEvent e) { label.setText("You Clicked Close!"); } }