我正在尝试添加 JFrame 背景图像,但在 Java 中没有显示出来。

huangapple go评论60阅读模式
英文:

I am trying to add JFrame background image but it is not is not showing up in java

问题

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) {
            // Set content pane with the background image
        }

        @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);
        }
    }
}
英文:

I am trying to add a background image to an JFrame.

I tried every possible solution but it's not working, all I want to do is to add a background image to the JFrame and then print shapes in it as you can see in my code.

Can anyone help me to fix it?

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);
}
}
}

答案1

得分: 1

请尝试这个。对我来说很好。

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);
        }
    }
}
英文:

Try this. It's fine for me.

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);
}
}
}

huangapple
  • 本文由 发表于 2020年5月29日 15:44:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/62081042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定