重绘 JPanel 在 JApplet 中不起作用 [英] Repaint JPanel doesn't work in JApplet

查看:22
本文介绍了重绘 JPanel 在 JApplet 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含子 JPanel 和按钮的主 JPanel(在 JApplet 中).我想单击按钮使子 JPanel 被删除,另一个子 JPanel 添加到主 JPanel,但问题是只有当我重新单击按钮或调整 JApplet 的大小时,第二个子 JPanel 才会出现.

I have the the main JPanel (in JApplet ) which containts the child JPanel and the button. I want to click the button make the child JPanel removed and another child JPanel added to the main JPanel, but the problem is that only when I reclick the button or adjust the JApplet's size the second child JPanel apprear then.

我的按钮的监听器:

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 在这里:

You should call revalidate before repaint here:

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

重新验证调用更新容器层次结构,之后可能需要重新绘制.容器调整大小会同时执行(重新验证和重新绘制),这就是为什么在调整小程序大小后会出现面板的原因.

Revalidate call updates container hierarchy and after that a repaint might be needed. Container resize does both (revalidate and repaint), thats why the panel appears after you resize the applet.

我还注意到您的代码中有一个不好的地方:

Also i noticed 1 bad thing in yuor code:

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

您在每次重新绘制自定义组件时都在加载图像.最好将图像加载移动到构造函数中并只加载一次.

You are loading image each time your custom component repaints. Better move the image loading into constructor and load it just once.

这篇关于重绘 JPanel 在 JApplet 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆