awt frame - java 窗口
Posted 荒野小肥羊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了awt frame - java 窗口相关的知识,希望对你有一定的参考价值。
1 package com.law.frame; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Frame; 6 import java.awt.Graphics; 7 import java.awt.Image; 8 import java.awt.event.WindowAdapter; 9 import java.awt.event.WindowEvent; 10 import java.net.URL; 11 12 public class MyWindow extends Frame{ 13 14 /** 15 * 16 */ 17 private static final long serialVersionUID = 2490169038285678148L; 18 19 private int x; 20 private int y; 21 private boolean flag; 22 23 24 public MyWindow(){ 25 26 } 27 public MyWindow(String title){ 28 super(title); 29 } 30 31 public void openMyWindow(){ 32 setBackground(new Color(0, 0, 0)); 33 setSize(500, 500); 34 setLocation(250, 250); 35 setVisible(true); 36 this.addWindowListener(new WindowAdapter() { 37 public void windowClosing(WindowEvent e){ 38 System.exit(0); 39 } 40 }); 41 new RepaintThread().start(); 42 } 43 44 45 46 @Override 47 public void paint(Graphics g) { 48 g.setColor(Color.white); 49 Font font = new Font("Candara", Font.BOLD, 30); 50 g.setFont(font); 51 g.drawString("Java Concurrency", 130, 150); 52 URL u = this.getClass().getClassLoader().getResource("images/Plane_32px_1194817_easyicon.net.png"); 53 Image img = Common.loadPic(u); 54 g.drawImage(img, x, 350, null); 55 } 56 57 class RepaintThread extends Thread{ 58 59 @Override 60 public void run() { 61 while(true){ 62 changePositionX(); 63 repaint(); 64 try { 65 Thread.sleep(60); 66 } catch (InterruptedException e) { 67 e.printStackTrace(); 68 } 69 } 70 } 71 72 } 73 public void changePositionX(){ 74 if(x>=500-32){ 75 flag = false; 76 } 77 if(x<=0){ 78 flag = true; 79 } 80 if(flag){ 81 x += 5; 82 }else{ 83 x -= 5; 84 } 85 } 86 public void changePositionY(){ 87 if(y>=500-32){ 88 flag = false; 89 } 90 if(y<=0){ 91 flag = true; 92 } 93 if(flag){ 94 y += 5; 95 }else{ 96 y -= 5; 97 } 98 } 99 public static void main(String[] args) { 100 MyWindow window = new MyWindow("open open"); 101 window.openMyWindow(); 102 } 103 104 public int getX() { 105 return x; 106 } 107 public void setX(int x) { 108 this.x = x; 109 } 110 public int getY() { 111 return y; 112 } 113 public void setY(int y) { 114 this.y = y; 115 } 116 public boolean isFlag() { 117 return flag; 118 } 119 public void setFlag(boolean flag) { 120 this.flag = flag; 121 } 122 123 }
加载图片的工具类:
1 package com.law.frame; 2 3 import java.awt.Image; 4 import java.io.IOException; 5 import java.net.URL; 6 7 import javax.imageio.ImageIO; 8 9 10 public class Common { 11 12 13 public static Image loadPic(URL u){ 14 Image img = null; 15 try { 16 img = ImageIO.read(u); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } 20 return img; 21 } 22 }
以上是关于awt frame - java 窗口的主要内容,如果未能解决你的问题,请参考以下文章
Java AWT 图形界面编程Frame 窗口标题栏大小问题 ( Container 容器的空白边框 Insets | 通过调用 frame.getInsets().top 获取窗口标题栏高度 )
Java AWT 图形界面编程Container 容器 ② ( Frame 窗口示例 | Panel 示例 | 窗口中文乱码处理 )
Java AWT 图形界面编程Frame 窗口中进行自定义布局 ( AWT 中常用的布局容器 )
Java AWT 图形界面编程设置窗口图标 ( IntelliJ IDEA 的图标资源位置 | 代码实现 | exe4j 设置导出 exe 程序对应的图标资源位置 )
Java AWT 图形界面编程Dialog 对话框 ( 简介 | 模式对话框 | 非模式对话框 | Dialog 构造函数 | Dialog 代码示例 | 向 Dialog 对话框添加布局组件 )