Java by API/javax.swing/JSlider — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:20, 31 мая 2010

extends JSlider

 
import java.awt.ruponent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;
import javax.swing.border.rupoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class MainClass extends JPanel {
  public JPopupMenu popup;
  SliderMenuItem slider;
  int theValue = 0;
  public MainClass() {
    popup = new JPopupMenu();
    slider = new SliderMenuItem();
    popup.add(slider);
    popup.add(new JSeparator());
    JMenuItem ticks = new JCheckBoxMenuItem("Slider Tick Marks");
    ticks.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        slider.setPaintTicks(!slider.getPaintTicks());
      }
    });
    JMenuItem labels = new JCheckBoxMenuItem("Slider Labels");
    labels.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        slider.setPaintLabels(!slider.getPaintLabels());
      }
    });
    popup.add(ticks);
    popup.add(labels);
  }
  public static void main(String s[]) {
    JFrame frame = new JFrame("Menu Element Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new MainClass());
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
  // Inner class that defines our special slider menu item
  class SliderMenuItem extends JSlider implements MenuElement {
    public SliderMenuItem() {
      setBorder(new CompoundBorder(new TitledBorder("Control"), new EmptyBorder(10, 10, 10, 10)));
      setMajorTickSpacing(20);
      setMinorTickSpacing(10);
    }
    public void processMouseEvent(MouseEvent e, MenuElement path[], MenuSelectionManager manager) {
    }
    public void processKeyEvent(KeyEvent e, MenuElement path[], MenuSelectionManager manager) {
    }
    public void menuSelectionChanged(boolean isIncluded) {
    }
    public MenuElement[] getSubElements() {
      return new MenuElement[0];
    }
    public Component getComponent() {
      return this;
    }
  }
}





JSlider: addChangeListener(ChangeListener l)

 
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    final JSlider slider = new JSlider(0, 150, 0);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    slider.setPreferredSize(new Dimension(150, 30));
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent event) {
        int value = slider.getValue();
        if (value == 0) {
          System.out.println("0");
        } else if (value > 0 && value <= 30) {
          System.out.println("value > 0 && value <= 30");
        } else if (value > 30 && value < 80) {
          System.out.println("value > 30 && value < 80");
        } else {
          System.out.println("max");
        }
      }
    });
    f.add(slider);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}





JSlider: getExtent()

 

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    int extent = slider.getExtent();
  }
}





JSlider: getMaximum()

 
import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    // Get the current value
    int value = slider.getValue();
    // Get the minimum value
    int min = slider.getMinimum();
    // Get the maximum value
    int max = slider.getMaximum();
  }
}





JSlider: getMinimum()

 
import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    // Get the current value
    int value = slider.getValue();
    // Get the minimum value
    int min = slider.getMinimum();
    // Get the maximum value
    int max = slider.getMaximum();
  }
}





JSlider: getPaintTicks()

 

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a horizontal slider that moves left-to-right
    JSlider slider = new JSlider();
    // Determine if currently showing ticks
    boolean b = slider.getPaintTicks(); // false
  }
}





JSlider: getSnapToTicks()

 
import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a horizontal slider that moves left-to-right
    JSlider slider = new JSlider();
    boolean b = slider.getSnapToTicks(); // false
  }
}





JSlider: getValue()

 
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    final JSlider slider = new JSlider(0, 150, 0);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    slider.setPreferredSize(new Dimension(150, 30));
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent event) {
        int value = slider.getValue();
        if (value == 0) {
          System.out.println("0");
        } else if (value > 0 && value <= 30) {
          System.out.println("value > 0 && value <= 30");
        } else if (value > 30 && value < 80) {
          System.out.println("value > 30 && value < 80");
        } else {
          System.out.println("max");
        }
      }
    });
    f.add(slider);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}





JSlider: getValueIsAdjusting()

 

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent evt) {
        JSlider slider = (JSlider) evt.getSource();
        if (!slider.getValueIsAdjusting()) {
          int value = slider.getValue();
          System.out.println(value);
        }
      }
    });
  }
}





JSlider.isFilled

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JSlider;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Filled Slider");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider js1 = new JSlider();
    js1.putClientProperty("JSlider.isFilled", Boolean.TRUE);
    JSlider js2 = new JSlider();
    js2.putClientProperty("JSlider.isFilled", Boolean.FALSE);
    frame.getContentPane().add(js1, BorderLayout.NORTH);
    frame.getContentPane().add(js2, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





JSlider: setExtent(int extent)

 

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    int newExtent = 10;
    slider.setExtent(newExtent);
  }
}





JSlider: setInverted(boolean b)

 
import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    slider.setInverted(true);
  }
}





JSlider: setLabelTable(Dictionary labels)

 
import java.awt.BorderLayout;
import java.util.Hashtable;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
public class MainClass {
  public static void main(String args[]) throws Exception {
    String title = (args.length == 0 ? "Sample Slider" : args[0]);
    JFrame frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider js4 = new JSlider(JSlider.VERTICAL);
    Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
    table.put(0, new JLabel("O"));
    table.put(10, new JLabel("Ten"));
    table.put(25, new JLabel("Twenty-Five"));
    table.put(34, new JLabel("Thirty-Four"));
    table.put(52, new JLabel("Fifty-Two"));
    table.put(70, new JLabel("Seventy"));
    table.put(82, new JLabel("Eighty-Two"));
    table.put(100, new JLabel("100"));
    js4.setLabelTable(table);
    js4.setPaintLabels(true);
    js4.setSnapToTicks(true);
    frame.add(js4, BorderLayout.EAST);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





JSlider: setMajorTickSpacing(int v)

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JSlider;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JSlider Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider slider = new JSlider();
    slider.setMinorTickSpacing(5);
    slider.setMajorTickSpacing(10);
    slider.setPaintTicks(true);
    slider.setSnapToTicks(true);
    slider.setPaintTrack(false);
    slider.setPaintLabels(true);
    f.add(slider, BorderLayout.CENTER);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}





JSlider: setMaximum(int maximum)

 
import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    int newMax = 256;
    slider.setMaximum(newMax);
  }
}





JSlider: setMinimum(int minimum)

 
import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    int newMin = 0;
    slider.setMinimum(newMin);
  }
}





JSlider: setMinorTickSpacing(int v)

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JSlider;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JSlider Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider slider = new JSlider();
    slider.setMinorTickSpacing(5);
    slider.setMajorTickSpacing(10);
    slider.setPaintTicks(true);
    slider.setSnapToTicks(true);
    slider.setPaintTrack(false);
    slider.setPaintLabels(true);
    f.add(slider, BorderLayout.CENTER);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}





JSlider: setOrientation(int orientation)

 

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    slider.setOrientation(JSlider.VERTICAL);
    slider.setInverted(false);
  }
}





JSlider: setPaintLabels(boolean t)

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JSlider;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JSlider Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider slider = new JSlider();
    slider.setMinorTickSpacing(5);
    slider.setMajorTickSpacing(10);
    slider.setPaintTicks(true);
    slider.setSnapToTicks(true);
    slider.setPaintTrack(false);
    slider.setPaintLabels(true);
    f.add(slider, BorderLayout.CENTER);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}





JSlider: setPaintTicks(boolean t)

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JSlider;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JSlider Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider slider = new JSlider();
    slider.setMinorTickSpacing(5);
    slider.setMajorTickSpacing(10);
    slider.setPaintTicks(true);
    slider.setSnapToTicks(true);
    slider.setPaintTrack(false);
    slider.setPaintLabels(true);
    f.add(slider, BorderLayout.CENTER);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}





JSlider: setPaintTrack(boolean b)

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JSlider;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JSlider Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider slider = new JSlider();
    slider.setMinorTickSpacing(5);
    slider.setMajorTickSpacing(10);
    slider.setPaintTicks(true);
    slider.setSnapToTicks(true);
    slider.setPaintTrack(false);
    slider.setPaintLabels(true);
    f.add(slider, BorderLayout.CENTER);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}





JSlider: setPreferredSize(Dimension preferredSize)

 
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    final JSlider slider = new JSlider(0, 150, 0);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    slider.setPreferredSize(new Dimension(150, 30));
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent event) {
        int value = slider.getValue();
        if (value == 0) {
          System.out.println("0");
        } else if (value > 0 && value <= 30) {
          System.out.println("value > 0 && value <= 30");
        } else if (value > 30 && value < 80) {
          System.out.println("value > 30 && value < 80");
        } else {
          System.out.println("max");
        }
      }
    });
    f.add(slider);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}





JSlider: setSnapToTicks(boolean b)

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JSlider;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JSlider Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider slider = new JSlider();
    slider.setMinorTickSpacing(5);
    slider.setMajorTickSpacing(10);
    slider.setPaintTicks(true);
    slider.setSnapToTicks(true);
    slider.setPaintTrack(false);
    slider.setPaintLabels(true);
    f.add(slider, BorderLayout.CENTER);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}





JSlider: setToolTipText(String s)

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JSlider;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JSlider Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider slider = new JSlider();
    slider.setMinorTickSpacing(5);
    slider.setMajorTickSpacing(10);
    slider.setPaintTicks(true);
    slider.setSnapToTicks(true);
    slider.setPaintTrack(false);
    slider.setPaintLabels(true);
    slider.setToolTipText("The value is 50");
    f.add(slider, BorderLayout.CENTER);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}





JSlider: setValue(int n)

 
import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    int newValue = 33;
    slider.setValue(newValue);
  }
}





new JSlider(int min, int max)

 
import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a horizontal slider with min=0, max=100, value=50
    JSlider slider = new JSlider();
    // Create a horizontal slider with custom min and max; value is set to the middle
    int minimum = -255;
    int maximum = 256;
    slider = new JSlider(minimum, maximum);
  }
}





new JSlider(int min, int max, int value)

 

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    int initValue = 0;
    int minimum = 10;
    int maximum = 100;
    JSlider slider = new JSlider(minimum, maximum, initValue);
  }
}





new JSlider(int orientation)

 
import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider(JSlider.VERTICAL);
  }
}





new JSlider(int orientation, int min, int max, int value)

 
import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    int initValue = 0;
    int minimum = 10;
    int maximum = 100;
    JSlider slider = new JSlider(JSlider.VERTICAL, minimum, maximum, initValue);
  }
}





Slider.horizontalThumbIcon

 
import java.awt.BorderLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Icon Slider");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon icon = new ImageIcon("logo.gif");
    UIDefaults defaults = UIManager.getDefaults();
    defaults.put("Slider.horizontalThumbIcon", icon);
    JSlider aJSlider = new JSlider();
    aJSlider.setPaintTicks(true);
    frame.add(aJSlider, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}