2 lizhizuoyongzhe lizhizuoyongzhe 于 2015.06.09 14:36 提问

急求怎么实现单击按钮重绘面板啊 1C

就是我写了一个jframe类 里面定义了a1,a2,a3等几个面板,然后我又写了一个JPanel类,jpanel类写了一个paint()方法,主要是实现画一条曲线的功能,然后我在jframe类里面定义a1=new jpanel();然后运行jframe 的时候a1面板就自动加载了一天曲线,我现在想实现的是 在jframe里面加上一个按钮,单击按钮,给jpanel传参--也就是传入两个数组分别作为x、y轴的数据 然后重绘,同时单击按钮后,要面板的曲线重绘成传入数据后的曲线; 虽然我知道好像是用repaint()的方法 ,但我第一次接触这些东西 ,还请大神帮帮忙

3个回答

qq_21733941
qq_21733941   2015.06.10 17:20

class O extends JPanel {
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);

g.fillOval(11, 11, 11, 11);

this.repaint();

}
}
大概就这样一个模型吧

tongyi55555
tongyi55555   2015.06.18 11:52
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BtnFrame extends JFrame {

    public BtnFrame() {
        this.setLayout(new BorderLayout());
        this.setSize(400, 400);
        final A1Panel a1 = new A1Panel();
        this.getContentPane().add(a1, BorderLayout.CENTER);
        JPanel a2 = new JPanel();
        this.getContentPane().add(a2, BorderLayout.NORTH);
        JPanel a3 = new JPanel();
        this.getContentPane().add(a3, BorderLayout.SOUTH);
        JButton btn = new JButton("更新数据");
        a3.add(btn);
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int[] xValue = new int[10];
                int[] yValue = new int[10];
                for (int i = 0; i < yValue.length; i++) {
                    xValue[i] = (int) (Math.random() * 400);
                    yValue[i] = (int) (Math.random() * 400);
                }
                // TODO Auto-generated method stub
                a1.updateData(xValue, yValue);
                a1.repaint();
            }
        });

    }

    public static void main(String[] args) {
        BtnFrame btnFrm = new BtnFrame();
        btnFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        btnFrm.setLocationRelativeTo(null);
        btnFrm.setVisible(true);
    }

    public class A1Panel extends JPanel {
        int[] xValue = null;
        int[] yValue = null;

        public void updateData(int[] xValue, int[] yValue) {
            this.xValue = xValue;
            this.yValue = yValue;
        }

        @Override
        public void paint(Graphics g) {
            // TODO Auto-generated method stub
            super.paint(g);
            if (xValue != null && yValue != null
                    && xValue.length == yValue.length && xValue.length > 1) {
                for (int i = 0; i < xValue.length - 1; i++) {
                    g.drawLine(xValue[i], yValue[i], xValue[i + 1],
                            yValue[i + 1]);
                }
            } else {
                g.fillOval(0, 0, this.getWidth(), this.getHeight());
            }
        }
    }

}
qq_21733941
qq_21733941   2015.08.27 18:04

改为g.paint试试

Csdn user default icon
上传中...
上传图片
插入图片