JPanel 的背景颜色在添加 paint() 方法后被替换为灰色。

huangapple go评论78阅读模式
英文:

Background color for JPanel replaced by grey when method paint() added

问题

我编写了以下基本的Java Swing示例,其中包含一个具有蓝色背景的JPanel:

public class Chart1 extends JFrame {
    
    private MainPanel1 main;    
    
    public Chart1() throws InterruptedException {
        
        setSize(600,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        // 创建并添加主面板
        main = new MainPanel1();
        this.getContentPane().add(main, BorderLayout.CENTER);
        
        // 绘制窗口
        this.setVisible(true);
                        
    }
    
}

public class MainPanel1 extends JPanel {        
    
    public MainPanel1() {
        
        setBackground(Color.BLUE);
        
    }
}

我得到了以下结果:

JPanel 的背景颜色在添加 paint() 方法后被替换为灰色。

到目前为止一切都好。

现在我添加一个paint()方法。源代码如下:

public class MainPanel1 extends JPanel {        
    
    public MainPanel1() {
        
        setBackground(Color.BLUE);
        
    }
    
    public void paint(Graphics g) {
    }

}

然后,即使在paint()中什么都不做,我也会得到一个灰色的背景,为什么?如何修复这个问题?

JPanel 的背景颜色在添加 paint() 方法后被替换为灰色。

英文:

I code the following basic Java Swing example which is a JPanel with blue background:

<!-- language: java -->

public class Chart1 extends JFrame {
    
    private MainPanel1 main;    
    
    public Chart1() throws InterruptedException {
        
        setSize(600,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        // CREATE AND ADD MAIN PANEL
        main = new MainPanel1();
        this.getContentPane().add(main, BorderLayout.CENTER);
        
        // DRAW WINDOW
        this.setVisible(true);
                        
    }
    
}

<!-- language: java -->

public class MainPanel1 extends JPanel {        
    
    public MainPanel1() {
        
        setBackground(Color.BLUE);
        
    }
}

I get the following result:

JPanel 的背景颜色在添加 paint() 方法后被替换为灰色。

So far, so good.

Now I add a paint() method. The source code is as follows:

<!-- language: java -->

public class MainPanel1 extends JPanel {        
    
    public MainPanel1() {
        
        setBackground(Color.BLUE);
        
    }
    
    public void paint(Graphics g) {
    }

}

Then even without doing anything in paint() I get a grey background, why? how can I fix this?

JPanel 的背景颜色在添加 paint() 方法后被替换为灰色。

答案1

得分: 1

答案是,paint(一个 Java 1.0 - 1.1 的方法)调用了 JComponent 中的 paintBackground 方法。当你重写它时,它没有调用所有的 Swing 绘制方法。但如果你添加 super.paint(g),它会看起来和之前一样。

另外,请注意,JFrame 中默认的 ContentPane 已经是一个 JPanel。与其替换 ContentPane 为你的 JPanel,你只需调用:

((JPanel) myFrame.getContentPane()).setBackground(Color.blue);
英文:

The answer is that paint (a java 1.0 - 1.1 method) calls paintBackground in JComponents. When you overrode it, it isn't calling all of the swing paint methods. But if you add super.paint(g), it will look like it did before.

Also - please note that the default ContentPane in JFrame is already a JPanel. Rather than replacing the ContentPane with your JPanel, you could just call:

((JPanel) myFrame.getContentPane()).setBackground(Color.blue);

答案2

得分: 1

你不应该覆盖 paint 方法,而应该覆盖 paintComponent 方法。问题仍然会出现,所以你需要调用 super.paintComponent(g)

所以,请将你的 paint 方法更改如下。

public class MainPanel1 extends JPanel {        
    public MainPanel1() {
        setBackground(Color.BLUE);
    }
    
    public void paintComponent(Graphics g) {
         // 以下语句确保背景颜色将被应用,
         // 以及其他用于图形的准备工作。
         super.paintComponent(g);
         // 如果有其他图形代码,在此处添加。
   }
}

并且在 Chart1 类中不要再继承 JFrame。这是不好的做法。使用一个实例。

JFrame frame = new JFrame();
英文:

You should not be overriding paint but paintComponent. The problem would still occur so you need to invoke super.paintComponent(g)

So Change your paint method to the following.

public class MainPanel1 extends JPanel {        
    
    public MainPanel1() {
        setBackground(Color.BLUE);
    }
    
    public void paintComponent(Graphics g) {
         // The following statement ensures that
         // the background color will be applied
         // as well as other preparations for 
         // doing graphics.
 
         super.paintComponent(g);
         // If you have other graphics
         // code, add it here.
   }

}

And do not subclass JFrame in the Chart1 class. It is bad practice. Use an instance.

JFrame frame = new JFrame();


</details>



huangapple
  • 本文由 发表于 2020年9月28日 06:22:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64093852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定