在左侧面板上我想我的JLabel左右移动的盒子内,并在右侧面板我希望我的JLabel与当前时间频繁更新,而盒子内走动,以随机位置上的随机位置以及。
我现在收到的问题是,当我设置JLabel的时间和位置时,我的JLabel在我的左边框中出现了错误。在移动到它应该移动的随机位置之前,它首先出现在初始位置(顶部)。
我有一个类实现了runnable,它处理左边的JLabel,另一个类也实现了可随时间更新处理JLabel的runnable。
任何人都可以帮我解决这个问题吗?我希望通过这种方式来设置这个程序,其中两个类实现可运行的JLabel移动的runnable。
这是左侧面板,关心班级:
public class MoveDisplay {
private GUIFrame gui;
private boolean moving = true;
public MoveDisplay(GUIFrame gui) {
this.gui = gui;
}
public void start() {
moving = true;
Random rand = new Random();
while (moving) {
int x = rand.nextInt(150) + 1;
int y = rand.nextInt(150) + 1;
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
gui.moveDisplay(x, y, 100, 100);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void stop() {
moving = false;
}
}
这是正确的面板,它是时钟。
public class MoveClock {
private GUIFrame gui;
private boolean clock = true;
private volatile boolean running = true;
public MoveClock(GUIFrame gui) {
this.gui = gui;
}
public void start() {
clock = true;
Random rand = new Random();
while (clock) {
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
gui.Klockan(hour, minute, second);
int a = rand.nextInt(100) + 1;
int b = rand.nextInt(100) + 1;
java.awt.EventQueue.invokeLater(new Runnable() { //Utan detta så kör inte Klockan random placeringar
public void run() {
gui.moveClock(a, b, 150, 150);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void stop() {
clock = false;
}
}
而这正是所有的帧,Jpanels等类:
private JLabel movingDisplay;
private JLabel movingClock;
private MP3Player mp3_player;
private boolean playing = true;
private boolean moving = true;
private boolean clocking = true;
// private Thread t1;
JDialog playingDialog;
private MoveDisplay moveDisplay;
private MoveClock moveClock;
Clip clip;
/**
* Starts the application
*/
public void Start() {
frame = new JFrame();
frame.setBounds(0, 0, 494, 437);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLayout(null);
frame.setTitle("Multiple Thread Demonstrator");
InitializeGUI(); // Fill in components
frame.setVisible(true);
frame.setResizable(false); // Prevent user from change size
frame.setLocationRelativeTo(null); // Start middle screen
moveDisplay = new MoveDisplay(this);
moveClock = new MoveClock(this);
}
JPanel pnlDisplay = new JPanel();
Border b2 = BorderFactory.createTitledBorder("Display Thread");
pnlDisplay.setBorder(b2);
pnlDisplay.setBounds(12, 118, 222, 269);
pnlDisplay.setLayout(null);
// Add buttons and drawing panel to this panel
btnDisplay = new JButton("Start Display");
btnDisplay.setBounds(10, 226, 121, 23);
pnlDisplay.add(btnDisplay);
btnDStop = new JButton("Stop");
btnDStop.setBounds(135, 226, 75, 23);
pnlDisplay.add(btnDStop);
pnlMove = new JPanel();
pnlMove.setBounds(10, 19, 200, 200);
Border b21 = BorderFactory.createLineBorder(Color.black);
pnlMove.setBorder(b21);
pnlDisplay.add(pnlMove);
// Then add this to main window
frame.add(pnlDisplay);
// The moving graphics outer panel
JPanel pnlTriangle = new JPanel();
Border b3 = BorderFactory.createTitledBorder("Triangle Thread");
pnlTriangle.setBorder(b3);
pnlTriangle.setBounds(240, 118, 222, 269);
pnlTriangle.setLayout(null);
// Add buttons and drawing panel to this panel
btnTriangle = new JButton("Start Rotate");
btnTriangle.setBounds(10, 226, 121, 23);
pnlTriangle.add(btnTriangle);
btnTStop = new JButton("Stop");
btnTStop.setBounds(135, 226, 75, 23);
pnlTriangle.add(btnTStop);
pnlRotate = new JPanel();
pnlRotate.setBounds(10, 19, 200, 200);
Border b31 = BorderFactory.createLineBorder(Color.black);
pnlRotate.setBorder(b31);
pnlTriangle.add(pnlRotate);
// Add this to main window
frame.add(pnlTriangle);
movingDisplay = new JLabel("DisplayThread");
pnlMove.add(movingDisplay);
btnDStop.setEnabled(false);
movingClock = new JLabel("TriangleThread");
pnlRotate.add(movingClock);
btnTStop.setEnabled(false);
btnDisplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
moving = true;
btnDisplay.setEnabled(false);
btnDStop.setEnabled(true);
startMoveDisplay();
}
});
btnDStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
moving = false;
btnDisplay.setEnabled(true);
btnDStop.setEnabled(false);
startMoveDisplay();
}
});
btnTriangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clocking = true;
btnTriangle.setEnabled(false);
btnTStop.setEnabled(true);
startMoveClock();
}
});
btnTStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clocking = false;
btnTriangle.setEnabled(true);
btnTStop.setEnabled(false);
startMoveClock();
}
});
}
public void startMoveDisplay() {
if(moving) {
new Thread(new Runnable() {
public void run() {
moveDisplay.start();
}
}).start();
} else {
moveDisplay.stop();
}
}
public void startMoveClock() {
if(clocking) {
new Thread(new Runnable() {
public void run() {
moveClock.start();
}
}).start();
} else {
moveClock.stop();
}
}
public void moveDisplay(int x, int y, int width, int height) {
movingDisplay.setBounds(x, y, width, height);
}
public void moveClock(int a, int b, int width, int height) {
movingClock.setBounds(a, b, width, height);
}
public void Klockan(int hour, int minute, int second) {
movingClock.setText(hour + ":" + minute + ":" + second);
}
我看到你的代码在你的新问题工作正常:http://stackoverflow.com/questions/33710527/cant-set-image-in-jlabel-from-anywhere-except-jframe/33710599# 33710599。不要忘了单击复选标记以“接受”答案,以便人们知道问题已解决。或者,如果您使用了不同的解决方案,请再次发布自己的答案,以便人们知道问题已得到解决。请考虑使用论坛的人员,以便我们不花时间回答已有解决方案的问题。 – camickr
@camickr我没有注意到我在这里回答。有了这个,我得到了我的问题排序,并得到了一切工作:)谢谢! – WeInThis
你为什么要删除你的其他问题?答案没有帮助解决问题吗? – camickr