簡體   English   中英

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

[英]How do you add a picture in an applet (java) after a button is pressed?

我發現了一種將圖片單獨放在小程序中的奇怪方法,但是當我將代碼放在 buttonListener 中以便在按下按鈕時顯示圖片時,它似乎不起作用。 如果您也能給我一個最簡單的將圖片放入小程序的代碼,將不勝感激!

有效的代碼:

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

對此不確定,但您可以嘗試以下操作:

  1. 你應該在你的drawing_panel上調用setBounds()

  2. 圖片異步加載; 您可能想使用ImageObserver來知道它何時加載,然后repaint 您可以覆蓋您的DrawingPanel.imageUpdate()方法。

  3. 樹正在更新嗎? 您可能需要在添加組件后調用getContentPane().invalidate()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM