import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;public class Draw extends JFrame{
JPanel  jPanel=new JPanel();
public Draw() {

            jPanel.setBackground(Color.red);
            add(jPanel); 
   Drawation drawaction=new Drawation();//添加画图,把上面jpanel的设置给覆盖了;要是先添加画图再添加
   add(drawaction);                    //jpanel则把画图覆盖了
  
}
public static void main(String[] args){
             Draw draw=new Draw();
         draw.setTitle("abc");
    draw.setSize(300,300);
    draw.setVisible(true);
    draw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}
class Drawation extends JPanel{
   public void paintComponent(Graphics g){
     super.paintComponents(g);
 g.drawString("agagh", 50, 45);
   }
}
这只是一段简单的验证代码,没什么意义。就是想了解怎么才能在动态画图的同时可以保留原有jpanel上的各种组件,而且可以在其上画图

解决方案 »

  1.   

    在一个位置添加组件,后来的会顶替前面的。1.7前可以使用 JXLayer, 1.7可以使用JLayer。
      

  2.   

    JLayer 的例子。JXLayer(从 swinglabs网站下载)的类似,JLayer就是 JXLayer整合到java 7的结果。
    import javax.swing.*;
     import javax.swing.plaf.LayerUI;
     import java.awt.*; public class JLayerSample {     private static JLayer<JComponent> createLayer() {
             // This custom layerUI will fill the layer with translucent green
             // and print out all mouseMotion events generated within its borders
             LayerUI<JComponent> layerUI = new LayerUI<JComponent>() {             public void paint(Graphics g, JComponent c) {
                     // paint the layer as is
                     super.paint(g, c);
                     // fill it with the translucent green
                     g.setColor(new Color(0, 128, 0, 128));
                     g.fillRect(0, 0, c.getWidth(), c.getHeight());
                 }             public void installUI(JComponent c) {
                     super.installUI(c);
                     // enable mouse motion events for the layer's subcomponents
                     ((JLayer) c).setLayerEventMask(AWTEvent.MOUSE_MOTION_EVENT_MASK);
                 }             public void uninstallUI(JComponent c) {
                     super.uninstallUI(c);
                     // reset the layer event mask
                     ((JLayer) c).setLayerEventMask(0);
                 }             // overridden method which catches MouseMotion events
                 public void eventDispatched(AWTEvent e, JLayer<? extends JComponent> l) {
                     System.out.println("AWTEvent detected: " + e);
                 }
             };
             // create a component to be decorated with the layer
             JPanel panel = new JPanel();
             panel.add(new JButton("JButton"));         // create the layer for the panel using our custom layerUI
             return new JLayer<JComponent>(panel, layerUI);
         }     private static void createAndShowGUI() {
             final JFrame frame = new JFrame();
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         // work with the layer as with any other Swing component
             frame.add(createLayer());         frame.setSize(200, 200);
             frame.setLocationRelativeTo(null);
             frame.setVisible(true);
         }     public static void main(String[] args) throws Exception {
             SwingUtilities.invokeAndWait(new Runnable() {
                 public void run() {
                     createAndShowGUI();
                 }
             });
         }
     }
      

  3.   


    是像下面这样吗?http://happy.host898.net/java/album/index.html
      

  4.   

    are you going to draw background image inside JFrame or what you means
      

  5.   

    差不多是这个意思吧。不过这个是插入的图片吗,我想用graphics画图。不知这两个性质一样不
      

  6.   

    at first you should insert the definit earticle that is “the” before JFrame,and then if you want to ask me you should say what do you mean rather than what you means,last something that you are not good at ,don‘t come out to make a fool of yourself  
      

  7.   

    你用一个 JPanel 作为最底层容器,然后再在这个 JPanel 上画图不就行了?重写 paintComponent 方法,
    第一句调用 super.paintComponent(g) 就行
    这个方法实现了双缓冲,不会覆盖 JPanel 上原有的控件
      

  8.   

    你可以画在GlassPane上 之前的组件不是在contentPane上么 这样就不会覆盖了 你还可以控制GlassPane显示与否
      

  9.   


    我说的和你写的那个意思有点区别,把你的类改了下,你看看效果import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class Draw extends JFrame {

    MainPane mainPane; public Draw() {
    setTitle("abc");
    setSize(300, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setContentPane(getMainPane());
    setVisible(true);
    }

    /**
     * 初始化并返回底层面板,
     * 你所有的其他控件都应该添加在这个底层面板上
     * @return
     */
    protected JPanel getMainPane() {
    if (mainPane == null) {
    mainPane = new MainPane();
    mainPane.add(new JButton("1"));
    mainPane.add(new JButton("2"));
    mainPane.add(new JButton("3"));
    mainPane.add(new JButton("4"));
    mainPane.add(new JButton("5"));
    mainPane.add(new JButton("6"));
    }
    return mainPane;
    } public static void main(String[] args) {
    new Draw();
    }

    class MainPane extends JPanel {

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g;
    int width = this.getWidth();
    int height = this.getHeight();
    g2d.setPaint(new GradientPaint(0, 0, Color.white, 
    width, height, Color.cyan));
    g2d.fillRect(0, 0, width, height);
    g2d.setColor(Color.black);
    g.drawString("agagh", 50, 45);
    }
    }
    }
      

  10.   

    check your own question and make it more readable and clear
    firstly before you posted the thread!!
    i could not catch on what you said at all...the intent of the comments is not to beat you
    just beat someone who regard others as a fool
      

  11.   

    谢谢你的回答,那样子确实实现了不覆盖,达到了一定目的。如果想在程序执行的动态中画图。比如有个判定条件成立则作图,不成立则取消。我大体做了,但是没哟判定条件的一段代码。可是说存在异常
    import java.awt.Graphics;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class Draw extends JFrame{ private JPanel jPanel=new JPanel();
    Graphics g=jPanel.getGraphics();
    public Draw() {
            g.drawString("agagh", 50, 45);
    jPanel.add(new JButton("d"));
    setContentPane(jPanel);
    }

    public static void main(String[] args){
        
    Draw draw=new Draw();
    draw.setTitle("赵威制作");
    draw.setSize(300,300);
    draw.setVisible(true);
    draw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

    }
      

  12.   

    学习…………我也在纠结,还有我画图是用了一个for循环,结果画的图闪一下就没有了