java中我想要在frame里的scrollPane里的panel上画图

2025-01-24 21:18:52
推荐回答(3个)
回答1:

/**
* 重绘DIALOG
*/
public void paint(Graphics g) {
super.paint(g);
this.getGraphics().drawImage(img1, 40, 25, imageLength, imageWidth, this);
}
this指你所需要的那个PANEL对象

不能放在scrollPane里,要放在scrollPane所setViewportView()的对象里!

回答2:

做了个小例子给你参考。

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);

}

}

回答3:

paint是在panel里画的,别放scrollPane的paint里画。