private void changeImage() throws Exception {
    JPanel imagePanel = new JPanel();
    JLabel imageLabel = new JLabel();
    imagePanel.setLayout(new GridLayout(1, 1)); //按自己需要修改
    ImageIcon icon = new ImageIcon("d:/p/5yBM_IMG_5439-bar.jpg");
    imageLabel.setIcon(icon);
    imagePanel.add(imageLabel);
    JScrollPane scrollPanel = new JScrollPane(imagePanel);
    scrollPanel.setBounds(new Rectangle(400, 400, 400, 400));
    this.getContentPane().add(imageLabel, null);
    this.getContentPane().repaint();
  }
初学GUI,啥都不懂。各位指导,指导
我是用的一个按钮,当按钮的时候,就出来一张图。怎么就没图呢,

解决方案 »

  1.   

    有些迷惑!!!!!! 上边的代码只是一小部分吧,就看这些。没有处理异常,是不是应该写一个public void actionPerformed(ActionEvent e){
        。
        。
        处理按钮事件!
        }
     我建议你还是应该从头看看,分析一下,再写编码! 
      

  2.   

    我是写在按钮的处理事件里面调的changeImage()方法
      

  3.   

    路径是不是有问题!
    d:/p/5yBM_IMG_5439-bar.jpg
    换成.class.getResource("5yBM_IMG_5439-bar.jpg")!
    注意类路径要对应!
    试试
      

  4.   

    路径没有问题,我在init里面这样写都可以读出图来
      

  5.   

    private void changeImage() throws Exception {
        JPanel imagePanel = new JPanel();
        JLabel imageLabel = new JLabel();
        imagePanel.setLayout(new GridLayout(1, 1)); //按自己需要修改
        ImageIcon icon = new ImageIcon("d:/p/5yBM_IMG_5439-bar.jpg");
        imageLabel.setIcon(icon);
        imagePanel.add(imageLabel);
        JScrollPane scrollPanel = new JScrollPane(imagePanel);
        scrollPanel.setBounds(new Rectangle(400, 400, 400, 400));
        this.getContentPane().add(imageLabel, null);
        this.getContentPane().repaint();
      }
    这什么程序,都不通
    imagePanel,scrollPanel,用了,可后来又没放到界面的面板上,而且你的主面板也不setLayout。
      

  6.   

    在最后加一句:
    getContentPane().add("Center",scrollPanel);你把那个imagePanel加入容器干嘛呢?应该把scrollPanel加入容器才能正确显示哈!