15.2

 1 import java.awt.Color;
 2 import java.awt.Graphics;
 3 import java.awt.GridLayout;
 4 
 5 import javax.swing.JButton;
 6 import javax.swing.JFrame;
 7 import javax.swing.JPanel;
 8 
 9 public class Test_15 extends JFrame{
10     public Test_15(){
11         setLayout(new GridLayout(1,2,5,5));
12         add(new OvalButton("OK"));
13         add(new OvalButton("CANCEL"));
14     }
15     
16     
17     public static void main(String[] args) {
18         // TODO Auto-generated method stub
19         Test_15 t = new Test_15();
20         t.setTitle("test");
21         t.setSize(400,100);
22         t.setLocationRelativeTo(null);
23         t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
24         t.setVisible(true);
25     }
26 
27 }
28 
29 class OvalButton extends JButton{
30     public String str;
31     public OvalButton(String str){
32         this.str = str;
33     }
34     protected void paintComponent(Graphics g){
35         super.paintComponent(g);
36         
37         int width = getWidth();
38         int height = getHeight();
39         g.drawString(str, width/2, height/2);
40         g.drawOval((int)(0.1*width), (int)(0.1*height), (int)(0.8*width), (int)(0.8*height));
41     }
42 }
Test_15.java
原文地址:https://www.cnblogs.com/wanjiang/p/5612426.html