按下按钮后如何在小程序(java)中添加图片?

发布于 2024-09-18 15:03:45 字数 2813 浏览 4 评论 0原文

我发现了一种奇怪的方法,可以将图片单独放入小程序中,但是当我将代码放入按钮监听器中以便在按下按钮时显示图片时,它似乎不起作用。如果您还可以给我在小程序中放入图片的最简单的代码,我将非常感激!

有效的代码:

import java.awt.*;
import java.applet.*;
import javax.swing.*;

public class gamedone extends JApplet {     
    public void init() {            
        Container cp = getContentPane();
        cp.setBackground(Color.black);
        Container content_pane = getContentPane();
        Image img = getImage(getCodeBase(), "portal-cake.jpg");
        DrawingPanel drawing_panel =  new DrawingPanel(img);
        // Add the DrawingPanel to the content pane.
        content_pane.add(drawing_panel);
      } // init
}
    class DrawingPanel extends JPanel
    {
      Image img;
      DrawingPanel (Image img)
      { this.img = img; }
    
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
        }
    }

但是当这是我将其添加到的程序时,按钮无法使其工作:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

public class TypeInNames extends JApplet{
     JButton StartButton;
     JTextField name1, name2;
     String player1, player2;
     String reply;
     
     Container cp = getContentPane();

     public void init() 
     {
        setSize(350, 400);
        setLayout(null);
        cp.setBackground(Color.black);
        StartButton = new JButton("Start Game!");
        name1 = new JTextField("Player 1",35);
        name2 = new JTextField("Player 2",35);
        //(x, y, width, height);
        StartButton.setBounds(115,200,120,30);
        name1.setBounds(115,140,120,20);
        name2.setBounds(115,170,120,20);
        startGame();
     }
     
     public void startGame()
     {
        add(StartButton);
        add(name1);
        add(name2);
        StartButton.addActionListener(new ButtonListener());
     }
     
     public void game()
     {
        
     }
     
     public void endGame()
     {
        Container cp = getContentPane();
        cp.setBackground(Color.black);
        Container content_pane = getContentPane();
        Image img = getImage(getCodeBase(), "portal-cake.jpg");
        DrawingPanel drawing_panel =  new DrawingPanel(img);
        // Add the DrawingPanel to the content pane.
        content_pane.add(drawing_panel);
     }
         
     private class ButtonListener implements ActionListener{
         public void actionPerformed(ActionEvent event)
         {
            if (event.getSource() == StartButton)
            {
                player1 = name1.getText();
                player2 = name2.getText();
                remove(StartButton);
                remove(name1);
                remove(name2);
                endGame();
                repaint();
            }
         }
     }
    }

I have found an odd way of putting a picture in an applet just by itself, but it doesn't seem to work when i put the code in the buttonListener for the picture to show when a button is pressed. If you could also give me the simplest code for putting a picture in an applet, it would be very much appreciated!

the code that works:

import java.awt.*;
import java.applet.*;
import javax.swing.*;

public class gamedone extends JApplet {     
    public void init() {            
        Container cp = getContentPane();
        cp.setBackground(Color.black);
        Container content_pane = getContentPane();
        Image img = getImage(getCodeBase(), "portal-cake.jpg");
        DrawingPanel drawing_panel =  new DrawingPanel(img);
        // Add the DrawingPanel to the content pane.
        content_pane.add(drawing_panel);
      } // init
}
    class DrawingPanel extends JPanel
    {
      Image img;
      DrawingPanel (Image img)
      { this.img = img; }
    
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
        }
    }

but when this is the program i added it to, and the button doesn't make it work:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

public class TypeInNames extends JApplet{
     JButton StartButton;
     JTextField name1, name2;
     String player1, player2;
     String reply;
     
     Container cp = getContentPane();

     public void init() 
     {
        setSize(350, 400);
        setLayout(null);
        cp.setBackground(Color.black);
        StartButton = new JButton("Start Game!");
        name1 = new JTextField("Player 1",35);
        name2 = new JTextField("Player 2",35);
        //(x, y, width, height);
        StartButton.setBounds(115,200,120,30);
        name1.setBounds(115,140,120,20);
        name2.setBounds(115,170,120,20);
        startGame();
     }
     
     public void startGame()
     {
        add(StartButton);
        add(name1);
        add(name2);
        StartButton.addActionListener(new ButtonListener());
     }
     
     public void game()
     {
        
     }
     
     public void endGame()
     {
        Container cp = getContentPane();
        cp.setBackground(Color.black);
        Container content_pane = getContentPane();
        Image img = getImage(getCodeBase(), "portal-cake.jpg");
        DrawingPanel drawing_panel =  new DrawingPanel(img);
        // Add the DrawingPanel to the content pane.
        content_pane.add(drawing_panel);
     }
         
     private class ButtonListener implements ActionListener{
         public void actionPerformed(ActionEvent event)
         {
            if (event.getSource() == StartButton)
            {
                player1 = name1.getText();
                player2 = name2.getText();
                remove(StartButton);
                remove(name1);
                remove(name2);
                endGame();
                repaint();
            }
         }
     }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱果果 2024-09-25 15:03:45

对此不确定,但您可以尝试以下操作:

  1. 您应该在 drawing_panel 上调用 setBounds()

  2. 图像异步加载;您可能需要使用ImageObserver来了解它何时加载,然后重新绘制。您可以重写 DrawingPanel.imageUpdate() 方法。

  3. 树正在更新吗?添加组件后您可能需要调用 getContentPane().invalidate()

Not sure about this but you could try the following:

  1. You should call setBounds() on your drawing_panel

  2. images load asynchronously; you may want to use an ImageObserver to know when it has loaded, then repaint. You could override your DrawingPanel.imageUpdate() method.

  3. is the tree being updated? you may need to call getContentPane().invalidate() after adding the component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文