Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

java - Image doesn't load inside of JScrollPane

I was developing a Swing App where I have a menu with different options, based on which the App show an image, which is contain into a JScrollPane of another JPanel.

Finally when yo make double click into the Image, appearance a description about the Image.

My problem is that when I click the Button, The ScrollPaint appears, but doesn't load the Image.

Here my code:

VentanaPrincipal

public class VentanaPrincipal extends JFrame {
public VentanaPrincipal() {
        super();
        setTitle("Formulario de Reservas – Manuel Lucas Sánchez");
        createMenu();
        initsComponents(); // Here I setUp the components.
        
    }
    public void cargaPanel(JPanel panel, String ruta) {
        scrollPane.setVisible(true);
        scrollPane.setViewportView(panel);
        
        pFoto = new PanelFoto(ruta);
        scrollPane.add(pFoto);
    }
    
    public PanelFoto getpFoto() {
        return pFoto;
    }
    public void setControlador(Controlador controlador) {
        mntmhbDoble.addActionListener(controlador);
        mntmhbPresidencial.addActionListener(controlador);
        mntmhbSimple.addActionListener(controlador);
        mntmhbSuite.addActionListener(controlador);
        mntmhbSuperior.addActionListener(controlador);
        mntmSalir.addActionListener(controlador);
        mntmseGym.addActionListener(controlador);
        mntmsePisc.addActionListener(controlador);
        mntmseRest.addActionListener(controlador);
    }
}

PanelFoto

public class PanelFoto extends JPanel {

    private Image img;
    private JTextArea txtAreaDescripcion;
    private JScrollPane conteinerFoto;
    private JLabel imageLabel;
    private ImageIcon ii;
    public PanelFoto(String ruta) {
        initsComponents(ruta);
    }
    public PanelFoto() {
        
    }
    private void initsComponents(String ruta) {
    
        setSize(550,600);
        setLayout(null);
/*
        ImageIcon ii = new ImageIcon(file_path);
        jScrollPane1 = new JScrollPane(new JLabel(ii));*/
    
        
        txtAreaDescripcion = new JTextArea();
        txtAreaDescripcion.setBounds(0, 449, 550, 151);
        add(txtAreaDescripcion);
        
        ii = new ImageIcon(ruta);
        conteinerFoto = new JScrollPane(new JLabel(ii));
        conteinerFoto.setBounds(0, 0, 550, 449);

        add(conteinerFoto);
    }

    public JTextArea getTxtAreaDescripcion() {
        return txtAreaDescripcion;
    }
    
    public JScrollPane getConteinerFoto() {
        return conteinerFoto;
    }
    
    public void setControlador(Controlador controlador) {
        this.addMouseListener((MouseListener) controlador);
    }

    public Image getImg() {
        return img;
    }

    public void setImg(Image img) {
        this.img = img;
    }
}

Controlador

public class Controlador implements ActionListener, MouseListener {

    private VentanaPrincipal vPrincipal;
    private PanelFoto pFoto;
    
    
    public Controlador(VentanaPrincipal vPrincipal, PanelFoto pFoto) {
        this.vPrincipal=vPrincipal;
        this.pFoto=pFoto;
    }
    
    @Override
    public void actionPerformed(ActionEvent ae) {
        
        if(ae.getSource() instanceof JMenu || ae.getSource() instanceof JMenuItem) {
            
            if(((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_HABSIMPLE)) {
                vPrincipal.cargaPanel(pFoto, "img/simple.jpg");
                vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getSource().equals(vPrincipal.getpFoto())) {
                            vPrincipal.getpFoto().getTxtAreaDescripcion().setText("Superficie de la habitacion: 20 metros
" + 
                                    "Ducha
" + 
                                    "Toallas
" + 
                                    "Secador de  pelo
" + 
                                    "TV
" + 
                                    "Politica de humo: no se puede fumar
" + 
                                    "");
                        }
                    }
                });
                Image img = null;
                try {
                    img = ImageIO.read(new File("img/simple.jpg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //ImageIcon ImgIcon = new ImageIcon(Img);
                pFoto.setImg(img);
                
            }else if (((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_HABDOBLE)) {
                    vPrincipal.cargaPanel(pFoto, "img/doble.jpg");
                    
                    vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getSource().equals(vPrincipal.getpFoto())) {
                            vPrincipal.getpFoto().getTxtAreaDescripcion().setText("Superficie de la habitacion: 30 m
" + 
                                    "Ducha 
" + 
                                    "Toallas y albornoces
" + 
                                    "Secador de  pelo
" + 
                                    "TV LED
" + 
                                    "Dos camas dobles");
                        }
                    }
                });
                Image Img = null;
                try {
                    Img = ImageIO.read(new File("img/doble.jpg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //ImageIcon ImgIcon = new ImageIcon(Img);
                pFoto.setImg(Img);
                
            }else if (((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_HABSUITE)) {
                vPrincipal.cargaPanel(pFoto, "img/suite.jpg");
vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getSource().equals(vPrincipal.getpFoto())) {
                            vPrincipal.getpFoto().getTxtAreaDescripcion().setText("Superficie de la habitacion: 50 metros
" + 
                                    "Banera hidromasaje
" + 
                                    "Todo tipo de amenities
" + 
                                    "Caja fuerte
" + 
                                    "Terraza vistas mar
" + 
                                    "Cama King Size");
                        }
                    }
                });
                Image Img = null;
                try {
                    Img = ImageIO.read(new File("img/suite.jpg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //ImageIcon ImgIcon = new ImageIcon(Img);
                pFoto.setImg(Img);
            }else if (((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_HABPRESI)) {
                vPrincipal.cargaPanel(pFoto, "img/presidencial.jpg");
                    vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getSource().equals(vPrincipal.getpFoto())) {
                            vPrincipal.getpFoto().getTxtAreaDescripcion().setText("Superficie de la habitacion: 90 metros
" + 
                                    "SPA privado
" + 
                                    "Servicio de habitacion personal
" + 
                                    "Jardin privado con piscina
" + 
                                    "HomeCinema
" + 
                                    "Cama King Size");
                        }
                        
                    }
                });
                Image Img = null;
                try {
                    Img = ImageIO.read(new File("img/presidencial.jpg"));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //ImageIcon ImgIcon = new ImageIcon(Img);
                pFoto.setImg(Img);
                
            }else if (((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_SERREST)) {
                vPrincipal.cargaPanel(pFoto, "img/restaurante.jpg");
    vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
        

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...