簡體   English   中英

重新繪制JPanel在JApplet中不起作用

[英]Repaint JPanel doesn't work in JApplet

我有主要的JPanel(在JApplet中),其中包含子JPanel和按鈕。 我想單擊該按鈕,以刪除子JPanel並將另一個子JPanel添加到主JPanel,但是問題是,只有當我重新單擊該按鈕或調整JApplet的大小時,第二個子JPanel才會出現。

我的按鈕的監聽器:

button.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            panel.remove(custompanel);
            panel.add(new CustomPanel("/hinhtu2.jpg"), BorderLayout.CENTER);
            panel.repaint();
            panel.revalidate();

        }
        });

我的整個代碼:

 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.Toolkit;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.File;
 import java.io.IOException;

 import javax.imageio.ImageIO;
 import javax.swing.BorderFactory;
 import javax.swing.ImageIcon;
 import javax.swing.JApplet;
 import javax.swing.JButton;
 import javax.swing.JLabel;
 import javax.swing.JPanel;

 public class applet extends JApplet {
   public void init() {

    try {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI();
            }
        });
    } catch (Exception e) {
        //System.err.println("createGUI didn't successfully complete");
        e.printStackTrace();
    }
}

 private void createGUI() {
    final JPanel panel = new JPanel(new BorderLayout());
    JButton button = new JButton("CLICK ME");
    panel.add(button, BorderLayout.SOUTH);
    final CustomPanel custompanel = new CustomPanel("/hinhtu.jpg");
    panel.add(custompanel, BorderLayout.CENTER);

    button.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            panel.remove(custompanel);
            panel.add(new CustomPanel("/hinhtu2.jpg"), BorderLayout.CENTER);
            panel.repaint();
            panel.revalidate();

        }

        });

    add(panel);
    }

public class CustomPanel extends JPanel{
    String resource;
    public CustomPanel(String resource){
        super();
        this.resource = resource;



    }
    public void paintComponent(Graphics g) {



        Image x = Toolkit.getDefaultToolkit().getImage(getClass().getResource(resource));
        g.drawImage(x, 0, 0, null); 

    }   



}

}

我的屏幕記錄: http : //www.screenr.com/prx8

您應該在這里重畫之前調用revalidate:

        panel.remove(custompanel);
        panel.add(new CustomPanel("/hinhtu2.jpg"), BorderLayout.CENTER);
        panel.repaint();
        panel.revalidate();

重新驗證調用將更新容器層次結構,此后可能需要重新繪制。 容器調整大小會同時進行(重新驗證和重新繪制),這就是為什么在調整小程序的大小后出現面板的原因。

我也注意到您的代碼中有1件事:

public void paintComponent(Graphics g) {
    Image x = Toolkit.getDefaultToolkit().getImage(getClass().getResource(resource));
    g.drawImage(x, 0, 0, null); 
}   

每次自定義組件重新繪制時,您都在加載圖像。 最好將圖像加載移至構造函數中,然后僅加載一次。

暫無
暫無

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

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