简体   繁体   English

JFrame不使用后台更改进行更新

[英]JFrame not updating with Background change

I created a class were bosex are displayed, and it reads where you click, and the handler changes the background color based on the location of you click, Everything works except for the updating of the JFrame. 我创建了一个类,显示了bosex,并且它显示了你单击的位置,并且处理程序根据你单击的位置更改了背景颜色,除了更新JFrame之外,Everything都有效。 I tried 3 different lines of code, as shown in the handler. 我尝试了3行不同的代码,如处理程序所示。 Code: 码:

public class Dynamic_Bg_Color extends JFrame{

private static final long serialVersionUID = 1L;

JFrame frame;

Handler handler = new Handler();

static Dimension size = new Dimension(500,400);

public Dynamic_Bg_Color(){


    frame = new JFrame("BG Color Changer");

    frame = this;

    setBackground(Color.cyan);

    frame.addMouseListener(handler);

}

public void paint(Graphics g1){

    System.out.println("Click");

    Graphics g = (Graphics)g1;

    g.setColor(Color.blue);
    g.fillRect(20,20,frame.getWidth()-40,100);

    g.setColor(Color.green);
    g.fillRect(20,140,frame.getWidth()-40,100);

    g.setColor(Color.orange);
    g.fillRect(20,260,frame.getWidth()-40,100);
}

public static void main(String[] args){

    Dynamic_Bg_Color d = new Dynamic_Bg_Color();

    d.frame.setPreferredSize(new Dimension(size));
    d.frame.setMinimumSize(new Dimension(size));
    d.frame.setMaximumSize(new Dimension(size));
    d.frame.setLocationRelativeTo(null);

    d.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    d.frame.setLayout(new FlowLayout());
    d.frame.setVisible(true);
}

public class Handler implements MouseListener{

    public void mouseClicked(MouseEvent e) {

    }
    public void mousePressed(MouseEvent e) {

        int x = e.getX();
        int y = e.getY();

        if(x>= 20 && x<=frame.getWidth()-40 && y>=20 && y<= 120){
            setBackground(Color.blue);
        }
        if(x>= 20 && x<=frame.getWidth()-40 && y>=140 && y<= 240){
            getContentPane().setBackground(Color.green);
        }
        if(x>= 20 && x<=frame.getWidth()-40 && y>=260 && y<= 360){
            frame.getContentPane().setBackground(Color.orange);
        }
        repaint();
    }

//Implemented Methods, cut to shorten, no code in them//

}

}
  1. Don't override paint of top level containers. 不要覆盖顶级容器的paint They are responsible for maintaining a complex paint chain (which you have now broken) and they aren't double buffered which can result in flickering when painted. 他们负责维护一个复杂的油漆链(你现在已经破坏了)并且它们没有双重缓冲,这可能导致涂漆时闪烁。
  2. Do call super.paintXxx . 请拨打super.paintXxx

Instead of extending JFrame , use JPanel . 而不是扩展JFrame ,使用JPanel Instead of overriding paint , override paintComponent . 而不是覆盖paint ,重写paintComponent Call super.paintComponent before you do any other custom painting. 在执行任何其他自定义绘制之前调用super.paintComponent It is responsible for painting the background 它负责绘制背景

Finally, add this panel to a instance of JFrame ... 最后,将此面板添加到JFrame的实例中......

Take a look at Performing Custom Painting for more details 有关更多详细信息,请参阅执行自定义绘画

Updated with runnable example 更新了runnable示例

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Dynamic_Bg_Color extends JPanel {

    private static final long serialVersionUID = 1L;

    public Dynamic_Bg_Color() {
        setBackground(Color.cyan);
        addMouseListener(new Handler());
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 500);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        System.out.println("Click");

        g.setColor(Color.blue);
        g.fillRect(20, 20, getWidth() - 40, 100);

        g.setColor(Color.green);
        g.fillRect(20, 140, getWidth() - 40, 100);

        g.setColor(Color.orange);
        g.fillRect(20, 260, getWidth() - 40, 100);
    }

    public class Handler extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {

            int x = e.getX();
            int y = e.getY();

            System.out.println(x + "x" + y);

            if (x >= 20 && x <= getWidth() - 40 && y >= 20 && y <= 120) {
                setBackground(Color.blue);
            }
            if (x >= 20 && x <= getWidth() - 40 && y >= 140 && y <= 240) {
                setBackground(Color.green);
            }
            if (x >= 20 && x <= getWidth() - 40 && y >= 260 && y <= 360) {
                setBackground(Color.orange);
            }
            repaint();
        }

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Dynamic_Bg_Color d = new Dynamic_Bg_Color();

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(d);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

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

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