如何从 JPanel swing 更改 JFrame 标签

发布于 2024-11-04 18:07:12 字数 1437 浏览 4 评论 0原文

您好,我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑脸一如从前 2024-11-11 18:07:12

根据您提供的代码,我假设 jLabel1 是公共的,因为您在面板的方法中调用它。

因此,我认为您的问题可能是,当您移动鼠标时会发生很多变化,并且标签没有足够快地重新绘制。为了确保它的重绘尽快发生,请使用下面的代码:

    private void setLabelText(String text)
    {
        jLabel1.setText(text);
        jLabel1.paintImmediately(jLabel1.getVisibleRect());
    }

希望就是这样。

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:

    private void setLabelText(String text)
    {
        jLabel1.setText(text);
        jLabel1.paintImmediately(jLabel1.getVisibleRect());
    }

Hope that was it.

蓝眼泪 2024-11-11 18:07:12

在 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 make jLabel1 global and it will be accessible by jPanel.

殊姿 2024-11-11 18:07:12

控件是否进入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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文