• GUI 下


    11.6 Swing组件

    JButton JLabel JTextField JTextArea JTable JTree

    publicclass JTableDemo extends JFrame

    {

          publicstaticvoid main(String[] args)

          {

               JTableDemo jTableDemo = new JTableDemo();

               jTableDemo.setVisible(true);

          }

          public JTableDemo()

          {

               setSize(300, 300);

               setLocation(400, 400);

               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

               setLayout(new BorderLayout());

               JTable table = new JTable(new AbstractTableModel()

               {

                     privatestaticfinallongserialVersionUID = 1L;

                     private String columnName[] =

                     { "第一列", "第二列", "第三列", "第四列" };

                     // 返回列的大小

                     @Override

                     publicint getColumnCount()

                     {

                          return 4;

                     }

                     // 返回列名

                     @Override

                     public String getColumnName(intcolumn)

                     {

                          returnthis.columnName[column];

                     }

                     // 返回行的大小

                     @Override

                     publicint getRowCount()

                     {

                          return 4;

                     }

                     public Object getValueAt(introw, intcol)

                     {

                          returnnew Integer(row * col);

                     }

               });

               // 为了防止表格过长,使用JScrollPane,使得表格具有滚动条

               JScrollPane scrollPane = new JScrollPane(table);

               add(scrollPane, BorderLayout.CENTER);

          }}

    11.7 图形编程

    11.7.1 AWT图形API

    Graphics 类是从JDK1.0开始就在AWT中提供的图形绘制类.由于Swing是基于AWT发展起来的组件,Graphics类作为一种图形绘制方式得到了保留

    drawLine

    绘制直线

    DrawOval

    fillOval

    绘制椭圆

    填充椭圆

    DrawPolygon

    FillPolygon

    绘制多边形

    填充多边形

    drawRect

    fillRect

    绘制矩形

    填充矩形

    drawRoundRect

    fillRoundRect

    绘制圆角矩阵

    填充圆角矩阵

    drawstring

    绘制字符串

    Draw3DRect

    Fill3DRect

    绘制带3D效果的矩形

    填充带3D效果的矩形

    drawImage

    绘制图片

    SetColor

    设置画笔颜色

    SetFont

    设置字体

    publicclass GraphicsDemo extends JFrame

    {

          publicstaticvoid main(String[] args)

          {

               GraphicsDemo graphicsDemo = new GraphicsDemo();

               graphicsDemo.setVisible(true);

          }

          public GraphicsDemo()

          {

               setSize(400, 400);

               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          }

          @Override

          publicvoid paint(Graphics g)

          {

               super.paint(g);

               // 设置画笔颜色为红色

          g.setColor(Color.red);

               // 绘制矩形

               g.drawRect(50, 50, 100, 100);

               // 填充矩形

               g.fillRect(200, 50, 100, 100);

               // 设置画笔颜色为绿色

               g.setColor(Color.green);

               // 绘制圆形

               g.drawOval(50, 200, 100, 100);

               // 填充圆形

               g.fillOval(200, 200, 100, 100);

          }}

    11.7.2 Java2D 图形API

     Java2D JFC的一员,加强了传统的AWT的描绘功能.

          绘制五角形代码如下

          publicclass Graphics2DDemo extends JFrame

    {

          publicstaticvoid main(String[] args)

          {

               Graphics2DDemo myFrame = new Graphics2DDemo();

               myFrame.setVisible(true);

          }

     

          public Graphics2DDemo()

          {

               setSize(400, 400);

               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          }

          @Override

          publicvoid paint(Graphics g)

          {

               super.paint(g);

               Graphics2D g2 = (Graphics2D) g;

               // 设置画笔样式

               g2.setStroke(new BasicStroke(4.0f));

               // 用GeneralPath 构造一个五角形

               GeneralPath p = new GeneralPath(GeneralPath.WIND_NON_ZERO);

               p.moveTo(-100.0f, -25.0f);

               p.lineTo(+100.0f, -25.0f);

               p.lineTo(-50.0f, +100.0f);

               p.lineTo(+0.0f, -100.0f);

               p.lineTo(+50.0f, +100.0f);

               p.closePath();

               // 将坐标平移到(200,200)

               g2.translate(200.0f, 200.0f);

               g2.draw(p);

          }}

    11.8 加载和使用多媒体资源

    11.8.1 加载图像

          现在我使用一种基于ImageIO的图像加载和使用方法.ImageIo中关于图像加载的方法主要有以下的方法

          BufferedImage read(File input)

          BufferedImage read(ImageInputStream stream)

          BufferedImage read(InputStream input)

          BufferedImage read(URL input)

     

  • 相关阅读:
    Mac 实用工具——迁移助理
    Mac OS 的终端工具 iTerm2 的使用及设置
    Python3的异常捕获和处理
    Python3 文件的重命名
    Linux下jetty的启动和停止
    MySQL使用select查询时,在查询结果中增加一个字段并指定固定值
    使用ThreadLocal请务必remove
    Vue基础-文本显示,v-html插入html代码
    nginx之location(root/alias)&& linux 上修改了nginx.conf 怎么重新加载配置文件生效
    CentOS7开启防火墙及特定端口
  • 原文地址:https://www.cnblogs.com/qingtianBKY/p/6069953.html
Copyright © 2020-2023  润新知