简体   繁体   English

摇摆闪烁

[英]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.我的 Swing 有问题,我正在尝试做一个日历,由于我在JPanel放置了一个JLabel并且当我调用函数setText来更改显示的日期时,显示的日期显示图像闪烁几毫秒内,所有组件都没有放在合适的位置,然后一切都显示良好。

Even when I launch my application the images flickers and i noticed that every component's paintComponent function is called three times ...即使当我启动我的应用程序时,图像也会闪烁,我注意到每个组件的paintComponent函数都被调用了 3 次......

To summarize I did a Jpanel called Container which contains all the JLabel and Jtextfield , I did override their paintComponent to call setBounds .总之我做了Jpanel称为Container包含了所有JLabelJtextfield ,我没有覆盖其paintComponent调用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.首先,如果您想使用 setBounds,请确保已将框架的布局设置为 null,在我的情况下,我创建了一个 JPanel,在其中添加了所有组件,因此将其布局设置为 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 :)希望它会有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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