Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我试图添加JFrame背景图像,但它没有在java中显示_Java_Image_Swing_Jframe_Jpanel - Fatal编程技术网

我试图添加JFrame背景图像,但它没有在java中显示

我试图添加JFrame背景图像,但它没有在java中显示,java,image,swing,jframe,jpanel,Java,Image,Swing,Jframe,Jpanel,我正在尝试将背景图像添加到JFrame public class Triangle { Oval o; public static void main(String[] args) { new Triangle(); } public Triangle() { EventQueue.invokeLater(new Runnable() { @Override public void

我正在尝试将背景图像添加到JFrame

public class Triangle {
    Oval o;

    public static void main(String[] args) {
        new Triangle();
    }

    public Triangle() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.setSize(400, 400);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {
        private Polygon poly;

        {
            BufferedImage img = null;
            try {
                img = ImageIO.read(new File("apple.PNG"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Image dimg = img.getScaledInstance(800, 508, Image.SCALE_SMOOTH);
            ImageIcon imageIcon = new ImageIcon(dimg);
            setContentPane(new JLabel(imageIcon));

            poly = new Polygon(
                    new int[] { 110, 150, 50 },
                    new int[] { 200, 0, 0 },
                    3);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        private void setContentPane(JLabel jLabel) {
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.blue);
            g2.fill(poly);
            g2.setColor(Color.red);
            g2.fillRect(0, 0, 25, 75);
            g2.setColor(Color.green);
            g2.fillOval(200, 100, 100, 50);
            g2.setColor(Color.green);
            g2.fillOval(300, 300, 250, 250);
        }
    }
}
我尝试了所有可能的解决方案,但都不起作用,我只想在JFrame中添加一个背景图像,然后在其中打印形状,正如您在代码中看到的那样

public class Triangle {
    Oval o;

    public static void main(String[] args) {
        new Triangle();
    }

    public Triangle() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.setSize(400, 400);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {
        private Polygon poly;

        {
            BufferedImage img = null;
            try {
                img = ImageIO.read(new File("apple.PNG"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Image dimg = img.getScaledInstance(800, 508, Image.SCALE_SMOOTH);
            ImageIcon imageIcon = new ImageIcon(dimg);
            setContentPane(new JLabel(imageIcon));

            poly = new Polygon(
                    new int[] { 110, 150, 50 },
                    new int[] { 200, 0, 0 },
                    3);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        private void setContentPane(JLabel jLabel) {
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.blue);
            g2.fill(poly);
            g2.setColor(Color.red);
            g2.fillRect(0, 0, 25, 75);
            g2.setColor(Color.green);
            g2.fillOval(200, 100, 100, 50);
            g2.setColor(Color.green);
            g2.fillOval(300, 300, 250, 250);
        }
    }
}
有人能帮我修一下吗

public class Triangle {
    Oval o;

    public static void main(String[] args) {
        new Triangle();
    }

    public Triangle() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.setSize(400, 400);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {
        private Polygon poly;

        {
            BufferedImage img = null;
            try {
                img = ImageIO.read(new File("apple.PNG"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Image dimg = img.getScaledInstance(800, 508, Image.SCALE_SMOOTH);
            ImageIcon imageIcon = new ImageIcon(dimg);
            setContentPane(new JLabel(imageIcon));

            poly = new Polygon(
                    new int[] { 110, 150, 50 },
                    new int[] { 200, 0, 0 },
                    3);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        private void setContentPane(JLabel jLabel) {
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.blue);
            g2.fill(poly);
            g2.setColor(Color.red);
            g2.fillRect(0, 0, 25, 75);
            g2.setColor(Color.green);
            g2.fillOval(200, 100, 100, 50);
            g2.setColor(Color.green);
            g2.fillOval(300, 300, 250, 250);
        }
    }
}

试试这个。对我来说没问题

public class Triangle {
    Oval o;

    public static void main(String[] args) {
        new Triangle();
    }

    public Triangle() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.setSize(400, 400);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {
        private Polygon poly;

        {
            BufferedImage img = null;
            try {
                img = ImageIO.read(new File("apple.PNG"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Image dimg = img.getScaledInstance(800, 508, Image.SCALE_SMOOTH);
            ImageIcon imageIcon = new ImageIcon(dimg);
            setContentPane(new JLabel(imageIcon));

            poly = new Polygon(
                    new int[] { 110, 150, 50 },
                    new int[] { 200, 0, 0 },
                    3);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        private void setContentPane(JLabel jLabel) {
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.blue);
            g2.fill(poly);
            g2.setColor(Color.red);
            g2.fillRect(0, 0, 25, 75);
            g2.setColor(Color.green);
            g2.fillOval(200, 100, 100, 50);
            g2.setColor(Color.green);
            g2.fillOval(300, 300, 250, 250);
        }
    }
}
package model;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class Triangle {

    public static void main(String[] args) {
        new Triangle();
    }

    public Triangle() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.setSize(400, 400);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {
        private Polygon poly;

        {
            poly = new Polygon(
                    new int[]{110, 150, 50},
                    new int[]{200, 0, 0},
                    3);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            try {
                BufferedImage img = ImageIO.read(new File("apple.PNG"));
                Image dimg = img.getScaledInstance(800, 508, Image.SCALE_SMOOTH);
                g.drawImage(dimg, 0, 0, this.getWidth(), this.getHeight(), null);
            } catch (IOException e) {
                e.printStackTrace();
            }

            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.blue);
            g2.fill(poly);
            g2.setColor(Color.red);
            g2.fillRect(0, 0, 25, 75);
            g2.setColor(Color.green);
            g2.fillOval(200, 100, 100, 50);
            g2.setColor(Color.green);
            g2.fillOval(300, 300, 250, 250);
        }
    }
}



请详细描述你的错误。没有错误它不会显示背景图像我尝试了各种方法可能是个奇怪的问题,但你确定能找到
apple.PNG
文件吗?你能尝试输入该文件的绝对路径吗?@BjornMistiaen“你确定可以找到apple.PNG文件吗?”如果不能,代码将抛出异常。给定代码调用
e.printStackTrace()这样的异常应该是显而易见的。您不会在JPanel的paintComponent方法中执行输入操作。在创建GUI之前,您只需读取一次图像并缩放一次。