下面列出了javax.swing.JSlider#addKeyListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void installUI(JComponent c) {
JSlider slider = (JSlider) c;
slider.addFocusListener(focusListener);
slider.addMouseListener(mouseListener);
slider.addMouseMotionListener(mouseListener);
RepaintChangeListener rcl = new RepaintChangeListener(c);
slider.getModel().addChangeListener(rcl);
slider.putClientProperty(REPAINT_CHANGE_LISTENER_KEY, rcl);
slider.addKeyListener(keyListener);
slider.setFocusable(true);
slider.setRequestFocusEnabled(true);
calculateGeometry((JSlider) c);
}
/**
* Create space slider panel with - and + buttons and label
* @param actionMinus ActionCommand for minus button
* @param actionPlus ActionCommand for plus button
* @param iconName Icon name to show
* @param text Text for label
* @param minValue Minimum value in slider
* @param maxValue Maximum value in slider
* @param sliderValue slider value
* @param actionSlider slider action command
* @param listener Action Listener
*/
public SpaceSliderPanel(final String actionMinus,
final String actionPlus, final String iconName, final String text,
final int minValue, final int maxValue, final int sliderValue,
final String actionSlider, final ActionListener listener) {
super();
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
btnMinus = new IconButton(Icons.getIconByName(Icons.ICON_MINUS),
Icons.getIconByName(Icons.ICON_MINUS_PRESSED), false, actionMinus,
this);
btnMinus.addActionListener(listener);
this.add(Box.createRigidArea(new Dimension(5, 5)));
this.add(btnMinus);
SpaceGreyPanel panel = new SpaceGreyPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
label = new IconLabel(null, Icons.getIconByName(iconName), text);
panel.add(label);
slider = new JSlider(minValue, maxValue, sliderValue);
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);
slider.setBackground(GuiStatics.COLOR_GREYBLUE);
slider.setForeground(GuiStatics.COLOR_COOL_SPACE_BLUE);
slider.addKeyListener(null);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(final ChangeEvent e) {
if (e.getSource() instanceof JSlider) {
JSlider slide = (JSlider) e.getSource();
if (slide.getValue() % slide.getMinorTickSpacing() == 0) {
listener.actionPerformed(new ActionEvent(e, 0, actionSlider));
}
}
}
});
panel.add(slider);
this.add(panel);
btnPlus = new IconButton(Icons.getIconByName(Icons.ICON_PLUS),
Icons.getIconByName(Icons.ICON_PLUS_PRESSED), false, actionPlus, this);
btnPlus.addActionListener(listener);
this.add(btnPlus);
this.add(Box.createRigidArea(new Dimension(5, 5)));
}
/**
* Create Research Tech panel with - and + buttons and up arrow to
* upgrade tech level.
* @param actionMinus ActionCommand for minus button
* @param actionPlus ActionCommand for plus button
* @param iconName Icon name to show
* @param text Text for tech focus label
* @param text2 Text for tech level label
* @param actionUpgrade ActionCommand for upgrade button
* @param sliderValue slider value
* @param actionSlider slider action command
* @param listener Action Listener
*/
public ResearchTechPanel(final String actionMinus,
final String actionPlus, final String iconName, final String text,
final String text2, final String actionUpgrade, final int sliderValue,
final String actionSlider, final ActionListener listener) {
super();
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
btnMinus = new IconButton(Icons.getIconByName(Icons.ICON_MINUS),
Icons.getIconByName(Icons.ICON_MINUS_PRESSED), false, actionMinus,
this);
btnMinus.addActionListener(listener);
this.add(Box.createRigidArea(new Dimension(5, 5)));
this.add(btnMinus);
SpaceGreyPanel panel = new SpaceGreyPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
label = new IconLabel(null, Icons.getIconByName(iconName), text);
panel.add(label);
slider = new JSlider(0, 100, sliderValue);
slider.setMinorTickSpacing(4);
slider.setMajorTickSpacing(20);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);
slider.setBackground(GuiStatics.COLOR_GREYBLUE);
slider.setForeground(GuiStatics.COLOR_COOL_SPACE_BLUE);
slider.addKeyListener(null);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(final ChangeEvent e) {
if (e.getSource() instanceof JSlider) {
JSlider slide = (JSlider) e.getSource();
if (slide.getValue() % 4 == 0) {
listener.actionPerformed(new ActionEvent(e, 0, actionSlider));
}
}
}
});
panel.add(slider);
lvlLabel = new IconLabel(null, Icons.getIconByName(Icons.ICON_EMPTY),
text2);
panel.add(lvlLabel);
this.add(panel);
btnUpgrade = new IconButton(Icons.getIconByName(Icons.ICON_ARROWUP),
Icons.getIconByName(Icons.ICON_ARROWUP_PRESSED), false, actionUpgrade,
this);
btnUpgrade.setDisabledImage(
Icons.getIconByName(Icons.ICON_ARROWUP_DISABLED).getIcon());
btnUpgrade.addActionListener(listener);
btnUpgrade.setEnabled(false);
this.add(btnUpgrade);
btnPlus = new IconButton(Icons.getIconByName(Icons.ICON_PLUS),
Icons.getIconByName(Icons.ICON_PLUS_PRESSED), false, actionPlus, this);
btnPlus.addActionListener(listener);
this.add(btnPlus);
this.add(Box.createRigidArea(new Dimension(5, 5)));
}
/** Constructs an image viewer. */
public ImageViewer(final Context context) {
super(TITLE);
context.inject(this);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(this);
// content pane
pane = new JPanel();
pane.setLayout(new BorderLayout());
setContentPane(pane);
setSize(350, 350); // default size
// navigation sliders
sliderPanel = new JPanel();
sliderPanel.setVisible(false);
sliderPanel.setBorder(new EmptyBorder(5, 3, 5, 3));
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.Y_AXIS));
pane.add(BorderLayout.SOUTH, sliderPanel);
final JPanel nPanel = new JPanel();
nPanel.setLayout(new BoxLayout(nPanel, BoxLayout.X_AXIS));
sliderPanel.add(nPanel);
sliderPanel.add(Box.createVerticalStrut(2));
nSlider = new JSlider(1, 1);
nSlider.setEnabled(false);
nSlider.addChangeListener(this);
nPanel.add(new JLabel("N"));
nPanel.add(Box.createHorizontalStrut(3));
nPanel.add(nSlider);
final JPanel ztcPanel = new JPanel();
ztcPanel.setLayout(new BoxLayout(ztcPanel, BoxLayout.X_AXIS));
sliderPanel.add(ztcPanel);
// image icon
final BufferedImage dummy = AWTImageTools.makeImage(new byte[1][1], 1, 1,
false);
icon = new ImageIcon(dummy);
iconLabel = new JLabel(icon, SwingConstants.LEFT);
iconLabel.setVerticalAlignment(SwingConstants.TOP);
pane.add(new JScrollPane(iconLabel));
// cursor probe
probeLabel = new JLabel(" ");
probeLabel.setHorizontalAlignment(SwingConstants.CENTER);
probeLabel.setBorder(new BevelBorder(BevelBorder.RAISED));
pane.add(BorderLayout.NORTH, probeLabel);
iconLabel.addMouseMotionListener(this);
// menu bar
final JMenuBar menubar = new JMenuBar();
// FIXME: currently the menu bar is disabled to restrict the use of
// ImageViewer to the Show command. We could attempt to get this
// implementation working nicely, or just convert to an IJ2
// implementation.
// setJMenuBar(menubar);
final JMenu file = new JMenu("File");
file.setMnemonic('f');
menubar.add(file);
final JMenuItem fileOpen = new JMenuItem("Open...");
fileOpen.setMnemonic('o');
fileOpen.setActionCommand("open");
fileOpen.addActionListener(this);
file.add(fileOpen);
fileSave = new JMenuItem("Save...");
fileSave.setMnemonic('s');
fileSave.setEnabled(false);
fileSave.setActionCommand("save");
fileSave.addActionListener(this);
file.add(fileSave);
fileView = new JMenuItem("View Metadata...");
final JMenuItem fileExit = new JMenuItem("Exit");
fileExit.setMnemonic('x');
fileExit.setActionCommand("exit");
fileExit.addActionListener(this);
file.add(fileExit);
final JMenu options = new JMenu("Options");
options.setMnemonic('p');
menubar.add(options);
final JMenuItem optionsFPS = new JMenuItem("Frames per Second...");
optionsFPS.setMnemonic('f');
optionsFPS.setActionCommand("fps");
optionsFPS.addActionListener(this);
options.add(optionsFPS);
final JMenu help = new JMenu("Help");
help.setMnemonic('h');
menubar.add(help);
final JMenuItem helpAbout = new JMenuItem("About...");
helpAbout.setMnemonic('a');
helpAbout.setActionCommand("about");
helpAbout.addActionListener(this);
help.add(helpAbout);
// add key listener to focusable components
nSlider.addKeyListener(this);
}