import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* @author joanna
*
* Window demo, taken from Johnston04 (java prog today)
*
*/
public class BangPopAp extends JFrame implements ActionListener {
// these are all the widgets our inane ap will use
JButton bangButton = new JButton ("Bang");
JButton popButton = new JButton ("Pop");
// now we set everything up in the constructor...
public BangPopAp() {
// set the title bar for the window
this.setTitle("Bangs and Pops!");
// Frames come with their main container. This just adds a reference
// so we can refer to it more easily.
Container canvas = getContentPane();
// the layout determines where things will go when we add them.
canvas.setLayout (new GridLayout(2,1));
// here we add the buttons
canvas.add(bangButton);
canvas.add(popButton);
// we have to tell the buttons what's going to listen to them.
// Doing this is sometimes called "registering". We can use the instance
// `this' because we are implementing an ActionListener (see below).
// Once we've registered, when an event happens to one of the buttons,
// it will call its listener's .actionPerformed.
bangButton.addActionListener(this); // Leave lots of space around these for inner classes!!
popButton.addActionListener(this);
// The below two methods are both inherited from JFrame.
// The first sets the size the window will be when it appears.
// Generally want a value so all your widgets look reasonable from start.
this.setSize(250,150);
// Everything would pretty much work now, but if we didn't do show()
// no one could see it!
this.show();
}
// this is the one thing we have to do to implement ActionListener.
// We have to define method "actionPerformed". This just looks to see which
// widget called it (buttons only have one action!)
public void actionPerformed(ActionEvent e) {
if (e.getSource()==bangButton) {
JOptionPane.showMessageDialog(this, "Bang!");
} else if (e.getSource()== popButton) {
JOptionPane.showMessageDialog(this, "Pop!");
}
}
// now all we do to start the ball rolling is make an instance...
public static void main(String[] args) {
BangPopAp theApp = new BangPopAp();
// except we also need to say what to do when the frame is closed!
// if we didn't have this line, the program would keep running even
// after the window was gone (that would be bad...)
theApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} // class
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* @author joanna
*
* GUI w/ inner classes demo, taken from Johnston04 (java prog today)
*
*/
public class BangPopInnerAp extends JFrame { // implements ActionListener {
// these are all the widgets our inane ap will use
JButton bangButton = new JButton ("Bang");
JButton popButton = new JButton ("Pop");
// now we set everything up in the constructor...
public BangPopInnerAp() {
// set the title bar for the window
this.setTitle("Bangs and Pops!");
// Frames come with their main container. This just adds a reference
// so we can refer to it more easily.
Container canvas = getContentPane();
// the layout determines where things will go when we add them.
canvas.setLayout (new GridLayout(2,1));
// here we add the buttons
canvas.add(bangButton);
canvas.add(popButton);
//Here's where the inner classes come in -- we can't use `this' anymore.
// since we aren't extending the listener.
bangButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, "Bang!");
}
});
popButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, "Pop!");
}
});
// The below two methods are both inherited from JFrame.
// The first sets the size the window will be when it appears.
// Generally want a value so all your widgets look reasonable from start.
this.setSize(250,150);
// Everything would pretty much work now, but if we didn't do show()
// no one could see it!
this.show();
}
/* this goes away now we're using inner classes
public void actionPerformed(ActionEvent e) {
if (e.getSource()==bangButton) {
JOptionPane.showMessageDialog(this, "Bang!");
} else if (e.getSource()== popButton) {
JOptionPane.showMessageDialog(this, "Pop!");
}
}
*/
// now all we do to start the ball rolling is make an instance...
public static void main(String[] args) {
BangPopAp theApp = new BangPopAp();
// except we also need to say what to do when the frame is closed!
// if we didn't have this line, the program would keep running even
// after the window was gone (that would be bad...)
theApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} // class
void |
mouseClicked(MouseEvent e)
Invoked when the mouse button has been clicked (pressed and released) on a component. |
void |
mouseEntered(MouseEvent e)
Invoked when the mouse enters a component. |
void |
mouseExited(MouseEvent e)
Invoked when the mouse exits a component. |
void |
mousePressed(MouseEvent e)
Invoked when a mouse button has been pressed on a component. |
void |
mouseReleased(MouseEvent e)
Invoked when a mouse button has been released on a component. |
void |
mouseDragged(MouseEvent e)
Invoked when a mouse button is pressed on a component and then dragged. |
void |
mouseMoved(MouseEvent e)
Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed. |