62,614
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
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");
@SuppressWarnings("serial")
Container container = new Container() {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(5, 5, 15, 5);
}
};
public Graphics_1() {
// TODO Auto-generated constructor stub
this.add(container);
container.setLayout(new FlowLayout());
container.add(button);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Graphics_1 frame = new Graphics_1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 300, 200);
frame.setVisible(true);
}
}