1

我是 Java 新手,我似乎无法用半透明背景修复这个错误。

每当我更改面板的某些部分时,它都会绘制更改以及窗口的一些随机图片。

更清晰的错误图片

这是完整工作项目的链接,因此您可以清楚地看到错误。稍等一下,我的服务器加载速度很慢http://fong.shujindesigns.net

我制作了这个较小版本的错误,以便您进行测试。

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

public class Example1 extends JApplet implements MouseListener {


    Container Con = getContentPane();
    JPanel panel1 = new JPanel(new GridLayout(5, 5, 0, 0));
    JPanel[][] panel2 = new JPanel[5][5];
    JLabel[] label = new JLabel[25];
    Color color;
    int count = 0;

    public void init() {

        int col = 0;
        int row = 0;
        color = new Color(0.00f, 0.24f, 0.70f, 0.50f);

        for (int i = 0; i < 25; i++) {
            if (col > 4) {
                col = 0;
                row++;
            }
        label[i] = new JLabel("test");
        label[i].addMouseListener(this);
        panel2[col][row] = new JPanel();
        panel2[col][row].add(label[i]);
        color = new Color(0.00f, 0.24f, 0.70f, 0.20f);
        panel2[col][row].setBackground(color);
        label[i].setBorder(BorderFactory.createLineBorder(Color.red));
        panel1.add(panel2[col][row]);
        color = new Color(0.50f, 0.24f, 0.10f, 0.20f);
        panel1.setBackground(color);
        col++;
        }



        Con.add(panel1);
        panel1.setOpaque(false);
        label[5].setText("test2");
    }


    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
        label[5].setText("Test 3" + count);
        count++;
    }

    public void mousePressed(MouseEvent e) {  
    }

    public void mouseReleased(MouseEvent e) { 
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {  
    }
}
4

1 回答 1

0

在 mouseClicked 事件中,添加“repaint()”

如果这不起作用,请尝试 invalidate()、repaint()

public void mouseClicked(MouseEvent e) {
    label[5].setText("Test 3" + count);
    count++;

    //revalidate();
    repaint();

}
于 2012-07-11T02:06:46.037 回答