• 图形界面中包括了事件处理


    下面语句导入Swing包

    importjavax.swing.*;

    大部分Swing程序用到了AWT的基础底层结构和事件模型,因此需要导入两个包:

    importjava.awt.*;

    importjava.awt.event.*;

    如果图形界面中包括了事件处理,那么还需要导入事件处理包:

    importjavax.swing.event.*;

     

     1 package Com.SwingTest;
     2 
     3 
     4 import java.awt.Component;
     5 import java.awt.Container;
     6 import java.awt.Graphics;
     7 
     8 import javax.swing.Icon;
     9 import javax.swing.JFrame;
    10 import javax.swing.JLabel;
    11 import javax.swing.SwingConstants;
    12 import javax.swing.WindowConstants;
    13 
    14 public class SwingText_06 extends JFrame implements Icon {    
    15 
    16     private int width;        // 声明图标的宽
    17     private int height;        // 声明图标的长
    18     
    19     public SwingText_06() {}        // 定义无参构造方法
    20     
    21     public SwingText_06(int width, int height) {        // 定义有参构造方法
    22         this.width = width;
    23         this.height = height;
    24     }
    25     
    26     @Override
    27     public int getIconHeight() {        // 实现getIconHeight()方法
    28         return this.height;
    29     }
    30 
    31     @Override
    32     public int getIconWidth() {            // 实现getIconWidth()方法
    33         return this.width;
    34     }
    35 
    36     @Override
    37     public void paintIcon(Component arg0, Graphics arg1, int arg2, int arg3) {        // 实现paintIcon()方法
    38         arg1.fillOval(arg2, arg3, width, height);        // 绘制一个圆形
    39     }
    40 
    41     public void init() {    // 定义一个方法用于实现界面
    42         SwingText_06 iconDemo = new SwingText_06(15, 15);        // 定义图标的长和宽
    43         JLabel jb = new JLabel("borter", iconDemo, SwingConstants.CENTER);    // 设置标签上的文字在标签正中间
    44         
    45         Container container = getContentPane();
    46         container.add(jb);
    47         
    48         this.setVisible(true);
    49         this.setSize(500, 350);
    50         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    51     }
    52     
    53     public static void main(String[] args) {
    54         new SwingText_06().init();
    55     }
    56 
    57 }
  • 相关阅读:
    【LeetCode-树】二叉搜索树中第K小的元素
    【机器学习-评估方法】模型评估方法
    【LeetCode-树】二叉搜索树的最小绝对差
    【LeetCode-树】检查平衡性
    【LeetCode-树】验证二叉搜索树
    【LeetCode-回溯/动态规划】不同路径
    Vue——风格指南
    潜在危险
    渲染HTML的方式
    Vue——状态管理
  • 原文地址:https://www.cnblogs.com/borter/p/9396223.html
Copyright © 2020-2023  润新知