我正在尝试在 java 中使用 jPanel 和 jFrame 使图像可滚动。我在 drawQuestion 类中绘制图像,然后将 drawQuestion 添加到 jScrollPanel 并且它不会滚动。请告诉我我的错误在哪里我一直试图找到几天,但我仍然找不到它。对不起,我的英语不好。
主.java:
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class main{
private static int width = 800;
private static int height = 450;
public static void main(String[] args){
JFrame window = new JFrame("DPA Physics 2013 9 class");
drawQuestion question = new drawQuestion();
JScrollPane scroll = new JScrollPane(question);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(width,height);
window.setLocationRelativeTo(null);
window.add(scroll);
window.setVisible(true);
}
}
drawQuestion.java:
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class drawQuestion extends JPanel{
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g){
super.paintComponent(g);
Image image1 = new ImageIcon(this.getClass().getResource("Page1.png")).getImage();
g.drawImage(image1,0,0,this);
}
}