zoukankan      html  css  js  c++  java
  • 15.7

     

     1 import java.awt.*;
     2 import java.util.Scanner;
     3 
     4 import javax.swing.*;
     5 
     6 public class Test extends JFrame{
     7     
     8     public Test(){
     9         setLayout(new GridLayout(3,3,0,0));
    10         
    11         for(int i = 0; i < 9; i++)            
    12         add(new WellPanel((int) Math.round(Math.random()*2)));
    13     }
    14     
    15     public static void  main(String[] args){    
    16         Test frame = new Test();
    17         frame.setSize(400, 400);
    18         frame.setTitle("Exercise15_7");
    19         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    20         frame.setLocationRelativeTo(null); // Center the frame
    21         frame.setVisible(true);        
    22     }
    23 }
    24 
    25 class WellPanel extends JPanel{
    26     public static final int EMPTY = 0;
    27     public static final int CIRCLE = 1;
    28     public static final int FORK = 2;
    29     private int type = 1;
    30         
    31     public WellPanel(int type){
    32         this.type = type;
    33     }
    34     
    35     protected void paintComponent(Graphics g){
    36         super.paintComponent(g);
    37         
    38         switch(type)
    39         {
    40         case EMPTY:  break;
    41         case CIRCLE: g.drawOval(0,0,getWidth()-10, getHeight()-10); break;
    42         case FORK: g.drawLine(0, 0, getWidth()-10, getHeight()-10);g.drawLine(0, getHeight()-10, getWidth()-10,0); break;
    43         }
    44     }
    45     
    46     public Dimension getPreferredSize() {
    47         return new Dimension(80, 80);
    48       }
    49 }
    Test.java

    下面是效果图:

  • 相关阅读:
    Openrasp源码分析
    feifeicms后台任意文件读取
    python之迭代器和生成器
    java之导入excel
    jquery单击事件的写法
    java之高效操作文件
    多条件搜索优化sql
    java之代码复用
    java之接口文档规范
    eclipse之常用快捷键
  • 原文地址:https://www.cnblogs.com/wanjiang/p/5624015.html
Copyright © 2011-2022 走看看