![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
下面是效果图: