本帖最后由 derrick5335 于 2010-05-08 18:49:25 编辑

解决方案 »

  1.   


    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        Circle c = new Circle();
        c.setLocation(30, 30);
        c.setSize(530, 160);
        jPanel1.add(c);
        ××Frame.this.pack();
    }   
      

  2.   

    你修改了窗体中的组件后需要重新通知布局管理器为所有组件重新布局(调用JFrame中的pack()方法),不然新加入的组件无法显示
      

  3.   

    public class MyFrame extends JFrame
    {
    /////////...................... private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
      Circle c = new Circle();
      c.setLocation(30, 30);
      c.setSize(530, 160);
      jPanel1.add(c);
    pack();  
    }  

    ////////......................}
      

  4.   


    试过了, 还是不行, 仍然是能够画出来circle的panel, 但是显示不出来那个图片.
      

  5.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class RotateImage extends JFrame implements ActionListener{    MyPanel jpanel1 = new MyPanel();
    JButton b = new JButton("test");
        public RotateImage() {
         add(b, BorderLayout.NORTH);
         b.addActionListener(this);
            setSize(400, 400);
            //setPreferredSize(new Dimension(400, 400));
            //pack();
            setVisible(true);
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        
        public void actionPerformed(ActionEvent evt) {                                              
            Circle c = new Circle();
            c.setLocation(30, 30);
            c.setSize(530, 160);
            setVisible(false);
            add(jpanel1);
            setVisible(true);
            //pack();
        }           public class MyPanel extends JPanel {        ImageIcon image = new ImageIcon("Sample.jpg");        public MyPanel() {
                super();
            }        @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.drawImage(image.getImage(), 0, 0, this);
                g2d.dispose();
            }
        }    public static void main(String[] args) {
            new RotateImage();
        }
    }
      

  6.   

    或者下面这种也行
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class RotateImage extends JFrame implements ActionListener{    MyPanel jpanel1 = new MyPanel();
    JButton b = new JButton("test");
        public RotateImage() {
         add(b, BorderLayout.NORTH);
         b.addActionListener(this);
            //setSize(400, 400);
            setPreferredSize(new Dimension(400, 400));
            pack();
            setVisible(true);
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        
        public void actionPerformed(ActionEvent evt) {                                              
            Circle c = new Circle();
            c.setLocation(30, 30);
            c.setSize(530, 160);
            //setVisible(false);
            add(jpanel1);
            //setVisible(true);
            pack();
        }           public class MyPanel extends JPanel {        ImageIcon image = new ImageIcon("Sample.jpg");        public MyPanel() {
                super();
            }        @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.drawImage(image.getImage(), 0, 0, this);
                g2d.dispose();
            }
        }    public static void main(String[] args) {
            new RotateImage();
        }
    }
      

  7.   

    pack()是调用布局管理器的方法,不知道你那里为啥结果不对
      

  8.   

    你可先试试去掉button的控制,直接在构造的时候将图片放上去。
    如果还是不行的话,我觉得可能是图片路径的问题。
    如果可以,则可能是重绘和布局的问题。
      

  9.   

    package com.snail.test;import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.*;public class Circle extends JPanel {
        private int positionX = 50;
        private int positionY = 50;
        ImageIcon image = new ImageIcon("D:/mm.jpg");    public Circle() {
            super();
        }    @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawImage(image.getImage(), positionX, positionY, this);
            g2d.dispose();
        }
    }
    class RotateImage extends JFrame implements ActionListener {    MyPanel imgpanel = new MyPanel();
        JPanel panel = new JPanel();    public RotateImage() {
            setSize(400, 400);
            JButton button = new JButton("Show");
            panel.setLayout(null);
            button.setBounds(300, 20, 80, 20);
            button.addActionListener(this);
            panel.add(button);
            
            setContentPane(panel);
            setVisible(true);
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }    public class MyPanel extends JPanel {        ImageIcon image = new ImageIcon("D:/mm.jpg");        public MyPanel() {
                super();
            }        @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.drawImage(image.getImage(), 0, 0, this);
                g2d.dispose();
            }
        }    public static void main(String[] args) {
            new RotateImage();
        } @Override
    public void actionPerformed(ActionEvent e) {
    System.out.println("Test");
    Circle c = new Circle();
            c.setLocation(30, 30);
            c.setSize(530, 160);
            panel.add(c);
    //        pack();
            this.repaint();//整个Frame重绘下就可以了 
    }
    }
      

  10.   

    我用你的代码测试了一下, 和我有一样的问题, 按了按钮之后, 可以打印出test, 但是是不能显示图片的...
      

  11.   

    图片没出来、改成下面的,就显示出来了、我已经测试了
    ImageIcon image = new javax.swing.ImageIcon(getClass().getResource("/Sample.jpg"));