HI all,
I am trying to add a close button to some basic swing code I was playing with, but no matter where I put the code for the Jbutton I get errors.
Here is the code for the program as is now
// Import the swing and AWT classes needed import java.awt.EventQueue; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * Basic Swing example. */ public class SwingExample { public static void main(String[] args){ private JButton quitButton; // Make sure all Swing/AWT instantiations and accesses are done on the // Event Dispatch Thread (EDT) EventQueue.invokeLater(new Runnable() { public void run() { // Create a JFrame, which is a Window with "decorations", i.e. // title, border and close-button JFrame f = new JFrame("Swing Example Window"); // Set a simple Layout Manager that arranges the contained // Components f.setLayout(new FlowLayout()); // Add some Components f.add(new JLabel("Hello, world!")); f.add(new JButton("Press me!")); f.add(new JButton("Quit Button")); // "Pack" the window, making it "just big enough". f.pack(); // quit button click event listener quitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Quit", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { System.exit(0); } } }); // Set the visibility as true, thereby displaying it f.setVisible(true); } }); } }
and here are the errors I am getting with this.
C:\Users\welshd.psrc\Documents\gui Java\SwingExample.java:17: illegal start of expression
private JButton quitButton;
^
1 error
Where in the code do I put the "private JButton quitButton; code at so it will work right?
Thanks in advance.
COY