gpt4 book ai didi

Java - 如何将 png 文件添加到 JPanel

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:32 24 4
gpt4 key购买 nike

这个星期五我需要交出我的 Java 项目 - 我现在只编程了大约两周 - 我真的很想将一个 PNG 文件添加到我的 JPanel 中。下面你可以找到我的代码,图像的名称是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();
}

}
}

最佳答案

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

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

抱歉,如果我犯了任何错误。

关于Java - 如何将 png 文件添加到 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13681778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com