Java Tutorial/Development/Toolkit — различия между версиями

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

Версия 20:44, 31 мая 2010

Centering a Frame, Window, or Dialog on the Screen

   <source lang="java">

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

}</source>





Getting the Screen Size

   <source lang="java">

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

}</source>





java.awt.Toolkit

The Toolkit abstract class has the following methods:

  1. public static Toolkit getDefaultToolkit(): Returns the default implementation of the Toolkit class.
  2. public abstract void beep (): Produces a beep sound.
  3. public abstract Dimension getScreenSize (): Returns a java.awt.Dimension object containing the width and the height of the screen.