Java by API/java.awt/Cursor

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

Cursor.CROSSHAIR_CURSOR

   <source lang="java">
 

import java.awt.Color; import java.awt.Cursor; import java.awt.Toolkit; import javax.swing.JFrame; public class MainClass {

 public static void main(String[] args) {
   JFrame aWindow = new JFrame();
   aWindow.setBounds(200, 200, 200, 200);
   aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   aWindow.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
   aWindow.getContentPane().setBackground(Color.PINK);
   aWindow.setVisible(true);
 }

}


 </source>
   
  
 
  



Cursor.DEFAULT_CURSOR

   <source lang="java">
 

import java.awt.Container; import java.awt.Cursor; import java.awt.Point; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JViewport; import javax.swing.event.MouseInputAdapter; public class Main extends JFrame {

 public Main() {
   super("Grab and drag Demo");
   ImageIcon ii = new ImageIcon("largejexpLogo.jpg");
   JScrollPane jsp = new JScrollPane(new GrabAndScrollLabel(ii));
   getContentPane().add(jsp);
   setSize(300, 250);
   setVisible(true);
   super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 public static void main(String[] args) {
   new Main();
 }

} class GrabAndScrollLabel extends JLabel {

 public GrabAndScrollLabel(ImageIcon i) {
   super(i);
   MouseInputAdapter mia = new MouseInputAdapter() {
     int xDiff, yDiff;
     Container c;
     public void mouseDragged(MouseEvent e) {
       c = GrabAndScrollLabel.this.getParent();
       if (c instanceof JViewport) {
         JViewport jv = (JViewport) c;
         Point p = jv.getViewPosition();
         int newX = p.x - (e.getX() - xDiff);
         int newY = p.y - (e.getY() - yDiff);
         int maxX = GrabAndScrollLabel.this.getWidth() - jv.getWidth();
         int maxY = GrabAndScrollLabel.this.getHeight() - jv.getHeight();
         if (newX < 0)
           newX = 0;
         if (newX > maxX)
           newX = maxX;
         if (newY < 0)
           newY = 0;
         if (newY > maxY)
           newY = maxY;
         jv.setViewPosition(new Point(newX, newY));
       }
     }
     public void mousePressed(MouseEvent e) {
       setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
       xDiff = e.getX();
       yDiff = e.getY();
     }
     public void mouseReleased(MouseEvent e) {
       setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
     }
   };
   addMouseMotionListener(mia);
   addMouseListener(mia);
 }

}


 </source>
   
  
 
  



Cursor: getPredefinedCursor(int type)

   <source lang="java">
 

import java.awt.Color; import java.awt.Cursor; import java.awt.Toolkit; import javax.swing.JFrame; public class MainClass {

 public static void main(String[] args) {
   JFrame aWindow = new JFrame();
   aWindow.setBounds(200, 200, 200, 200);
   aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   aWindow.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
   aWindow.getContentPane().setBackground(Color.PINK);
   aWindow.setVisible(true);
 }

}


 </source>
   
  
 
  



Cursor.HAND_CURSOR

   <source lang="java">
 

import java.awt.Cursor; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Main extends JFrame {

 public static void main(String[] args) {
   SwingUtilities.invokeLater(new Runnable() {
     public void run() {
       Main mainForm = new Main();
       mainForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       mainForm.setSize(250, 250);
       Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
       mainForm.setCursor(cursor);
       mainForm.pack();
       mainForm.setVisible(true);
     }
   });
 }

}


 </source>
   
  
 
  



Cursor.MOVE_CURSOR

   <source lang="java">
 

import java.awt.Container; import java.awt.Cursor; import java.awt.Point; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JViewport; import javax.swing.event.MouseInputAdapter; public class Main extends JFrame {

 public Main() {
   super("Grab and drag Demo");
   ImageIcon ii = new ImageIcon("largejexpLogo.jpg");
   JScrollPane jsp = new JScrollPane(new GrabAndScrollLabel(ii));
   getContentPane().add(jsp);
   setSize(300, 250);
   setVisible(true);
   super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 public static void main(String[] args) {
   new Main();
 }

} class GrabAndScrollLabel extends JLabel {

 public GrabAndScrollLabel(ImageIcon i) {
   super(i);
   MouseInputAdapter mia = new MouseInputAdapter() {
     int xDiff, yDiff;
     Container c;
     public void mouseDragged(MouseEvent e) {
       c = GrabAndScrollLabel.this.getParent();
       if (c instanceof JViewport) {
         JViewport jv = (JViewport) c;
         Point p = jv.getViewPosition();
         int newX = p.x - (e.getX() - xDiff);
         int newY = p.y - (e.getY() - yDiff);
         int maxX = GrabAndScrollLabel.this.getWidth() - jv.getWidth();
         int maxY = GrabAndScrollLabel.this.getHeight() - jv.getHeight();
         if (newX < 0)
           newX = 0;
         if (newX > maxX)
           newX = maxX;
         if (newY < 0)
           newY = 0;
         if (newY > maxY)
           newY = maxY;
         jv.setViewPosition(new Point(newX, newY));
       }
     }
     public void mousePressed(MouseEvent e) {
       setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
       xDiff = e.getX();
       yDiff = e.getY();
     }
     public void mouseReleased(MouseEvent e) {
       setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
     }
   };
   addMouseMotionListener(mia);
   addMouseListener(mia);
 }

}


 </source>
   
  
 
  



Cursor.NE_RESIZE_CURSOR

   <source lang="java">
 

import java.awt.Cursor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class Main implements Runnable, ActionListener {

 private boolean animate;
 private Cursor[] cursors = new Cursor[] { Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR),
     Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR) };
 private JFrame frame;
 public Main(JFrame frame) {
   animate = false;
   this.frame = frame;
 }
 public void run() {
   int count = 0;
   while (animate) {
     try {
       Thread.currentThread().sleep(200);
     } catch (Exception ex) {
     }
     frame.setCursor(cursors[count % cursors.length]);
     count++;
   }
   frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
 }
 public void actionPerformed(ActionEvent evt) {
   JButton button = (JButton) evt.getSource();
   if (animate) {
     button.setText("Start Animation");
     animate = false;
   } else {
     animate = true;
     button.setText("Stop Animation");
     new Thread(this).start();
   }
 }
 public static void main(String[] args) {
   JFrame frame = new JFrame();
   JButton button = new JButton("Start Animation");
   button.addActionListener(new Main(frame));
   frame.getContentPane().add(button);
   frame.setSize(300, 300);
   frame.setVisible(true);
 }
 public static void p(String str) {
   System.out.println(str);
 }

}


 </source>