Java 使用repaint()进行奇怪的重新定位;

Java 使用repaint()进行奇怪的重新定位;,java,swing,repaint,Java,Swing,Repaint,我在JPanel中有一个JSlider,它返回我一个值R-G-B。 我在JPanel的构造中创建了它。我在同一个面板上(使用paintComponent)画了一个小圆圈,并使用滑块更改了他的颜色。我想在滑块移动的同时改变颜色。 因此,我使用重新绘制的方法。。面板旁边是另一个面板,有两个按钮。。如果我在第一个面板中使用方法repaint,则第二个面板的按钮在第一个面板的左上角重复。为什么?谢谢你 第一小组: public class OptionsPanel extends JPanel {

我在JPanel中有一个JSlider,它返回我一个值R-G-B。 我在JPanel的构造中创建了它。我在同一个面板上(使用paintComponent)画了一个小圆圈,并使用滑块更改了他的颜色。我想在滑块移动的同时改变颜色。 因此,我使用重新绘制的方法。。面板旁边是另一个面板,有两个按钮。。如果我在第一个面板中使用方法repaint,则第二个面板的按钮在第一个面板的左上角重复。为什么?谢谢你

第一小组:

public class OptionsPanel extends JPanel {


static JSlider RBG = new JSlider(0,255);
OptionsPanel(){

this.setVisible(false);
this.setSize(350,1000);
this.setLayout(null);
this.setBackground(new Color(200,200,0));
Main.f1.add(this); 



 RBG.setVisible(true);
 RBG.setSize(255,50);
 RBG.setLocation(30,240);


 this.add(RBG);



LotL lotl = new LotL();
Button save = new Button("Save");

save.setVisible(true);
save.setSize(100,40);
save.setLayout(null);
save.setLocation(60,300);
save.addActionListener(lotl); 
save.setBackground(Color.yellow);
save.identificatore=3;
this.add(save);







  }


  boolean draw=false;

  @Override
 public void paintComponent(Graphics g){



 g.drawOval(50,100,70,70);

 g.setColor(new Color(RBG.getValue(),180,200));
 g.fillOval(50,100,70,70);


 repaint();




}
}
第二小组:

public class FirstPanel extends JPanel{
FirstPanel(){

this.setVisible(true);
this.setSize(1000,1000);
this.setLayout(null);
this.setBackground(new Color(255,200,180)); 
Main.f1.add(this);

Button start = new Button("Start Game!");


Button options = new Button("Options");
LotL LotL = new LotL();  



start.setVisible(true);
start.setSize(200,80);
start.setLayout(null);
start.setLocation(400,450);
start.addActionListener(LotL); 
start.setBackground(Color.green);
start.identificatore=1;
this.add(start);

options.setVisible(true);
options.setSize(200,70);
options.setLayout(null);
options.setLocation(400,550);
options.addActionListener(LotL); 
options.setBackground(Color.green);
options.identificatore=2;
this.add(options);

}


}

你打破了油漆链

@Override
public void paintComponent(Graphics g){

    g.drawOval(50,100,70,70);

    g.setColor(new Color(RBG.getValue(),180,200));
    g.fillOval(50,100,70,70);


    repaint();

}
Graphics
是一个共享资源,在给定的绘制周期内,它将传递给所有绘制的组件

paintComponent
的工作之一是准备用于绘制的
Graphics
上下文,但填充组件的背景色

在执行任何自定义绘制之前,必须调用
super.paintComponent

@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawOval(50,100,70,70);

    g.setColor(new Color(RBG.getValue(),180,200));
    g.fillOval(50,100,70,70);
}
另外,永远不需要
paintComponent
成为
public
,任何人都不应该直接调用,也不应该从任何可能触发重新绘制的绘制方法中修改组件的状态,您将陷入一个无限循环,最终会消耗CPU并使计算机无法使用

查看和了解更多详细信息


您还应该避免使用
null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃它们将导致无止境的问题,您将花费越来越多的时间试图纠正这些问题,因为您使用的是Button而不是JButton。为您的问题提供一个最小、完整且可运行的示例,您可能会得到更多帮助。
save.identificatore=3那个按钮是什么?这是你的自定义类吗?它扩展了什么?抱歉,您有理由按钮是JButton的扩展类,我只添加了ad属性'id'identificatore。。谢谢,你疯了!现在一切都好了!