Java/Development Class/Toolkit

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

Centering a Frame, Window, or Dialog on the Screen

  
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    JFrame window = new JFrame();
    window.setSize(300,300);
    
    int w = window.getSize().width;
    int h = window.getSize().height;
    int x = (dim.width - w) / 2;
    int y = (dim.height - h) / 2;
    window.setLocation(x, y);
    window.setVisible(true);
  }
}





Check Desktop Property by using the Toolkit.getDefaultToolkit()

  
import java.awt.Toolkit;
public class DynamicLayout {
  public static void main(String[] args) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Object prop = tk.getDesktopProperty("awt.dynamicLayoutSupported");
    System.out.println(tk.isDynamicLayoutActive() ? "yes" : "no");
    if (tk.isDynamicLayoutActive())
      tk.setDynamicLayout(false);
    else
      tk.setDynamicLayout(true);
    System.out.println(tk.isDynamicLayoutActive() ? "yes" : "no");
  }
}





Getting the Screen Size

  
import java.awt.Dimension;
import java.awt.Toolkit;
public class Main {
  public static void main(String[] argv) throws Exception {
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    System.out.println(dim);
  }
}





Use the getResourceAsStream method

  
import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.InputStream;
public class Main {
  public static void main(String[] argv) throws Exception {
    InputStream is = Main.class.getResourceAsStream("image.gif");
    BufferedInputStream bis = new BufferedInputStream(is);
    byte[] byBuf = new byte[10000];
    int byteRead = bis.read(byBuf, 0, 10000);
    Image img = Toolkit.getDefaultToolkit().createImage(byBuf);
  }
}