package com.SwingTest;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Line2D;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class imageTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("绘制示例");
frame.add(new drawJP());
frame.setBounds(300,200,450,400);
frame.setResizable(false);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class drawJP extends JPanel{
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.BLACK);
g2.drawLine(3, 3, 65, 80);
}
}
为什么运行看不到画出来的线段
imageTest不应该作为主类,单独写,这个类是实现在一个窗口(window extends Frame)里面添加画板的,即drawJP,然后在写一个public class,实例化一个window。你可以试下