描述问题:主要是想通过单击"开始"按钮后,Label(程序中命名为lbl1),根据设定的thread.sleep(50);进行坐标(lblX,lblY)的增加,然后进行移动,目前,Label只能一次移动到最终的位置,不能展示移动的过程,希望能解答,感谢了!!!
*程序中需要c:\z.jpg,如果需要可自己在C盘放个z.jpg命名的图片。import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import javax.swing.UIManager;class KillJapaneseDemo extends JFrame { private static final long serialVersionUID = 1L;
public int lblX = 10;
public int lblY = 10;
public int lblWidth = 10;
public int lblHigh = 10; private int frameX = 500;
private int frameY = 500; JButton button1;
JButton button2;
JLabel lbl1;
JLabel lblRank; JInternalFrame interframe1, interframe2;
JPanel jpanel;
JapaneseMove jMove; private String lblView = "1"; KillJapaneseDemo(int x, int y, int width, int high) {
lblX = x;
lblY = y;
lblWidth = width;
lblHigh = high; // setVisible(true);
jpanel = (JPanel) this.getContentPane();
jpanel.setLayout(new GridLayout(1, 1));
button1 = new JButton("开始");
button2 = new JButton("级别"); lblRank = new JLabel(lblView); button1.setBounds(160, 10, 80, 22);
button2.setBounds(250, 10, 80, 22);
lblRank.setBounds(350, 10, 80, 22); lbl1 = new JLabel();
lbl1.setIcon(new ImageIcon("C:\\Z.jpg")); lbl1.setBounds(lblX, lblY, lblWidth, lblHigh);
JDesktopPane desk1 = new JDesktopPane();
desk1.setBackground(Color.WHITE);
desk1.add(button1);
desk1.add(button2);
desk1.add(lbl1);
desk1.add(lblRank); jpanel.add(desk1); addAction();
} /*
* Click Rank Button
*/
public Dimension getPreferredSize() {
return new Dimension(frameX, frameY);
} public void addButton(Container c, String title, ActionListener listener) {
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
} public void addAction() {
jMove = new JapaneseMove(lbl1);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count();
}
}); button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// jMove.run();
lblMove();
}
}); } /*
* Rank Count
*/
public void count() { int tep = Integer.parseInt(lblRank.getText());
if (tep == 3) {
tep = 0;
}
lblView = String.valueOf(tep + 1); lblRank.setText(lblView);
} public void lblMove() {
try {
for (int i = 1; i < 20; i++, Thread.sleep(50), lbl1.repaint()) { lblX += 3;
lblY += 3;
System.out.println(lblX + "===" + lblY);
lbl1.setLocation(lblX, lblY);
lbl1.repaint();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}public class interframe {
public static void main(String args[]) {
int x = 50, y = 50, width = 50, hith = 50;
KillJapaneseDemo kjd = new KillJapaneseDemo(x, y, width, hith); kjd.pack();
kjd.setResizable(false);
kjd.setVisible(true); kjd.addWindowListener(new WindowAdapter() {
public void windowsClosing(WindowEvent e) {
System.exit(0);
}
});
}}
*程序中需要c:\z.jpg,如果需要可自己在C盘放个z.jpg命名的图片。import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import javax.swing.UIManager;class KillJapaneseDemo extends JFrame { private static final long serialVersionUID = 1L;
public int lblX = 10;
public int lblY = 10;
public int lblWidth = 10;
public int lblHigh = 10; private int frameX = 500;
private int frameY = 500; JButton button1;
JButton button2;
JLabel lbl1;
JLabel lblRank; JInternalFrame interframe1, interframe2;
JPanel jpanel;
JapaneseMove jMove; private String lblView = "1"; KillJapaneseDemo(int x, int y, int width, int high) {
lblX = x;
lblY = y;
lblWidth = width;
lblHigh = high; // setVisible(true);
jpanel = (JPanel) this.getContentPane();
jpanel.setLayout(new GridLayout(1, 1));
button1 = new JButton("开始");
button2 = new JButton("级别"); lblRank = new JLabel(lblView); button1.setBounds(160, 10, 80, 22);
button2.setBounds(250, 10, 80, 22);
lblRank.setBounds(350, 10, 80, 22); lbl1 = new JLabel();
lbl1.setIcon(new ImageIcon("C:\\Z.jpg")); lbl1.setBounds(lblX, lblY, lblWidth, lblHigh);
JDesktopPane desk1 = new JDesktopPane();
desk1.setBackground(Color.WHITE);
desk1.add(button1);
desk1.add(button2);
desk1.add(lbl1);
desk1.add(lblRank); jpanel.add(desk1); addAction();
} /*
* Click Rank Button
*/
public Dimension getPreferredSize() {
return new Dimension(frameX, frameY);
} public void addButton(Container c, String title, ActionListener listener) {
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
} public void addAction() {
jMove = new JapaneseMove(lbl1);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count();
}
}); button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// jMove.run();
lblMove();
}
}); } /*
* Rank Count
*/
public void count() { int tep = Integer.parseInt(lblRank.getText());
if (tep == 3) {
tep = 0;
}
lblView = String.valueOf(tep + 1); lblRank.setText(lblView);
} public void lblMove() {
try {
for (int i = 1; i < 20; i++, Thread.sleep(50), lbl1.repaint()) { lblX += 3;
lblY += 3;
System.out.println(lblX + "===" + lblY);
lbl1.setLocation(lblX, lblY);
lbl1.repaint();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}public class interframe {
public static void main(String args[]) {
int x = 50, y = 50, width = 50, hith = 50;
KillJapaneseDemo kjd = new KillJapaneseDemo(x, y, width, hith); kjd.pack();
kjd.setResizable(false);
kjd.setVisible(true); kjd.addWindowListener(new WindowAdapter() {
public void windowsClosing(WindowEvent e) {
System.exit(0);
}
});
}}
其中【JapaneseMove】不知道是什么,我给注释掉了。
主要修改了lblMove方法。
class KillJapaneseDemo extends JFrame { private static final long serialVersionUID = 1L;
public int lblX = 10;
public int lblY = 10;
public int lblWidth = 10;
public int lblHigh = 10; private int frameX = 500;
private int frameY = 500; JButton button1;
JButton button2;
JLabel lbl1;
JLabel lblRank; JInternalFrame interframe1, interframe2;
JPanel jpanel;
// JapaneseMove jMove; private String lblView = "1"; KillJapaneseDemo(int x, int y, int width, int high) {
lblX = x;
lblY = y;
lblWidth = width;
lblHigh = high; // setVisible(true);
jpanel = (JPanel) this.getContentPane();
jpanel.setLayout(new GridLayout(1, 1));
button1 = new JButton("开始");
button2 = new JButton("级别"); lblRank = new JLabel(lblView); button1.setBounds(160, 10, 80, 22);
button2.setBounds(250, 10, 80, 22);
lblRank.setBounds(350, 10, 80, 22); lbl1 = new JLabel();
lbl1.setIcon(new ImageIcon("C:\\Z.jpg")); lbl1.setBounds(lblX, lblY, lblWidth, lblHigh);
JDesktopPane desk1 = new JDesktopPane();
desk1.setBackground(Color.WHITE);
desk1.add(button1);
desk1.add(button2);
desk1.add(lbl1);
desk1.add(lblRank); jpanel.add(desk1); addAction();
} /*
* Click Rank Button
*/
public Dimension getPreferredSize() {
return new Dimension(frameX, frameY);
} public void addButton(Container c, String title, ActionListener listener) {
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
} public void addAction() {
// jMove = new JapaneseMove(lbl1);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count();
}
}); button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// jMove.run();
lblMove lm = new lblMove();
new Thread(lm).start();
}
}); } /*
* Rank Count
*/
public void count() { int tep = Integer.parseInt(lblRank.getText());
if (tep == 3) {
tep = 0;
}
lblView = String.valueOf(tep + 1); lblRank.setText(lblView);
}// public void lblMove() {
// try {
// for (int i = 1; i < 20; i++, Thread.sleep(50), lbl1.repaint()) {
//
// lblX += 3;
// lblY += 3;
// System.out.println(lblX + "===" + lblY);
// lbl1.setLocation(lblX, lblY);
// lbl1.repaint();
// }
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// } class lblMove implements Runnable { public void run() { try {
for (int i = 1; i < 20; i++, Thread.sleep(50), lbl1.repaint()) { lblX += 3;
lblY += 3;
System.out.println(lblX + "===" + lblY);
lbl1.setLocation(lblX, lblY);
lbl1.repaint();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}public class interframe {
public static void main(String args[]) {
int x = 50, y = 50, width = 50, hith = 50;
KillJapaneseDemo kjd = new KillJapaneseDemo(x, y, width, hith); kjd.pack();
kjd.setResizable(false);
kjd.setVisible(true); kjd.addWindowListener(new WindowAdapter() {
public void windowsClosing(WindowEvent e) {
System.exit(0);
}
});
}}