JFrame: setLocationRelativeTo(Component c) : JFrame « javax.swing « Java by API






JFrame: setLocationRelativeTo(Component c)

  

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);
  }
}

   
    
  








Related examples in the same category

1.JFrame.DISPOSE_ON_CLOSE
2.JFrame.DO_NOTHING_ON_CLOSE
3.JFrame.EXIT_ON_CLOSE
4.JFrame: addComponentListener(ComponentListener l)
5.JFrame: addContainerListener(ContainerListener l)1
6.JFrame: addMouseWheelListener(MouseWheelListener l)
7.JFrame: addWindowListener(WindowListener lis)
8.JFrame: getAccessibleContext()
9.JFrame: getContentPane().getActionMap() [Bind key action to JFrame]
10.JFrame: getDefaultCloseOperation()
11.JFrame: getFontMetrics(Font font)
12.JFrame: getGlassPane()
13.JFrame: getLayeredPane()
14.JFrame: isFocusOwner()
15.JFrame: isUndecorated()
16.JFrame: processWindowEvent(WindowEvent e)
17.JFrame: setAlwaysOnTop(boolean alwaysOnTop)
18.JFrame: setBackground(Color c)
19.JFrame: setCursor(Cursor c)
20.JFrame: setDefaultButton(JButton defaultButton)
21.JFrame: setDefaultButton(JButton bn) (Remove 'Enter' key stroke from JTextField)
22.JFrame: setDefaultCloseOperation(int option)
23.JFrame: setDefaultLookAndFeelDecorated(boolean b)
24.JFrame: setExtendedState(int state)
25.JFrame: setFocusable(boolean focusable)
26.JFrame: setFocusableWindowState(boolean focusableWindowState)
27.JFrame: setFocusTraversalPolicy(FocusTraversalPolicy lo)
28.JFrame: setIconImage(Image image)
29.JFrame: setJMenuBar(JMenuBar bar)
30.JFrame: setLocation(int x, int y)
31.JFrame: setLocationByPlatform(boolean locationByPlatform)
32.JFrame: setMaximizedBounds(Rectangle bounds)
33.JFrame: setSize(int width, int height)
34.JFrame: setTitle(String title)
35.JFrame: setUndecorated(boolean b)
36.JFrame: setVisible(boolean visible)