qq_35156372 2016-11-02 04:36 采纳率: 28.6%
浏览 2009

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条回答 默认 最新

  • zqbnqsdsmd 2016-11-04 04:10
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作