日期:2014-05-20 浏览次数:20788 次
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
public class SliderTest_1
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
SliderTestFrame frame = new SliderTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class SliderTestFrame extends JFrame
{
public SliderTestFrame()
{
setTitle("SliderTest_1");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
sliderPanel = new JPanel();
sliderPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
listener = new ChangeListener()
{
public void stateChanged(ChangeEvent event)
{
JSlider source = (JSlider)event.getSource();
textField.setText(""+source.getValue());
}
};
JSlider slider = new JSlider(SwingConstants.VERTICAL,0,100,20);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
addSlider(slider,"Plain");
textField = new JTextField();
add(sliderPanel,BorderLayout.CENTER);
add(textField,BorderLayout.SOUTH);
}
public void addSlider(JSlider s,String description)
{
s.addChangeListener(listener);
JPanel panel = new JPanel();
panel.add(s);
panel.add(new JLabel(description));
sliderPanel.add(panel);
}
public static final int DEFAULT_WIDTH = 350;
public static final int DEFAULT_HEIGHT = 450;
private JPanel sliderPanel;
private JTextField textField;
private ChangeListener listener;
}
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
public class SliderTest_2
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
SliderTestFrame frame = new SliderTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);