import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicArrowButton;
public class Spinner extends JPanel {
private int orientation = SwingConstants.VERTICAL;
private BasicArrowButton incrementButton;
private BasicArrowButton decrementButton;
public Spinner() {
createComponents();
}
public Spinner(int o) {
orientation = o;
createComponents();
}
public void setEnabled(boolean enable) {
incrementButton.setEnabled(enable);
decrementButton.setEnabled(enable);
}
public boolean isEnabled() {
return (incrementButton.isEnabled() && decrementButton.isEnabled());
}
protected void createComponents() {
if (orientation == SwingConstants.VERTICAL) {
setLayout(new GridLayout(2, 1));
incrementButton = new BasicArrowButton(SwingConstants.NORTH);
decrementButton = new BasicArrowButton(SwingConstants.SOUTH);
add(incrementButton);
add(decrementButton);
} else if (orientation == SwingConstants.HORIZONTAL) {
setLayout(new GridLayout(1, 2));
incrementButton = new BasicArrowButton(SwingConstants.EAST);
decrementButton = new BasicArrowButton(SwingConstants.WEST);
add(decrementButton);
add(incrementButton);
}
}
public JButton getIncrementButton() {
return (incrementButton);
}
public JButton getDecrementButton() {
return (decrementButton);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = (JPanel) frame.getContentPane();
panel.setLayout(new BorderLayout());
JTextField field = new JTextField(20);
Spinner spinner = new Spinner();
panel.add(field, "Center");
panel.add(spinner, "East");
Dimension dim = frame.getToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getWidth() / 2, dim.height / 2
- frame.getHeight() / 2);
frame.pack();
frame.show();
}
}
|