자바 swing 에서 JLabel 연속 으로 이동 시키기

jang900824의 이미지

//버튼이 눌렸을 때 실행되는 코드
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
 
        try {
            for(int i=0;i<3;i++)
            {
                Point point=jLabel1.getLocation();
                jLabel1.setLocation(point.x+10, point.y+10);
                t.run();
            }
        } catch (Exception ex) {
            Logger.getLogger(GraphicsContainer.class.getName()).log(Level.SEVERE, null, ex);
        }
}
 
//Thread의 객체인 t
private Thread t=new Thread()
        {
            public void run()
            {
                try
                {
                  Thread.sleep(100L);
                }
                catch (InterruptedException ex)
                {
 
                }
            }
        };

JLabel1은 그림이 그려져 있습니다.
그리고 버튼을 누르면 화면에 있던 JLabel이 0.1초 간격으로 x축으로 10, y축으로 10씩 3번 이동하는것이 목표입니다.
그런데 실제로 프로그램을 돌리면 3번 움직이는게 아니라 3번 움직일 거리를 한번에 기다리고 움직이네요.
아무래도 실제 JLabel1은 움직이지만 화면에 새로 그리질 못해서 그런 것 같습니다.
revalidate();를 사용하면 아예 움직이지도 않습니다.

어떻게 하는것이 좋을까요?

jang900824의 이미지

참고로 t.start는 이미 한번 실행이 된 상태입니다.

chisquare88의 이미지

스윙에선 슬립이 안먹힌다고 합니다.

public class test implements ActionListener{
 
	JFrame frame;
	JPanel panel;
	JLabel jlabel1;
	int speed=10;
	int count=0;
	Timer t= new Timer(speed,this); 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test t= new test();
		t.go();
	}
 
	public void go()
	{
		frame = new JFrame();
		JButton labelButton = new JButton("Test");
		panel = new JPanel();
		labelButton.addActionListener(this);
		jlabel1=new JLabel("왜");
		jlabel1.setLocation(10, 10);
		frame.getContentPane().add(panel);
		panel.add(labelButton);
		panel.add(jlabel1);
		frame.setSize(400, 300);
		panel.repaint();
		frame.setVisible(true);
	}
	public void actionPerformed(ActionEvent event)
	{
		t.start();
		anime();
	}
	public void anime()
	{
		Point point = jlabel1.getLocation();
		jlabel1.setLocation((point.x+1),(point.y+1));
 
		panel.repaint();
		count++;
		if(count>=30)t.stop();
	}
 
}

χxχ=χ^2
GIS SW 개발자

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.