gpt4 book ai didi

java - 为什么在这个简单的程序中 setBackground 不立即并一致地更新背景颜色

转载 作者:行者123 更新时间:2023-12-01 10:19:57 34 4
gpt4 key购买 nike

我有一个非常简单的程序,要求用户单击其背景颜色与较大面板(又名显示面板)的背景颜色相匹配的四个面板之一,该面板随机将其背景颜色设置为四个面板之一。如果面板错误点击Joptionpane就出来了;显示面板背景暂时设置为黑色。如果单击"is",显示面板应重新设置其背景颜色。问题是我并不总是看到背景被更新。相反,有时背景保持黑色。 ...更令人困惑的是,如果您将另一个窗口拖到该程序的窗口上,它会保持黑色,如果您看到颜色在显示面板上移动时部分更新,或者只是切换到不同的窗口并重新聚焦,那么您会看到完成更新的背景颜色。

那么为什么 setMethod 会被调用,但只是偶尔由幕后的 Paint 方法执行呢?为什么类型转换其他 window 或框架使其可见?它与正在处理的鼠标单击事件有什么关系吗?

我很感谢对这一切的任何解释,谢谢大家

public class MainPanel extends JPanel{


Subpanel panel1;
Subpanel panel2;
Subpanel panel3;
Subpanel panel4;
Subpanel displaypanel;

Color[] color; //stores all the colors that display on the subpanels


public static void main(String[] args) {
JFrame window=new JFrame("This is a test");
window.setContentPane(new MainPanel());
window.setLocation(100,30);
window.setSize(600,500);
window.setVisible(true);

}


public MainPanel(){


setLayout(new FlowLayout(FlowLayout.CENTER,80,30));


panel1= new Subpanel();
panel2= new Subpanel();
panel3= new Subpanel();
panel4= new Subpanel();

//the big sub panel
displaypanel= new Subpanel();

color=new Color[4];
color[0]=Color.BLUE;
color[1]=Color.RED;
color[2]=Color.YELLOW;
color[3]=Color.GREEN;



setBackground(Color.GRAY);
displaypanel.setBackground(displayRandomColor());
panel1.setBackground(color[0]);
panel2.setBackground(color[1]);
panel3.setBackground(color[2]);
panel4.setBackground(color[3]);

displaypanel.setPreferredSize(new Dimension(400,250));
panel1.setPreferredSize(new Dimension(70,70));
panel2.setPreferredSize(new Dimension(70,70));
panel3.setPreferredSize(new Dimension(70,70));
panel4.setPreferredSize(new Dimension(70,70));

add(displaypanel);
add(panel1);
add(panel2);
add(panel3);
add(panel4);





}


public void paintComponent(Graphics g){
super.paintComponent(g);



}

public Color displayRandomColor(){
Color i=Color.WHITE;
switch ((int)(Math.random()*4)+1){
case 1:
i= Color.YELLOW;
break;

case 2:
i= Color.BLUE;
break;

case 3:
i= Color.GREEN;
break;

case 4:
i= Color.RED;
}
return i;
}


public class Subpanel extends JPanel{

public Subpanel(){
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent evt){
Component source=(Component)evt.getSource();

if((source.getBackground()).equals(displaypanel.getBackground())){
//do nothing for this test..
}
else{
displaypanel.setBackground(Color.BLACK);

//ask user to reset the background color
//**the following 2 lines introduces the problem
if(JOptionPane.showOptionDialog(null,"click Yes to see a new Color","Incorrect",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE,null,null,null)==JOptionPane.YES_OPTION){
displaypanel.setBackground(displayRandomColor());

}
}

}





});
} //end of constructor
public void paintComponent(Graphics g){
super.paintComponent(g);

}

}

}

最佳答案

synchronized correctly时,您的程序运行得相当好。 。一些注意事项:

  • 在程序执行时尽可能早地初始化对象一次。

  • 使用Random实例获取随机整数;请注意 displayRandomColor() 的实现要简单得多。

    private Color displayRandomColor() {
    return color[r.nextInt(color.length)];
    }
  • Swing GUI 对象应该event dispatch thread 上构建和操作。 .

  • 当您确实想要覆盖 getPreferredSize() 时,请勿使用 setPreferredSize()],如建议 here .

  • 使用layoutspack() 封闭窗口。

image

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MainPanel extends JPanel {

private static final Random r = new Random();
private final JPanel displayPanel;
private Color[] color = {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW};
private Subpanel panel1 = new Subpanel(color[0]);
private Subpanel panel2 = new Subpanel(color[1]);
private Subpanel panel3 = new Subpanel(color[2]);
private Subpanel panel4 = new Subpanel(color[3]);

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
JFrame window = new JFrame("This is a test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(new MainPanel());
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
});
}

public MainPanel() {
setLayout(new BorderLayout());
setBackground(Color.GRAY);

//the big sub panel
displayPanel = new JPanel() {

@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
};
displayPanel.setBackground(displayRandomColor());

//the control panel
JPanel p = new JPanel();
p.add(panel1);
p.add(panel2);
p.add(panel3);
p.add(panel4);

add(displayPanel, BorderLayout.CENTER);
add(p, BorderLayout.SOUTH);
}

private Color displayRandomColor() {
return color[r.nextInt(color.length)];
}

public class Subpanel extends JPanel {

public Subpanel(Color color) {
this.setBackground(color);
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent evt) {
Component source = (Component) evt.getSource();
if ((source.getBackground()).equals(displayPanel.getBackground())) {
System.out.println(source.getBackground());
} else {
displayPanel.setBackground(Color.BLACK);
if (JOptionPane.showOptionDialog(null,
"Click Yes to see a new Color", "Incorrect",
JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, null, null) == JOptionPane.YES_OPTION) {
displayPanel.setBackground(displayRandomColor());
}
}
}
});
}

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

关于java - 为什么在这个简单的程序中 setBackground 不立即并一致地更新背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35663241/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com