如何从 JPanel swing 更改 JFrame 标签
您好,我需要从 JPanel 更改 JFrame 上的 JLabel。我只能在鼠标移动时更改此设置,但每次得分++时我都需要更改;
private void panelKwadraty1MouseMoved(java.awt.event.MouseEvent evt)
{
jLabel1.setText("Twoj wynik to: "+panelKwadraty1.getScore());
}
我需要更改的时间:
if (kwadrat[i].sprawdzKolizje(belka) == 1)
{
kwadrat[i]=new Kwadrat(kwadrat[i].getKolor());
score++;
// Jframe.jLabel1.setText("Your score is :" + score); <--- i need do that
}
编辑:
我解决了问题。我使用了 propertyChangeListener。
在 JFrame 中:
panelKwadraty1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
panelKwadraty1PropertyChange(evt);
}
});
private void panelKwadraty1PropertyChange(java.beans.PropertyChangeEvent evt)
{
if(evt.getPropertyName().equals(PanelKwadraty.ZMIANA_WYNIKU)){
setLabelText(""+evt.getNewValue());
}
}
在 JPanel 中:
int old_score = score;
score++;
this.firePropertyChange(ZMIANA_WYNIKU, old_score, score);
Hi i need to change JLabel which is on JFrame, from JPanel. I can change this only when the mouse is moving, but i need to change everytime when score++;
private void panelKwadraty1MouseMoved(java.awt.event.MouseEvent evt)
{
jLabel1.setText("Twoj wynik to: "+panelKwadraty1.getScore());
}
I need to change when:
if (kwadrat[i].sprawdzKolizje(belka) == 1)
{
kwadrat[i]=new Kwadrat(kwadrat[i].getKolor());
score++;
// Jframe.jLabel1.setText("Your score is :" + score); <--- i need do that
}
edit:
I solved problem. I used propertyChangeListener.
In JFrame:
panelKwadraty1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
panelKwadraty1PropertyChange(evt);
}
});
private void panelKwadraty1PropertyChange(java.beans.PropertyChangeEvent evt)
{
if(evt.getPropertyName().equals(PanelKwadraty.ZMIANA_WYNIKU)){
setLabelText(""+evt.getNewValue());
}
}
In JPanel:
int old_score = score;
score++;
this.firePropertyChange(ZMIANA_WYNIKU, old_score, score);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您提供的代码,我假设 jLabel1 是公共的,因为您在面板的方法中调用它。
因此,我认为您的问题可能是,当您移动鼠标时会发生很多变化,并且标签没有足够快地重新绘制。为了确保它的重绘尽快发生,请使用下面的代码:
希望就是这样。
From the code you have provided I assumed that the jLabel1 is public since you are calling it within a method of the panel.
Thus I think that your problem here might be that there are to many changes when you move the mouse and the label is not being repainted quickly enough. To make sure its repaint happens ASAP use code below:
Hope that was it.
在 JFrame 中将
jLabel1
设为公共,以便 JPanel 可以访问它。如果 JFrame 和 JPanel 属于同一个类,那么只需将 jLabel1 设为全局,jPanel 就可以访问它。Make
jLabel1
public in JFrame so that JPanel can access it. And if both JFrame and JPanel belongs to same class then just makejLabel1
global and it will be accessible by jPanel.控件是否进入if()??如果它们位于同一窗口中,则不必将其引用为 JFrame.JLabel .. 您只需将 JLabel 公开并调用该方法即可
jLabel1.setText()
简单..您能告诉我们更多有关组件放置位置的信息吗?is the control enters the if() ?? you dont have to reference it as JFrame.JLabel if they are in the same window .. You just have to make the JLabel public and call the method
jLabel1.setText()
simple.. can you tell us more about where the components are placed ?