Scroll
File: java/Scroll/Main.java
/*
* Like simple button, but showing Silder
* R. Jacob 9/18/2001
*/
import java.awt.Frame;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentListener;
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main (String [] args) {
new Main ();
}
public Main () {
// Window setup
setSize (300, 300);
Container content = getContentPane();
content.setLayout (new FlowLayout());
// Put a scrollbar in it, don't bother remembering
content.add (new MyScrollBar (1));
// Put another scrollbar
content.add (new MyScrollBar (2));
// And a button, for good measure
content.add (new MyButton ("Push me"));
// Show the window
setVisible (true);
}
}
File: java/Scroll/MyScrollBar.java
import java.awt.event.AdjustmentListener;
import java.awt.event.AdjustmentEvent;
import javax.swing.JScrollBar;
public class MyScrollBar extends JScrollBar implements AdjustmentListener {
// My private ID number, so I can tell my scrollbar's apart
private int id;
public MyScrollBar (int id) {
// Customize properties of our ScrollBar
// Named static constant
setOrientation (HORIZONTAL);
setMinimum (0);
setMaximum (100);
// Initial value
setValue (25);
this.id = id;
addAdjustmentListener (this);
}
public void adjustmentValueChanged (AdjustmentEvent event) {
System.out.println ("Scrollbar " + id + ": new value = " + getValue());
}
}
File: java/Scroll/MyButton.java
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
/*
* Same as before
*/
public class MyButton extends JButton implements ActionListener {
public MyButton (String label) {
setText (label);
addActionListener (this);
}
public void actionPerformed(ActionEvent e) {
System.out.println ("Button was pushed");
}
}