简体   繁体   中英

Swing Flickering

I've a problem with Swing , i'm trying to do a calendar and the date is displayed thanks to a JLabel that i've put in a JPanel and when I call the function setText to change the date displayed the image flickers for a few milliseconds all the components are not in the good place and then everything displays well.

Even when I launch my application the images flickers and i noticed that every component's paintComponent function is called three times ...

To summarize I did a Jpanel called Container which contains all the JLabel and Jtextfield , I did override their paintComponent to call setBounds . And then i add the container inside the frame and I display it.

Class draw :

package gui;

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

public class draw {
    JTextField B3;
    JTextField C3;
    JTextField D3;
    JTextField E3;
    JTextField F3;
    JTextField B4;
    JTextField C4;
    JTextField D4;
    JTextField E4;
    JTextField F4;
    JTextField B5;
    JTextField C5;
    JTextField D5;
    JTextField E5;
    JTextField F5;
    JTextField B6;
    JTextField C6;
    JTextField D6;
    JTextField E6;
    JTextField F6;
    JTextField B7;
    JTextField C7;
    JTextField D7;
    JTextField E7;
    JTextField F7;
    JTextField B8;
    JTextField C8;
    JTextField D8;
    JTextField E8;
    JTextField F8;

    public draw(){}
    public void drawCalendar() {
        B3 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(470, 170, 200, 30);

                super.paintComponent(g);
            }

        };
        C3 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(670, 170, 200, 30);

                super.paintComponent(g);
            }
        };
        D3 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                System.out.println("IL MA REPAIT !");
                this.setBounds(870, 170, 200, 30);

                super.paintComponent(g);
            }
        };
        E3 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1070, 170, 200, 30);

                super.paintComponent(g);
            }
        };
        F3 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1270, 170, 200, 30);

                super.paintComponent(g);
            }
        };
        B4 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(470, 200, 200, 30);

                super.paintComponent(g);
            }
        };
        C4 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(670, 200, 200, 30);

                super.paintComponent(g);
            }
        };
        D4 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(870, 200, 200, 30);

                super.paintComponent(g);
            }
        };
        E4 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1070, 200, 200, 30);

                super.paintComponent(g);
            }
        };
        F4 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1270, 200, 200, 30);

                super.paintComponent(g);
            }
        };

        B5 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(470, 230, 200, 30);

                super.paintComponent(g);
            }
        };
        C5 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(670, 230, 200, 30);

                super.paintComponent(g);
            }
        };
        D5 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(870, 230, 200, 30);

                super.paintComponent(g);
            }
        };
        E5 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1070, 230, 200, 30);

                super.paintComponent(g);
            }
        };
        F5 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1270, 230, 200, 30);

                super.paintComponent(g);
            }
        };
        B6 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(470, 260, 200, 30);

                super.paintComponent(g);
            }
        };
        C6 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(670, 260, 200, 30);

                super.paintComponent(g);
            }
        };
        D6 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(870, 260, 200, 30);

                super.paintComponent(g);
            }
        };
        E6 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1070, 260, 200, 30);

                super.paintComponent(g);
            }
        };
        F6 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1270, 260, 200, 30);

                super.paintComponent(g);
            }
        };
        B7 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(470, 350, 200, 30);

                super.paintComponent(g);
            }
        };
        C7 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(670, 350, 200, 30);

                super.paintComponent(g);
            }
        };
        D7 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(870, 350, 200, 30);

                super.paintComponent(g);
            }
        };
        E7 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1070, 350, 200, 30);

                super.paintComponent(g);
            }
        };
        F7 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1270, 350, 200, 30);

                super.paintComponent(g);
            }
        };
        B8 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(470, 380, 200, 30);

                super.paintComponent(g);
            }
        };
        C8 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(670, 380, 200, 30);

                super.paintComponent(g);
            }
        };
        D8 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(870, 380, 200, 30);

                super.paintComponent(g);
            }
        };
        E8 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1070, 380, 200, 30);

                super.paintComponent(g);
            }
        };
        F8 = new JTextField(15) {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(1270, 380, 200, 30);

                super.paintComponent(g);
            }
        };

Example class

public class example {
    int weeknumber = 0;
    String month = "June";
    JLabel A1;
    public void updateLabelText(){
        weeknumber++;
        A1.setText("<html>week" + weeknumber + "<br>" + "<center>" + month + "</html>");

    }

    public example(){
        JFrame frame = new JFrame("Planning");
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setResizable(false);
        frame.setPreferredSize(new Dimension((int) dimension.getWidth() - 50, (int) dimension.getHeight() - 50));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Integer weeknumber = 23;

        draw draw = new draw();
        draw.drawCalendar();

         A1 = new JLabel("<html>week " + weeknumber + "<br>" + "<center>" + month + "</html>") {
            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds(70, 40, 400, 100);
                this.setHorizontalAlignment(SwingConstants.CENTER);




                g.setColor(Color.BLUE);

                g.fillRect(0, 25, 400, 60);
                g.setColor(Color.BLACK);
                g.drawRect(0, 25, 400, 60);
                super.paintComponent(g);


            }
        };
        JPanel Container = new JPanel();
        Container.setBounds(0, 0, (int) dimension.getWidth(), (int) dimension.getWidth());
        Container.add(A1);

        JButton next_week = new JButton("Suivante") {


            @Override
            protected void paintComponent(Graphics g) {
                this.setBounds((int) dimension.getWidth() / 2 + 70, 10, 150, 50);

                super.paintComponent(g);

            }
        };
        next_week.addActionListener(e -> {
            updateLabelText();

        });
        Container.add(next_week);
        Container.add(draw.B6);
        Container.add(draw.C6);
        Container.add(draw.D6);
        Container.add(draw.E6);
        Container.add(draw.F6);
        Container.add(draw.C7);
        Container.add(draw.D7);
        Container.add(draw.E7);
        Container.add(draw.F7);
        Container.add(draw.B8);
        Container.add(draw.C8);
        Container.add(draw.D8);
        Container.add(draw.E8);
        Container.add(draw.F8);
        frame.add(Container);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        example example = new example();

    }
}

Thanks for your help

I found the solution of my problem. I'm going to explain how I solved it if someone encounters the same problem than me it might help.

First of all if you want to use setBounds make sure that you have set the frame's layout as null and in my case I created a JPanel where I add all my components so I set its layout to null.

JPanel Container = new JPanel();
JFrame frame = new JFrame("Name of your APP");
Container.setLayout(null);
frame.setLayout(null);

Then write a class that extends the component that you wanna use so in my case I wrote the following classes

class label extends JLabel{
        int x;
        int y;
        int width;
        int height;
        public label(String text,int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.setText(text);
            this.setBounds(x, y, width,height);
            this.setHorizontalAlignment(SwingConstants.CENTER);

        }
}


class button extends JButton{
        public button(String text,int x, int y,int width,int height){
            this.setText(text);
            this.setBounds(x, y, width, height);

        }


} 
class textField extends JTextField{

        public textField(int col,int x,int y ,int width,int height){
            this.setBounds(x, y, width,height);
            this.setColumns(col);

        }

}       

This way works well if you want to control everything in your frame and set absolute position.

Hope it will help :)

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