【问题标题】:Java - how to add a png file to JPanelJava - 如何将 png 文件添加到 JPanel
【发布时间】:2012-11-20 19:57:56
【问题描述】:

这个星期五我需要提交我的 Java 项目——我现在才编程大约两个星期——我真的想在我的 JPanel 中添加一个 PNG 文件。下面你找到我的代码,图像的名称是 java.png,我把它和我的 src 文件放在同一个文件夹中。我只是找不到关于如何做到这一点的好教程。请帮助我!
注意:图片应该插入到代码底部的booOpAfbeam结构中,也很抱歉这个杂乱无章的消息,我不知道如何发布我的代码不错。

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.ImageObserver;

    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.Image;
    import java.awt.Dimension; 
    import javax.swing.*;

public class Project extends JPanel {

private JLabel invoerLabel, invoerKleur, invoerKleur2, invoerBril, invoerBril2;
private JTextField invoerAantal, invoerK, invoerB;
private int intAantal;
private String strKleur="";
private String strBril="";
private JButton knop, knopBril, btnOpAf, btnOpAfbeam;
private Boolean booOpAf = false, booOpAfbeam = false;
private Image image1;


public Project(){
    setLayout(null); 
    invoerLabel = new JLabel("Voer aantal stoelen in: ");
    invoerAantal = new JTextField (10);
    invoerKleur = new JLabel("Voer kleur stoel in: ");
    invoerKleur2 = new JLabel("(blauw, rood, groen, paars, roos, zwart, geel, oranje)");
    invoerK = new JTextField (10);
    invoerBril = new JLabel("Voer kleur bril in: ");
    invoerBril2 = new JLabel("(blauw, rood, groen, paars, roos, zwart, geel, oranje)");
    invoerB = new JTextField (10);
    knopBril = new JButton ("ok");
    knop = new JButton("ok");
    btnOpAf = new JButton ("Wijs/Wijs niet");
    btnOpAfbeam = new JButton ("aan/uit");
    knop.addActionListener (new InvoervakHandler());
    knopBril.addActionListener(new InvoervakHandler2());
    btnOpAf.addActionListener(new aanuit());
    btnOpAfbeam.addActionListener(new beam());

    invoerLabel.setBounds(1000, 10, 150, 20);
    invoerAantal.setBounds(1150, 10, 60, 20);
    invoerKleur.setBounds(1000, 70, 120, 20);
    invoerKleur2.setBounds(985, 90, 300, 20);
    invoerK.setBounds(1150, 70, 60, 20);
    knop.setBounds(1155, 120, 50, 20);
    invoerBril.setBounds(1000, 170, 150, 20);
    invoerBril2.setBounds(985, 195, 300, 20);
    invoerB.setBounds(1150, 170, 60, 20);
    knopBril.setBounds(1155, 220, 50, 20);
    btnOpAf.setBounds(1000,275,115,20);
    btnOpAfbeam.setBounds(365, 26, 75, 20);

add(invoerLabel);
add(invoerAantal);
add(invoerKleur);
add(invoerKleur2);
add(invoerK);
add(invoerBril);
add(invoerBril2);
add(invoerB);
add(knop);
add(knopBril);
add(btnOpAf);
add(btnOpAfbeam);

}


public void paintComponent(Graphics g) {
    super.paintComponent(g);
    ImageIcon i = new ImageIcon("java.png");
    image1 = i.getImage();
    g.drawImage(image1, 0, 0, null);
    ImageIcon("java.png");
    //labels//
    invoerBril2.setForeground(new Color(0,173,173));
    invoerKleur2.setForeground(new Color(0,173,173));
    invoerLabel.setForeground(new Color(227,227,227));
    invoerKleur.setForeground(new Color(227,227,227));
    invoerBril.setForeground(new Color(227,227,227));
    //vloer
    g.setColor(new Color(102,77,51));
    g.fillRect(0, 375, 1000, 200);
    //muur
    g.setColor(new Color(99,136,176));
    g.fillRect(0, 0, 1000, 400);
    //achtergrond kleur balk//
    g.setColor(new Color(0,102,53));
    g.fillRect(980, 0, 386, 766);
    //beamer
    g.setColor(new Color(122,122,122));
    g.fillRect(355,20,100,30);
    g.fillRect(397,0,15,40);
    g.setColor(Color.BLACK);
    g.fillRect(355, 20, 100, 3);
    g.fillRect(355, 50, 100, 3);
    g.fillRect(352, 20, 3, 33);
    g.fillRect(452, 20, 3, 33);
    g.fillRect(410, 0, 3, 20);
    g.fillRect(395, 0, 3, 20);





    //bord
    g.setColor(new Color(98,121,74));
    g.fillRect(250, 100,300 , 200);
    g.setColor(Color.BLACK);
    g.fillRect(250, 300,300 , 10);
    g.fillRect(250, 100,300 , 10);
    g.fillRect(250, 100,10 , 200);
    g.fillRect(550, 100,10 , 210);




    //boekenkast horizontale balken
    g.setColor(Color.BLACK);
    g.fillRect(50, 160, 30, 250);
    g.fillRect(200, 160, 30, 250);
    //boekenkast verticale balken
    g.fillRect(50, 160, 150, 20);
    g.fillRect(50, 220, 150, 20);
    g.fillRect(50, 280, 150, 20);
    g.fillRect(50, 340, 150, 20);

    //boekenkast boeken rij 1
    g.setColor(new Color(204,0,0));
    g.fillRect(80, 180, 20, 40);
    g.setColor(new Color(0,204,102));
    g.fillRect(110, 180, 20, 40);
    g.setColor(new Color(204,102,0));
    g.fillRect(140, 180, 20, 40);
    g.setColor(new Color(204,204,0));
    g.fillRect(170, 180, 20, 40);
    //boekenkast boeken rij 2
    g.setColor(new Color(0,204,102));
    g.fillRect(80, 240, 20, 40);
    g.setColor(new Color(204,0,0));
    g.fillRect(110, 240, 20, 40);
    g.setColor(new Color(204,102,0));
    g.fillRect(140, 240, 20, 40);
    g.setColor(new Color(204,204,0));
    g.fillRect(170, 240, 20, 40);
    //boekenkast boeken rij 3
    g.setColor(new Color(204,0,0));
    g.fillRect(80, 300, 20, 40);
    g.setColor(new Color(204,102,0));
    g.fillRect(110, 300, 20, 40);
    g.setColor(new Color(204,204,0));
    g.fillRect(140, 300, 20, 40);
    g.setColor(new Color(0,204,102));
    g.fillRect(170, 300, 20, 40);
    //boeklabels
    g.setColor(new Color(224,224,224));
    g.fillRect(85,190,10,20);
    g.fillRect(115,190,10,20);
    g.fillRect(145,190,10,20);
    g.fillRect(175,190,10,20);
    g.fillRect(85,250,10,20);
    g.fillRect(115,250,10,20);
    g.fillRect(145,250,10,20);
    g.fillRect(175,250,10,20);
    g.fillRect(85,310,10,20);
    g.fillRect(115,310,10,20);
    g.fillRect(145,310,10,20);
    g.fillRect(175,310,10,20);

    //hoofd//
    g.setColor(new Color(255,237,184));
    g.fillOval(615,170,150,150);
    g.setColor(new Color(255,255,255));
    g.fillOval(645,220,25,25);
    g.fillOval(715,220,25,25);
    g.setColor(new Color(0,0,0));
    g.fillOval(655,230,10,10);
    g.fillOval(725,230,10,10);
    g.drawArc(675,240,40,40,0,-180);
    g.drawArc(635,250,115,50,0,-180);


    //lichaam
    g.setColor(new Color(153,153,0));
    g.fillRect(650, 300, 100, 125);
    g.setColor(Color.BLACK);
    g.fillRect(650, 430, 25, 60);
    g.fillRect(730, 430, 25, 60);
    g.fillRect(650, 420, 100, 15);
    g.setColor(Color.BLUE);
    g.fillOval(632, 470, 45, 25);
    g.fillOval(725, 470, 45, 25);


    // bureau
    g.setColor(new Color(184, 184, 184));
    g.fillRect(540, 410, 325, 20);
    g.fillRect(540, 430, 20, 60);
    g.fillRect(845, 430, 20, 60);


    //vingers
    g.setColor(new Color(255,237,184));

    g.fillOval(785, 375, 10, 25);
    g.fillOval(775, 375, 10, 25);
    g.fillOval(765, 375, 10, 25);
    g.fillOval(755, 375, 10, 25);
    //pc
    g.setColor(Color.BLACK);
    g.fillRect(650, 280, 175, 100);
    g.fillRect(676, 400, 125, 10);
    g.fillRect(730, 375, 15, 30);
    //
    g.setColor(new Color(184, 184, 184));
    g.fillRect(660, 320, 20, 5);
    g.fillRect(660, 340, 20, 5);
    g.fillRect(795, 320, 20, 5);
    g.fillRect(795, 340, 20, 5);
    //apple
     g.fillOval(725, 320, 20, 20);
     g.fillOval(732, 310, 5, 10);
     g.setColor(Color.BLACK);
     g.fillOval(735, 325, 10, 10);






    if(strBril.equals("blauw")) {
        g.setColor(Color.BLUE);
    }
    if(strBril.equals("rood")) {
        g.setColor(Color.RED);
    }
    if(strBril.equals("groen")) {
        g.setColor(Color.GREEN);
    }
    if(strBril.equals("paars")) {
        g.setColor(new Color(204,51,255));
    }
    if(strBril.equals("roos")) {
        g.setColor(new Color(255,51,204));
    }
    if(strBril.equals("zwart")) {
        g.setColor(Color.BLACK);
    }
    if(strBril.equals("geel")) {
        g.setColor(Color.YELLOW);
    }
    if(strBril.equals("oranje")) {
        g.setColor(Color.ORANGE);
    }{
    //bril//

    //linkerglas
    g.fillRect(600,200,80,5);
    g.fillRect(600,200,5,50);
    g.fillRect(600,250,80,5);
    g.fillRect(680,200,5,55);
    //tussenbeentje//
    g.fillRect(680,225,20,10);
    //rechterglas
    g.fillRect(700,200,80,5);
    g.fillRect(700,200,5,50);
    g.fillRect(700,250,80,5);
    g.fillRect(780,200,5,55);


    }









int teller1;

    if(strKleur.equals("blauw")) {
        g.setColor(Color.BLUE);
    }
    if(strKleur.equals("rood")) {
        g.setColor(Color.RED);
    }
    if(strKleur.equals("groen")) {
        g.setColor(Color.GREEN);
    }
    if(strKleur.equals("paars")) {
        g.setColor(new Color(204,51,255));
    }
    if(strKleur.equals("roos")) {
        g.setColor(new Color(255,51,204));
    }
    if(strKleur.equals("zwart")) {
        g.setColor(Color.BLACK);
    }
    if(strKleur.equals("geel")) {
        g.setColor(Color.YELLOW);
    }
    if(strKleur.equals("oranje")) {
        g.setColor(Color.ORANGE);
    }

    int Ra = 20;
    int Rb = 20;
    int Rc = 40;
    int Rd = 20;
    for (teller1=1; teller1 <= intAantal; teller1++) {
        g.fillRect(Ra,450,10,50);
        g.fillRect(Rb,500,50,10);
        g.fillRect(Rc,500,10,30);
        g.fillRect(Rd,530,50,10);

        Ra = Ra + 70;
        Rb = Rb + 70;
        Rc = Rc + 70;
        Rd = Rd + 70;
    }

     if (booOpAf) {
            if (!booOpAf) {
                g.setColor(new Color(153,153,0));
                g.fillRect(625, 300, 30, 100);
            } else {
                //arm
                g.setColor(new Color(153,153,0));
                g.fillRect(550, 315, 100, 25);
                //linkerarm vingers
                g.setColor(new Color(255,237,184));

                g.fillOval(530, 310, 30, 30);
                g.fillOval(515, 310, 25, 10);
                g.fillOval(515, 320, 25, 10);
                g.fillOval(515, 330, 25, 10);
                g.fillOval(545, 300, 10, 25);
            }
        }
     if (booOpAfbeam) {
            if (!booOpAfbeam) {
            } else {
                g.drawImage(image1, 500, 100, null);
            }
        }
}


private void ImageIcon(String string) {
    // TODO Auto-generated method stub

}


class InvoervakHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String strAantal = invoerAantal.getText();
        intAantal = Integer.parseInt(strAantal);
        strKleur= invoerK.getText();

        repaint();
    }
}

class InvoervakHandler2 implements ActionListener {
    public void actionPerformed(ActionEvent f){
        strBril= invoerB.getText();

        repaint();
}
}
    public class aanuit implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {
            if (booOpAf == true) {
                booOpAf = false;
            } else {
                booOpAf = true;
            }
            repaint();
        }

           }    
    public class beam implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {
            if (booOpAfbeam == true) {
                booOpAfbeam = false;
            } else {
                booOpAfbeam = true;
            }
            repaint();
        }

   }    
   }

【问题讨论】:

  • setLayout(null); 使用布局!
  • 请不要将所有(不相关的)代码放在这里并要求修复它...仅显示相关部分并告诉您尝试过的内容!
  • 1) 在达到 404 行代码之前对这些内容进行排序可能是个好主意。 2) 为了尽快获得更好的帮助,请发布SSCCE。 (即有一个 main(String[]) 将其放在屏幕上,使用生成的图像,少于 404 行..
  • public void paintComponent(Graphics g) { super.paintComponent(g); ImageIcon i = new ImageIcon("java.png"); 不要尝试在paintComponent() 方法中加载图像!它现在应该被加载和缓存。还有g.drawImage(image1, 0, 0, this);
  • 或使用 JLabel:new JLabel(new ImageIcon(...));

标签: java image swing png jpanel


【解决方案1】:

试试这个:
添加另一个面板,然后在该面板上上传图像。

lblImage = new javax.swing.JLabel();
lblImage.setIcon(new javax.swing.ImageIcon("E:..path...png));

【讨论】:

    【解决方案2】:

    我无法验证您的完整代码。所以最好确保您已将图像导入项目(否则,如果图像不在指定的位置,您将无法获取图像)。并检查图像是否在 src 中,您需要指定“/”和图像名称。定位图像。

    我建议最好的方法是创建一个标签,右键单击并获取图像的属性并选择您要显示的图像。 netbeans 将处理其余代码。

    对不起,如果我犯了任何错误。

    【讨论】:

    • 这是相关代码,我想在此处插入图像,因为我希望它能够打开或关闭 'if (booOpAfbeam) { if (!booOpAfbeam) { } else { ' g.drawImage(image1, 500, 100, null);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多