import javax.swing.*;
import java.awt.*;
import java.awt.Container;
public class ERER{
JPanel pp;
public static void main(String [] args){
ERER cc=new ERER();
cc.go();
}
public void go(){
JFrame frame=new JFrame();
JPanel pp=new JPanel();
//JButton button=new JButton("cao!!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel=new MyDrawPanel();
JScrollPane scroller=new JScrollPane(drawPanel);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
frame.setSize(600,600);
frame.setVisible(true);
}
class MyDrawPanel extends JPanel{
public void paintComponent(Graphics g){
Image img=new ImageIcon("C:/Users/Job/Documents/Tencent Files/317867865/FileRecv/P1018233.jpg").getImage();
g.drawImage(img,0,0,800,800,this);
}
}
}
图片显示出来后没有滚动条 怎么能加上滚动条
求大神帮忙 在线等
------解决方案--------------------
你要重写JPanel的getPreferredSize方法才行。要设置大小的。点这里