problem in adding image to JFrame

佐手、 提交于 2019-12-06 11:13:52
Mike Caron

There's several issues with the code you've posted:

  • You should use getContentPane().add() instead of simply add() in your BorderLayoutFrame class.
  • You should really use SwingUtilities.invokeLater() to launch your JFrame from the tester class. Something like this:

 SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        System.setProperty("DEBUG_UI", "true");

        BorderLayoutFrame blf = new BorderLayoutFrame();
        blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        blf.setSize(600,600);
        blf.setVisible(true);
    }
});
  • Don't use Toolkit to load images! In the following code, if "Table.jpg" is in the same package as PicPanel, the image will correctly load.

public PicPanel() {
    super();
    try {
        rUrl = getClass().getResource("Table.jpg");
        if (rUrl != null) {
            img = ImageIO.read(rUrl);
        }
    } catch (IOException ex) {
        Logger.getLogger(PicPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}
  • In PicPanel.PaintComponent() you call super.paintComponents() is the 's' a typeo?
  • In PicPanel.PaintComponent(), you don't need all the width/height stuff, just do this:

    g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

And avoid the call to super.paintComponent all together because you're painting an image, why do you want the panel to paint at all?

My final implementation of your stuff:

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                System.setProperty("DEBUG_UI", "true");

                BorderLayoutFrame blf = new BorderLayoutFrame();
                blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                blf.setSize(600,600);
                blf.setVisible(true);
            }
        });
    }

}

class BorderLayoutFrame extends JFrame implements ActionListener
{
    private final BorderLayout layout;
    private final JButton[] buttons;
    private final String names[] = {"North", "South", "East", "West", "Center"};

    public BorderLayoutFrame() {
        super( "Philosofic Problem" );
        layout = new BorderLayout( 5, 5 );
        getContentPane().setLayout( layout );
        buttons = new JButton[ names.length ];

        for (int i=0; i<names.length; i++)
        {
            buttons[i] = new JButton(names[i]);
            buttons[i].addActionListener(this);
        }

        getContentPane().add(buttons[0], BorderLayout.NORTH);
        getContentPane().add(buttons[1], BorderLayout.SOUTH);
        getContentPane().add(buttons[2], BorderLayout.EAST);
        getContentPane().add(buttons[3], BorderLayout.WEST);
        getContentPane().add(new PicPanel(), BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e) {
        // ignore
    }

}

class PicPanel extends JPanel
{
    private URL rUrl;
    private BufferedImage img;



    public PicPanel() {
        super();
        try {
            rUrl = getClass().getResource("UtilBtn.png");
            if (rUrl != null) {
                img = ImageIO.read(rUrl);
            }
        } catch (IOException ex) {
            Logger.getLogger(PicPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        //super.paintComponent(g);

        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    }

}
jjnguy

In Eclipse, if you are using the path "table.jpg" then you need that image to be at the top level of the project.

For a bit more info on the Eclipse project structure see this Stack Overflow question.

You may want to try using an ImageIcon to display the image instead of drawing it on the panel.

The following code will create an ImageIcon that you can just add to the JPanel.

ImageIcon pic = new ImageIcon(getClass().getResource("myimage.jpeg");

This uses a fancy way to load the pic so that it will work when put into a Jar file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!