注册 登录
编程论坛 JAVA论坛

如何将滚动条画在Jpanel上

a3575181 发布于 2017-04-19 22:06, 1157 次点击
RT,在面板画了很多图片,然后窗口空间有限无法全部显示,可不可以加个滚动条

只有本站会员才能查看附件,请 登录



程序代码:
package midterm;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;


public class photo extends JFrame implements ActionListener{

    JScrollPane jsp ;

    JPanel showjp = new JPanel();
   
    public photo(){
        ImageIcon icon0 = new ImageIcon("0.jpg");
        ImageIcon icon1 = new ImageIcon("1.jpg");
        ImageIcon icon2 = new ImageIcon("2.jpg");
        ImageIcon icon3 = new ImageIcon("3.jpg");
        ImageIcon icon4 = new ImageIcon("4.jpg");
        ImageIcon icon5 = new ImageIcon("5.jpg");
        ImageIcon icon6 = new ImageIcon("6.jpg");
        ImageIcon icon7 = new ImageIcon("7.jpg");
        ImageIcon icon8 = new ImageIcon("8.jpg");
        ImageIcon icon9 = new ImageIcon("9.jpg");
        
        JLabel jl0 = new JLabel(icon0);
        JLabel jl1 = new JLabel(icon1);
        JLabel jl2 = new JLabel(icon2);
        JLabel jl3 = new JLabel(icon3);
        JLabel jl4 = new JLabel(icon4);
        JLabel jl5 = new JLabel(icon5);
        JLabel jl6 = new JLabel(icon6);
        JLabel jl7 = new JLabel(icon7);
        JLabel jl8 = new JLabel(icon8);
        JLabel jl9 = new JLabel(icon9);
        
        
        showjp.add(jl0);
        showjp.add(jl1);
        showjp.add(jl2);
        showjp.add(jl3);
        showjp.add(jl4);
        showjp.add(jl5);
        showjp.add(jl6);
        showjp.add(jl7);
        showjp.add(jl8);
        showjp.add(jl9);
        
        jsp = new JScrollPane();
        
        this.add(jsp);

        this.add(showjp);
        this.add("Center", showjp);
        this.setTitle("photo");
        this.setSize(400, 300);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        //this.pack();
   
        
        
    }
    public static void main(String[] args) {
        photo win = new photo();
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        
    }}
     




[此贴子已经被作者于2017-4-19 23:48编辑过]

1 回复
#2
wlrjgzs2017-04-20 12:23
可以加滚动条
1