日期:2014-05-20 浏览次数:20720 次
package com.han; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JFrame; public class Graphics_1 extends JFrame { /** * */ private static final long serialVersionUID = -5861565474703590207L; JButton button = new JButton("JButton"); Container container; public Graphics_1() { // TODO Auto-generated constructor stub container = getContentPane(); container.setLayout(new FlowLayout()); container.add(button); } void paintJButton() { /* * Creates a graphics context for Container (you can also try it for * JFrame) */ Graphics g = container.getGraphics(); g.setColor(Color.BLACK); /* * Draws a line, using the current color, between the points (x1, y1) * and (x2, y2) in this graphics context's coordinate system. */ g.drawLine(5, 5, 15, 5); System.out.println("here"); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub final Graphics_1 frame = new Graphics_1(); frame.setTitle("Painting figures on the JButton"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(100, 100, 300, 200); frame.setVisible(true); try { Thread.sleep(100); // Thread.sleep(20); does not work ! } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } frame.paintJButton(); } }