📜  Java AWT工具包

📅  最后修改于: 2020-09-29 01:20:37             🧑  作者: Mango

Java AWT工具包

Toolkit类是Abstract Window Toolkit中每个实现的抽象超类。 Toolkit的子类用于绑定各种组件。它继承了Object类。

AWT Toolkit类声明

public abstract class Toolkit extends Object

Java AWT工具包示例

import java.awt.*;
public class ToolkitExample {
  public static void main(String[] args) {
  Toolkit t = Toolkit.getDefaultToolkit();
  System.out.println("Screen resolution = " + t.getScreenResolution());
  Dimension d = t.getScreenSize();
  System.out.println("Screen width = " + d.width);
  System.out.println("Screen height = " + d.height);
  }
}

输出:

Screen resolution = 96
Screen width = 1366
Screen height = 768

Java AWT Toolkit示例:beep()

import java.awt.event.*;
public class ToolkitExample {
public static void main(String[] args) {
Frame f=new Frame("ToolkitExample");
Button b=new Button("beep");
b.setBounds(50,100,60,30);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Toolkit.getDefaultToolkit().beep();
}
});
}
}

输出:

Java AWT Toolkit示例:更改TitleBar图标

import java.awt.*; 
class ToolkitExample { 
ToolkitExample(){ 
Frame f=new Frame(); 
Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");
f.setIconImage(icon);
f.setLayout(null); 
f.setSize(400,400); 
f.setVisible(true); 
} 
public static void main(String args[]){ 
new ToolkitExample(); 
} 
}

输出: