简体   繁体   English

使用JSlider的JPanel不会替换图形

[英]JPanel with JSlider not displaing the Graphics

All i have is a JPanel with a white background and the JSlider on the bottom, not displaying the square, i think i have made some mistake with the JPanel class but i can't figure it out. 我所拥有的只是一个带有白色背景的JPanel,底部是JSlider,没有显示正方形,我想我对JPanel类犯了一些错误,但是我无法弄清楚。 Just before i made another project with g.fillOval and it worked properly, i checked it out and every line of code seems the same, i am really confused. 在我使用g.fillOval进行另一个项目并正常工作之前,我将其签出,每一行代码似乎都一样,我真的很困惑。

public class Main00 {

    public static void main(String[] args) {
        Gui asd=new Gui();
        asd.setVisible(true);
        asd.setSize(500,400);
    }
}  

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

public class Gui extends JFrame {

    private JSlider slider;
    private DrawSquare square;

    public Gui() {
        super("square modificator");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        square = new DrawSquare();
        square.setBackground(Color.WHITE);
        slider = new JSlider(SwingConstants.HORIZONTAL, 0, 300,
                    square.getSide());
        slider.setMajorTickSpacing(20);
        slider.setPaintTicks(true);
        add(square);
        add(slider, BorderLayout.SOUTH);
        slider.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                square.setSide(slider.getValue());
            }
        });
    }
}

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

public class DrawSquare extends JPanel {

    private int side = 10;

    public void paintComponents(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.RED);
        g.fillRect(20, 20, side, side);
    }

    public void setSide(int side) {
        this.side=(side>=0)?side:0;
        repaint();
    }

    public Dimension getPrefferedSize(){
        return new Dimension(200,200);
    }

    public Dimension getMinimumSize(){
        return getPrefferedSize();
    }

    public int getSide(){
        return side;
    }
}

You're overriding paintComponents rather than the correct paintComponent . 您要覆盖paintComponents而不是正确的paintComponent These two methods have drastically different effects, and the effect of the second is the one you want. 这两种方法的效果截然不同,第二种方法的效果是您想要的。

From the API: 从API:

  • paintComponents API entry : Paints each of the components in this container. paintComponents API条目 :绘制此容器中的每个组件。
  • paintComponent API entry : Calls the UI delegate's paint method, if the UI delegate is non-null. paintComponent API条目 :如果UI委托为非null,则调用UI委托的paint方法。 We pass the delegate a copy of the Graphics object to protect the rest of the paint code from irrevocable changes 我们传递给委托人Graphics对象的副本,以保护其余的绘画代码免受不可撤消的更改

Again, you are interested in painting the component itself via its delegate and not in painting the components held by this component. 同样,您有兴趣通过组件的代理绘制组件本身,而不是绘制由该组件持有的组件。

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

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