Here is that self contained simple example.
package ca.sarah_happy.sandbox.circles; import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSlider; import javax.swing.SwingUtilities; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class SliderSample implements Runnable { public static void main(String[] args) { /* * do all the work on the GUI thread. */ SwingUtilities.invokeLater(new SliderSample()); } /* * things I will need access to later. */ private JSlider slider; private JLabel label; /* * do the work of creating the user interface. */ @Override public void run() { slider = new JSlider(); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setMaximum(127); slider.setMinimum(-128); slider.setMajorTickSpacing(32); slider.setMinorTickSpacing(8); slider.setValue(0); label = new JLabel("value = " + slider.getValue()); JFrame frame = new JFrame("Slider Sample"); frame.setLocationByPlatform(true); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Container p = frame.getContentPane(); p.add(label, BorderLayout.SOUTH); p.add(slider); slider.addChangeListener(sliderChangeListener); frame.pack(); frame.setVisible(true); } /* * and things that will be triggered later */ private ChangeListener sliderChangeListener = new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { String adjust = ""; if (slider.getValueIsAdjusting()) { adjust = " (adjusting)"; } label.setText("value = " + slider.getValue() + adjust); } }; }
No comments:
Post a Comment