简体   繁体   English

禁用JPanel上的绘画

[英]Disable painting on a JPanel

I'm trying to completely disable all painting and refreshing on a portion of a JFrame. 我试图完全禁用JFrame的一部分上的所有绘画和刷新。 I got the desired effect on the entire JFrame by simply overriding public void paint(Graphics) like so: 通过像这样简单地重写public void paint(Graphics)我在整个JFrame上获得了预期的效果:

import javax.swing.*;

class Test extends JFrame {

  Test () {
    setBounds(20,20, 100,100);
    setVisible(true);
    }

  //This disables all painting and refreshing ON A JFRAME.
  //Just doing this on a JPanel doesn't work.
  public void paint (Graphics g) {}

  public static void main (String[] args)
    { new Test(); }

  }

I want this same effect, but only on a particular region of the JFrame. 我想要同样的效果,但是只在JFrame的特定区域上。 I want to be able to add GUI components like normal to the rest of the frame. 我希望能够将正常的GUI组件添加到框架的其余部分。 I've tried disabling double buffering (using JPanel's constructor) and overriding the following methods (extending both JPanel and JComponent) like so: 我试过禁用双缓冲(使用JPanel的构造函数)并覆盖以下方法(扩展JPanel和JComponent),如下所示:

public class DontRefresh extends JComponent/JPanel {
  public void paint (Graphics g) {}
  public void paintComponent (Graphics g) {}
  public void repaint () {}
  public void update (Graphics g) {}
  public void updateUI () {}
  }

and i also tried disabling refresh via: 我也尝试通过以下方式禁用刷新:

DontRefresh component = new DontRefresh();
RepaintManager.currentManager(component).markCompletelyClean(component);

but nothing worked. 但没有任何效果。

Well, without knowing exactly what you're doing I would recommend you have a "filler panel" (just a JPanel you don't do anything with) in the space you don't want anything to appear and then have other panels for everything else. 好吧,在不确切知道自己在做什么的情况下,我建议您在不希望出现任何内容的空间中拥有一个“填充面板”(只是您不执行任何操作的JPanel),然后为所有内容使用其他面板其他。

So say you have a JFrame. 假设您有一个JFrame。 If you wanted the top right corner to never display anything, you could fill the JFrame with 4 JPanels, one in each corner (or 3 with one on the left, one in the top right corner and one in the bottom right corner). 如果希望右上角从不显示任何内容,则可以用4个JPanel填充JFrame,每个JPanel填充一个(或者3个,其中一个在左侧,一个在右上角,一个在右下角)。 Then put all you swing components in the other panels. 然后将所有摆动组件放到其他面板中。 I don't know if this will accomplish your purpose, but I'm not totally certain what you're purpose is, so that's the best I can do :) I hope it helps! 我不知道这是否可以实现您的目标,但是我不确定您的目标是什么,所以这是我能做的最好的事情:)我希望它会有所帮助!

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

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