简体   繁体   English

Java:在JFrame中将图像添加到JPanel

[英]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. 编译器抱怨无法解决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. 问题在于该组件没有首选大小(因为您没有覆盖getPreferredSize()方法),因此没有需要绘制的内容。

Just use a JLabel with an Icon. 只需将JLabel与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. 阅读JLabel API,您将找到有关如何使用标签的Swing教程的链接,以获取更多信息。

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. 这很容易做到,您只需添加System.out.println(...)以确保正确创建了Icon。

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. 并且应该正确显示在中间的面板中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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