Java swing添加图像不工作
所以我上了这门课Java swing添加图像不工作,java,image,swing,paintcomponent,Java,Image,Swing,Paintcomponent,所以我上了这门课 public class ImagePanel extends JPanel { BufferedImage lionImage; public ImagePanel(){ try { lionImage = ImageIO.read (new File ("imgres.jpg")); } catch (IOException e) { e.printStackTrace(); } } @Override pub
public class ImagePanel extends JPanel {
BufferedImage lionImage;
public ImagePanel(){
try {
lionImage = ImageIO.read (new File ("imgres.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
if (lionImage != null) {
g.drawImage(lionImage, 0, 0, this);
}
}
public BufferedImage getLionImage() {
return lionImage;
}
public void setLionImage(BufferedImage lionImage) {
this.lionImage = lionImage;
}
}
然后我上了一堂测试课
public class test {
public static void main (String [] args) throws Exception {
ImagePanel test = new ImagePanel();
JFrame frame = new JFrame ("Image");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane ().setLayout (new BorderLayout ());
frame.add(test);
frame.pack ();
frame.setVisible (true);
}
}
一切都很好,简单制作一个框架制作一个ImagePanel,将其添加到框架中
然后我在我的实际工作中尝试了它
public class Enviroment implements Runnable, ActionListener{
private JFrame frame;
private JPanel enviromentPanel,totalGUI,enviromentButtonPanel;
private JButton newFrogButton, resetButton, hungryButton;
private JTextField enterName;
private JLabel hungryLabel;
private ArrayList<Frog> frogs = new ArrayList<Frog>();
private ArrayList<Fly> flys = new ArrayList<Fly>();
public Enviroment(){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("[=] Hungry Cyber Pet [=]");
frame.setContentPane(runEnviroment());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(740, 800);
frame.setVisible(true);
}
public JPanel runEnviroment(){
JPanel totalGUI = new JPanel();
totalGUI.setLayout(null);
JPanel enviromentPanel = new JPanel();
enviromentPanel.setLayout(null);
enviromentPanel.setLocation(10, 10);
enviromentPanel.setSize(700, 700);
enviromentPanel.setBackground(Color.WHITE);
totalGUI.add(enviromentPanel);
JPanel enviromentButtonPanel = new JPanel();
FlowLayout experimentLayout = new FlowLayout();
enviromentButtonPanel.setLayout(experimentLayout);
enviromentButtonPanel.setLocation(10, 710);
enviromentButtonPanel.setSize(700, 50);
totalGUI.add(enviromentButtonPanel);
newFrogButton = new JButton("New Frog");
newFrogButton.setLocation(0, 0);
newFrogButton.setSize(120, 30);
newFrogButton.addActionListener(this);
enviromentButtonPanel.add(newFrogButton);
enterName = new JTextField("Enter name");
enterName.setLocation(140,0);
enterName.setSize(120,30);
enviromentButtonPanel.add(enterName);
hungryButton = new JButton("Hungry!");
hungryButton.setLocation(280, 0);
hungryButton.setSize(120, 30);
hungryButton.addActionListener(this);
enviromentButtonPanel.add(hungryButton);
resetButton = new JButton("Reset");
resetButton.setLocation(420, 0);
resetButton.setSize(120, 30);
resetButton.addActionListener(this);
enviromentButtonPanel.add(resetButton);
totalGUI.setOpaque(true);
return totalGUI;
}
public void draw(){
ImagePanel frogImage = new ImagePanel();
enviromentPanel.add(frogImage);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == newFrogButton){
Frog frog = new Frog(enterName.getText());
frogs.add(frog);
draw();
System.out.println(frogs);
Fly fly = new Fly();
flys.add(fly);
System.out.println(flys);
}
else if(e.getSource() == hungryButton){
}
else if(e.getSource() == resetButton){
frogs.clear();
flys.clear();
System.out.println(frogs);
System.out.println(flys);
}
}
在任何人说话之前,图像就在那里,而且效果很好。怎么了
青蛙和苍蝇的课程很简单
public class Fly {
private int xPosition;
private int yPosition;
private boolean eaten;
public Fly(){
Random randomGenerator = new Random();
xPosition = randomGenerator.nextInt(100);
yPosition = randomGenerator.nextInt(100);
eaten = false;
}
public int getxPosition() {
return xPosition;
}
public void setxPosition(int xPosition) {
this.xPosition = xPosition;
}
public int getyPosition() {
return yPosition;
}
public void setyPosition(int yPosition) {
this.yPosition = yPosition;
}
public boolean isEaten() {
return eaten;
}
public void setEaten(boolean eaten) {
this.eaten = eaten;
}
public void move(){
Random randomGenerator = new Random();
int xChange = -10 + randomGenerator.nextInt(20);
int yChange = -10 + randomGenerator.nextInt(20);
xPosition = xPosition + xChange;
yPosition = yPosition + yChange;
move();
}
@Override
public String toString() {
return "Fly [xPosition=" + xPosition + ", yPosition=" + yPosition
+ ", eaten=" + eaten + "]";
}
}
public class Frog {
@Override
public String toString() {
return "Frog [xPosition=" + xPosition + ", yPosition=" + yPosition
+ ", name=" + name + ", hungry=" + hungry + "]";
}
private int xPosition;
private int yPosition;
private BufferedImage lionImage=null;
private String name;
private boolean hungry;
public Frog(String newName){
Random randomGenerator = new Random();
xPosition = randomGenerator.nextInt(100);
yPosition = randomGenerator.nextInt(100);
name = newName;
hungry = false;
getImage();
}
public void getImage(){
try{
lionImage =ImageIO.read(new File("imgres.jpg"));
}catch (IOException e){}
}
public void move(){
Random randomGenerator = new Random();
int xChange = -10 + randomGenerator.nextInt(20);
int yChange = -10 + randomGenerator.nextInt(20);
xPosition = xPosition + xChange;
yPosition = yPosition + yChange;
move();
}
public void moveHungry(){
}
public void eat(){
}
public int getxPosition() {
return xPosition;
}
public void setxPosition(int xPosition) {
this.xPosition = xPosition;
}
public int getyPosition() {
return yPosition;
}
public void setyPosition(int yPosition) {
this.yPosition = yPosition;
}
public BufferedImage getLionImage() {
return lionImage;
}
public void setLionImage(BufferedImage lionImage) {
this.lionImage = lionImage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isHungry() {
return hungry;
}
public void setHungry(boolean hungry) {
this.hungry = hungry;
}
public void bounce(){
}
}
如果将环境面板创建为局部变量,则类变量为null 您的代码应该是:
// JPanel enviromentPanel = new JPanel();
enviromentPanel = new JPanel();
enviromentPanel.setLayout(null);
enviromentPanel.setLocation(10, 10);
enviromentPanel.setSize(700, 700);
另外,不要使用空布局。使用适当的布局工具来布局组件。请发布青蛙和苍蝇类,以获得更多帮助。青蛙和苍蝇都已完成。我让您的代码正常工作。如果你还感兴趣,那么我可以给你答案???哦,太简单了。非常感谢,但我希望你能理解,当你累了的时候,你整天都在看代码,很容易错过这样的东西。好吧,现在没有错误消息,但它没有绘制图像…哦,面板只有一个块,所以有没有布局的需要?图片将随机添加当您将组件动态添加到可见面板时,您需要重新验证()面板。如果随机定位组件,则实际上不需要布局管理器。
// JPanel enviromentPanel = new JPanel();
enviromentPanel = new JPanel();
enviromentPanel.setLayout(null);
enviromentPanel.setLocation(10, 10);
enviromentPanel.setSize(700, 700);