Gougo2013 2017-01-23 12:55 采纳率: 0%
浏览 1011
已结题

关于repaint(x,y,h,w)重绘两个独立区域的问题!

怎么才能实现使用repaint在同一个窗口内同时绘制两个分离的区域?并且不影响在这两个区域外的文本框和按钮?下面有一个按钮和文本框不能正常显示的代码?希望给帮忙:

package Fichnan.com;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Play extends JPanel{
private static int barrageX = 500;
public void paint(Graphics g){
System.out.println(111);
g.clearRect(0, 0, 500, 30);
g.clearRect(0, 380, 500, 30);
g.setColor(new Color(0x000000));
g.setFont(new Font(Font.DIALOG,Font.PLAIN ,20));
g.drawString("asd", barrageX, 400);
g.drawString("这是一句会动的话!", barrageX, 20);
}

public void action(JPanel panel){
    Timer timer = new Timer();
    int intervel = 10;
    timer.schedule(new TimerTask(){
        public void run() {
            repaint(0, 0, 500, 30);
            repaint(5, 0, 380, 500, 30);
            System.out.println(222);
            barrageX--;
            if(barrageX<0){
                barrageX = 500;
            }
        }
    }, intervel,intervel);
}


public static void main(String[] args) {

    Play play = new Play();

    JFrame frame = new JFrame();
    frame.add(play);
    frame.setSize(500,500);
    frame.setVisible(true);
    frame.setAlwaysOnTop(true);
    frame.setResizable(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(0,0,500,500);
    panel.setLayout(null);
    frame.add(panel);


    JPanel panel1 = new JPanel();
    panel1.setBounds(100,100,100,100);
    panel1.setBackground(Color.black);
    panel1.setLayout(new GridLayout(1,1));
    panel.add(panel1);

    JTextArea text = new JTextArea();
    text.setBounds(100,200,100,100);
    text.setBackground(Color.yellow);
    text.setVisible(true);
    panel.add(text);

    JButton button = new JButton("click");
    button.setBackground(Color.yellow);
    button.setBounds(200,100,100,100);
    panel.add(button);

    play.action(panel);

}

}

  • 写回答

1条回答 默认 最新

  • devmiao 2017-01-23 15:54
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?