设计思路是一个大的Jpanel(继承Frame)上布局有一些常规控件,另外还添加了一个小Jpanel,这个Jpanel上用于绘制出点(点坐标已有)。效果是点击某一个按钮,小Jpanel上绘制出点。问题是运行后Frame下的组件都能显示,但点击按钮小Jpanel绘出的点一闪即逝,或是根本不显示。试过将绘制方法重写并放入另一线程中,仍然不能解决。 急!!!望大神们不吝赐教,谢谢Java控件

解决方案 »

  1.   

    我把重写的paint方法放在另一线程里并获取点数据不知道怎么又能绘出来了,但是每次窗体最小化后图像消失了,这好像是个问题。能解决吗?
      

  2.   

            /**双缓冲画布*/
    private Image memoryImage;
    /**双缓冲画笔*/
    private Graphics memoryGraphics;//重新开辟一块内存区域,用于存放叠加后的图片
    this.memoryImage = this.createImage(900,700);
    //从双缓冲画布当中得到双缓冲画笔,这样通过双缓冲画笔绘制的图片就会叠加在双缓冲画布中
    this.memoryGraphics = this.memoryImage.getGraphics();
    //绘制文字
    this.memoryGraphics.drawString("分数:"+pointer, 100, 57);
    //绘制图片
    this.memoryGraphics.drawImage(hartNumImg,50,80,30,25,this);
      

  3.   

    谢谢大家的意见。我把代码贴出来大家看看吧,重写的paint方法如下:
    class Mypaintpanel extends JPanel implements Runnable
    {
    private static final long serialVersionUID = 1L;
    public Mypaintpanel()
    {
        super();
    }
    public void paint(Graphics g)
    {
    super.paint(g);
                            ArrayList<PointStruct> point = new ArrayList<PointStruct>();
                            for(int i = 0;i<point.size();i++)
    {
    g.setColor(Color.RED);
    g.drawString("@",X[i],Y[i]);
    }
                     }
                    public void run()
    {
    mypanel.paint(panelforshow.getGraphics());//panelforshow是显示图像的                          panel
    mypanel.repaint();
    }
    }
    点的数据结构什么的就不列出来了。调用的代码:
                            /**处理绘图的线程*/
    Runnable rb = new Mypaintpanel();
    Thread td = new Thread(rb);
    td.start();
    结构就是大的Jpanel上布局一些控件,另外小的panel是用来显示的。点击按钮后启动线程,大家可以看看代码吧
      

  4.   

    感觉你的Panel使用是出现了问题
    大的Jpanel,小的panel,mypanel,panelforshow 哪个是Mypaintpanel的对象?
    另外,repaint()将自动调用paint(Graphics g),而这里的参数 g 是自身的!!即mypanel.repaint();
    是在mypanel上画
    mypanel.paint(panelforshow.getGraphics());//panelforshow是显示图像的        panel
    mypanel.repaint();
    如果上面的第2句去掉,不知什么效果
    不建议使用mypanel.paint(panelforshow.getGraphics());的形式,最好panelforshow.repaint();
      

  5.   

    如果去掉的话什么都没显示了,那两句是run方法里面的,如果不用的话那不是重写的pain方法都没有了,到底该不该重写pain方法呢?
      

  6.   

    不知道你是怎么处理,我一般是自己另外实现一个JPanle,控制逻辑,自己drawimport javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;class MyJPanle extends JPanel
    {
    @Override
    public void paintComponent(Graphics g) 
    {
    super.paintComponent(g);
    //话一个区域
    g.setColor(Color.YELLOW);
    g.fillRect(20,20,50,80);
    //画一行文字
    g.setColor(Color.BLACK);
    g.setFont(new Font("Times New Roman",Font.BOLD,20));
    g.drawString("The National GeoGraphics",40,50);
    }
    }public class JPanelTest extends JFrame
    {
    private JPanel big;
    private JPanel small; public JPanelTest()
    {
    //自由定位
    setLayout(null);
    setBounds(0,0,800,600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    big = new JPanel();
    big.setBounds(0,0,500,500);
    //为了方便看,给Big画一个边框
    Border lb = BorderFactory.createLineBorder(Color.ORANGE,7);
    big.setBorder(lb);
    add(big); Border lb2 = BorderFactory.createLineBorder(Color.ORANGE,7); 
    //让小panel 是自己实现的Panel
    small = new MyJPanle();
    small.setBounds(50,50,300,200);
    small.setBorder(lb2);
    big.setLayout(null);
    big.add(small); //在samll panel 里面画东西


    setVisible(true);
    } public static void main(String[] args) 
    {
    JPanelTest f = new JPanelTest();
    System.out.println("Hello World!");
    }
    }
      

  7.   

    和你实现的没啥不一样 ,加了按钮进行测试import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;class MyJPanle extends JPanel
    {
    @Override
    public void paintComponent(Graphics g) 
    {
    super.paintComponent(g);
    if(JPanelTest.flag)
    {
    //话一个区域
    g.setColor(Color.YELLOW);
    g.fillRect(20,20,50,80);
    //画一行文字
    g.setColor(Color.BLACK);
    g.setFont(new Font("Times New Roman",Font.BOLD,20));
    g.drawString("The National GeoGraphics",40,50);
    }
    else
    {
    //话一个区域
    g.setColor(Color.YELLOW);
    g.fillRect(20,20,50,80);
    //画一行文字
    g.setColor(Color.BLACK);
    g.setFont(new Font("宋体",Font.BOLD,20));
    g.drawString("国家地理杂志",40,50);
    } }
    }public class JPanelTest extends JFrame
    {
    public static boolean flag;
    private JPanel big;
    private JPanel small;
    private JButton btnAction; static{
    flag = true;
    }

    public JPanelTest()
    {
    //自由定位
    setLayout(null);
    setBounds(0,0,800,600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    big = new JPanel();
    big.setBounds(0,0,500,500);
    //为了方便看,给Big画一个边框
    Border lb = BorderFactory.createLineBorder(Color.ORANGE,7);
    big.setBorder(lb);
    add(big);
    Border lb2 = BorderFactory.createLineBorder(Color.ORANGE,7); 
    //让小panel 是自己实现的Panel
    small = new MyJPanle();
    small.setBounds(50,50,300,200);
    small.setBorder(lb2);
    big.setLayout(null);
    big.add(small); //在samll panel 里面画东西
    btnAction = new JButton("Click Me");
    btnAction.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    JPanelTest.flag = !JPanelTest.flag;
    small.repaint();
    }
    });
    btnAction.setBounds(10,10,100,30);
    big.add(btnAction);


    setVisible(true);
    } public static void main(String[] args) 
    {
    JPanelTest f = new JPanelTest();
    System.out.println("Hello World!");
    }
    }
      

  8.   

    我仔细看了你的代码,我想我发现问题出在那里了。刚开始的时候想在大Panel上的小Panel上绘图,没有重写pain方法,所有不行。然后写了个类复写了pain方法,获得小Panel的getGraphics()来调用pain方法。楼上大哥说的“不建议使用mypanel.paint(panelforshow.getGraphics());的形式,最好panelforshow.repaint();”非常有道理,所以直接使用panelforshow.repaint()。还需要注意的是实例化小panel的对象panelforshow时,用自己写的类(其中包含复写的pain方法)来构造,用new Jpanel()构造的话绘图时也会出现图像时有时无的情况。感谢大家积极回复,好人一生平安!哈哈