Trying to add a close button
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
Code :
// 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
Re: Trying to add a close button
Members of a class have access modifiers, but variables declared within a function (in this case main) aren't defined with access modifiers. If you want the JButton within main only, remove the private and move the variable within scope of where you need it. If you want access associated with a class move it to the appropriate location so the class has access. In this case, you could probably move it into the scope of the run method (remove the private).
Re: Trying to add a close button
OK I tried to add it to the run command, but it still gives errors.
Here is the updated code:
Code :
// 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){
// Make sure all Swing/AWT instantiations and accesses are done on the
// Event Dispatch Thread (EDT)
EventQueue.invokeLater(new Runnable() {
public void run(JButton"quitButton"); {
// 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("quitButton"));
// "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
C:\Users\welshd.psrc\Documents\gui Java\SwingExample.java:20: <identifier> expected
public void run(JButton"quitButton"); {
^
Ok the first carrot is supposed to be under teh " before the Q
C:\Users\welshd.psrc\Documents\gui Java\SwingExample.java:20: ';' expected
public void run(JButton"quitButton"); {
^
the second carrot is supposed to be under the last )
2 errors
What does <identifier> expected mean??
Thanks
COY
Re: Trying to add a close button
Code :
public void run(JButton"quitButton");
This is not a correct format...you want to create your button object as you would any other object within the run method.
Re: Trying to add a close button
so are you talking about using the f.add (new JButton(:closeButton:)); method??
If so I already tried that and it said it couldn't find the jbutton.
so where else would I add it??
Re: Trying to add a close button
No, because you need a reference to the button to add a listener to it...in pseudocode:
Code :
JButton quit = new JButton("text");
whatever.add(quit);
quit.addActionListener(...