简体   繁体   中英

Bad rendering onUpdate Swing component Java

i have a rendering problem with a JLabel in Java: i use the pattern oberver observable and when my Model notify to my View that JLabel has changed the content of JLabel or specifically the content that is showed in my JLabel area is random. Sometimes it renders a part of a button that is in another panel or sometimes it renders colors that i set in other components of my view! But if i minimize and then maximize my frame, all is correctly rendered.

Sorry for my bad english.

This is the correct rendering of numbers on the right and on the left 在此处输入图片说明

This is the rendering when I click some button and then executing some OnUpdate() 在此处输入图片说明

EDIT this is the class of my JPanel that contains the JLabels:

class InformationPanel extends JPanel {

    private static final int DEFAULT_INFO_WIDTH = Configuration.DEFAULT_INFO_PANEL_WIDTH;
    private static final int DEFAULT_INFO_HEIGHT = Configuration.DEFAULT_INFO_PANEL_HEIGHT;

    private String playerId;
    private int numNaviVive;
    private int numNaviAffondate;
    private JLabel JLabelNumNaviAffondate;
    private JLabel JLabelNumNaviVive;

    public InformationPanel(String playerId) {
        this.playerId = playerId;
        numNaviVive = 0;
        numNaviAffondate = 0;

        JButton JButtonPlayerId = new JButton(playerId);
        JLabelNumNaviAffondate = new JLabel(String.valueOf(numNaviAffondate));
        JLabelNumNaviVive = new JLabel(String.valueOf(numNaviVive));
        JLabel JlblNumNaviVive = new JLabel("Navi disponibili:");
        JLabel JlblNumNaviAffondate = new JLabel("Navi affondate: ");

        JButtonPlayerId.setBackground(new Color(0, 0, 0, 0));
        JButtonPlayerId.setFont(new Font("Serif", Font.BOLD, 16));
        JButtonPlayerId.setForeground(Color.YELLOW);
        JButtonPlayerId.setFocusable(false);
        JButtonPlayerId.setEnabled(false);

        JlblNumNaviVive.setBackground(new Color(0, 0, 0, 0));
        JlblNumNaviVive.setFont(new Font("Serif", Font.BOLD, 16));
        JlblNumNaviVive.setForeground(Color.YELLOW);
        JlblNumNaviVive.setDoubleBuffered(true);

        JlblNumNaviAffondate.setBackground(new Color(0, 0, 0, 0));
        JlblNumNaviAffondate.setFont(new Font("Serif", Font.BOLD, 16));
        JlblNumNaviAffondate.setForeground(Color.YELLOW);
        JlblNumNaviAffondate.setDoubleBuffered(true);

        JLabelNumNaviAffondate.setBackground(new Color(0, 0, 0, 0));
        JLabelNumNaviAffondate.setFont(new Font("Serif", Font.BOLD, 16));
        JLabelNumNaviAffondate.setForeground(Color.YELLOW);
        JLabelNumNaviAffondate.setFocusable(false);
        JLabelNumNaviAffondate.setEnabled(false);

        JLabelNumNaviVive.setBackground(new Color(0, 0, 0, 0));
        JLabelNumNaviVive.setFont(new Font("Serif", Font.BOLD, 16));
        JLabelNumNaviVive.setForeground(Color.YELLOW);
        JLabelNumNaviVive.setFocusable(false);
        JLabelNumNaviVive.setEnabled(false);

        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        setPreferredSize(new Dimension(DEFAULT_INFO_WIDTH, DEFAULT_INFO_HEIGHT));
        //setBackground(new Color(0, 0, 0, 0));
        setBackground(new Color(0,0,0,40));

        add(JButtonPlayerId);
        add(JlblNumNaviAffondate);
        add(JLabelNumNaviAffondate);
        add(JlblNumNaviVive);
        add(JLabelNumNaviVive);
        setVisible(true);
    }

    public String getId() {
        return playerId;
    }

    public int getNumNaviAffondate() {
        return numNaviAffondate;
    }

    public int getNumNaviVive() {
        return numNaviVive;
    }

    public void setNumNaviVive(int numNaviVive) {
        this.numNaviVive = numNaviVive;
        this.JLabelNumNaviVive.setText(String.valueOf(numNaviVive));
        this.validate();

    }

    public void setNumNaviAffondate(int numNaviAffondate) {
        this.numNaviAffondate = numNaviAffondate;
        this.JLabelNumNaviAffondate.setText(String.valueOf(numNaviAffondate));
        this.validate();
    }
}

Frist of all variable names should NOT start with an upper case character. You code is hard to read because the forum thinks all your variables are class names and highlights them as such. Follow Java conventions and don't make up your own.

JLabelNumNaviAffondate.setBackground(new Color(0, 0, 0, 0));

Variable are transparent by default so you can't set a background Color to them. The fact that you are trying to set a transparent color may or may not be your problem.

setBackground(new Color(0,0,0,40));

This may be the problem. You can't just set transparency on a Swing components as this breaks the painting chain. That is an opaque component guarantees to paint the background of itself, but when you use transparency the background is not completely painted so you get painting artifacts.

So basically you need to manage painting of the background yourself. So you actuallyl need to make your component non-opaque to the parent component is first painted, then you can paint your transparent background.

JPanel panel = new JPanel()
{
    protected void paintComponent(Graphics g)
    {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }
};
panel.setOpaque(false);
panel.setBackground( new Color(255, 0, 0, 20) );
frame.add(panel);

See Background With Transparency for more information and a simpler solution so that you don't need to do custom painting on all your components.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM