/**
* 重绘DIALOG
*/
public void paint(Graphics g) {
super.paint(g);
this.getGraphics().drawImage(img1, 40, 25, imageLength, imageWidth, this);
}
this指你所需要的那个PANEL对象
不能放在scrollPane里,要放在scrollPane所setViewportView()的对象里!
做了个小例子给你参考。
import java.awt.*;
import javax.swing.*;
class DrawPanel extends JPanel{
public DrawPanel() {
setPreferredSize(new Dimension(200,200));
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.drawOval(100, 100, 1800, 800);
}
}
public class MyFrame extends JFrame{
public static void main(String[] args){
MyFrame demo = new MyFrame();
DrawPanel drawPanel = new DrawPanel();
JScrollPane scrollPane = new JScrollPane(drawPanel);
scrollPane.setSize(500,768);
demo.getContentPane().add(scrollPane,BorderLayout.CENTER);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
demo.setSize(new Dimension(800,600));
demo.setVisible(true);
}
}
paint是在panel里画的,别放scrollPane的paint里画。