简体   繁体   中英

Java: Adding an image to a JPanel in a JFrame

I took some suggestions and have rewritten some of the code to implement the suggestions made and to make it more readable. Now it won't compile. The compiler is complaining that it can't resolve the constructor on the JLabel. I made a comment where the issue is.

/**
 * Created with IntelliJ IDEA.
 * User: goulartj
 * Date: 9/4/13
 * Time: 10:11 AM
 * To change this template use File | Settings | File Templates.
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class NewSwing implements ActionListener{

    JFrame frame;
    JTextField textField;
    JTextArea textArea;
    JPanel panel;
    Image image;
    JLabel label;
    private final static String newline = "\n";

    public static void main(String[] args) {
        NewSwing gui = new NewSwing();
        gui.go();
    }

    public void go(){

        frame = new JFrame();
        frame.getContentPane().setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        textField = new JTextField("This is a text field and these are my texticles!");
        textField.selectAll();
        textArea = new JTextArea();
        panel = new JPanel();
        image = new ImageIcon("cuteKitten.jpg").getImage();
        label = new JLabel(image); //COMPILER COMPLAINS HERE
        panel.add(label);


        frame.getContentPane().add(BorderLayout.NORTH, textField);
        frame.getContentPane().add(BorderLayout.CENTER, textArea);
        frame.getContentPane().add(BorderLayout.EAST, panel);
        panel.setBackground(Color.CYAN);




        textField.addActionListener(this);

    }

    public void actionPerformed(ActionEvent event){
        String text = textField.getText();
        textArea.append(text + newline);
        textField.selectAll();
    }

   /* class MyDrawPanel extends JPanel {
        public void paintComponent(Graphics g) {
            g.drawImage(image, 3, 4, this);
        }
    }     */
}

Thanks in advance for all the help! You guys always treat me so well!

Why are you doing custom painting to display an image???

The problem is that the component doesn't have a preferred size (since you didn't override the getPreferredSize() method) so there is nothing to paint.

Just use a JLabel with an Icon. It will look after painting the icon and determining the proper size. Don't reinvent the wheel. Read the JLabel API and you will find a link to the Swing tutorial on how to use labels for more information.

Of course you also need to make sure you are reading the image. That is easy enough to do you just add a System.out.println(...) to make sure the Icon is created properly.

It is showing up, but it's located at the edge of the screen. Just change:

frame.getContentPane().add(BorderLayout.EAST, panel);

to

frame.getContentPane().add(BorderLayout.CENTER, panel);

and it should display properly in the panel in the center.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM