java的JPanel边框和背景均不显示
 public class Car{
    private static int speed;
    public static void main(String args[]){
        //Timer timer=new Timer();
        JFrame win=new JFrame();
        JPanel panel=new JPanel();
        JButton btn1=new JButton("速度1");
        JButton btn2=new JButton("速度2");
        Mycanvs canvs=new Mycanvs();
        win.setLayout(null);
        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                speed=10;
            }
        });
        btn2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                speed=30;
                //timer.schedule(, time);
            }
        });
        //panel.setBounds(100,100, 200, 200);
        canvs.setBounds(100,100, 200, 200);
        canvs.setBorder(BorderFactory.createLineBorder(Color.blue,1));
        canvs.setBackground(Color.BLUE);
        //panel.setBorder(BorderFactory.createLineBorder(Color.blue,1));
        //panel.add(canvs);
        btn1.setBounds(40, 10, 100, 30);
        btn2.setBounds(200, 10, 100, 30);
        win.add(canvs);
        win.add(btn1);
        win.add(btn2);
        win.setBounds(100, 100, 400, 400);
        win.setVisible(true);


    }
    public static class Mycanvs extends JPanel{
        int x=100;
        int y=100;
        public void paint(Graphics g){
            g.fillRect(x, y, 20, 20);
        }
        public void repaint(){
            if(x==100){
                x=0;
                y=0;
            }
            x+=speed;
            y+=speed;
        }
    }

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐