【问题标题】:How to change a JLabel dynamically如何动态更改 JLabel
【发布时间】:2011-12-13 22:15:23
【问题描述】:

我有一个JLabel 和一个按钮,JLabel 显示按钮被按下的次数,但是,我不知道如何更新 JLabel 显示按钮按下的次数。

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class SimpleGui {
   private JFrame f = new JFrame("Basic GUI"); // create Frame
   int pressed = 0; // tracks number of button presses.
   JLabel label1 = new JLabel("You have pressed button " + pressed + "times.");
   private JButton start = new JButton("Click To Start!");

   public SimpleGui() {
      // Setup Main Frame
      f.getContentPane().setLayout(new GridLayout(0, 1));
      start.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            calculate();
         }
      });
      // Add components
      f.add(label1);
      f.add(start);
      // Allows the Swing App to be closed
      f.addWindowListener(new ListenCloseWdw());
   }

   public class ListenMenuQuit implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         System.exit(0);
      }
   }

   public class ListenCloseWdw extends WindowAdapter {
      public void windowClosing(WindowEvent e) {
         System.exit(0);
      }
   }

   public void launchFrame() {
      // Display Frame
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.pack(); // Adjusts panel to components for display
      f.setVisible(true);
   }

   public static void main(String args[]) {
      PrimeTime gui = new PrimeTime();
      gui.launchFrame();
   }

   public void calculate() {
      pressed++;
      label1 = new JLabel("You have pressed button " + pressed + "times.");
      // update the GUI with new jLabel
      f.repaint();
   }
}

【问题讨论】:

  • 已编辑以使代码可读。
  • 谢谢,我之前没有注意到成员... :)

标签: java swing jlabel


【解决方案1】:

问题是您正在创建一个新的、不同的 JLabel,该 JLabel 未显示在面板中。

public void calculate(){
   pressed++;
   this.label1.setText("You have pressed button " + pressed + "times.");
} 

【讨论】:

    【解决方案2】:

    label1 = new JLabel("You have pressed button " + pressed + "times."); 更改为label1.setText("You have pressed button " + pressed + "times.");

    【讨论】:

      【解决方案3】:

      您只有在单击按钮 start 时才调用 calculate()。因此,您可以将该方法移动到按钮的 ActionListener 中。通过在 JLabel 上调用 setText,您不必调用 repaint。通常你不必在 Swing 中调用repaint。例如。将您的代码改为这样的:

      final JLabel label1 = new JLabel("You have pressed button " + pressed + "times.");
      private JButton start = new JButton(new AbstractAction("Click To Start!") {
          public void actionPerformed(ActionEvent e) {
              pressed++;
              label1.setText("You have pressed button " + pressed + "times.");
          }
      });
      

      【讨论】:

        【解决方案4】:

        试着理解这段代码,这里我使用一个图标来设置标签的图像,而标签的getIcon方法使用setIcon方法来改变前一个标签的图标。

        Icon picLabelicon new ImageIcon(img); /* setting the icon initially */
        JLabel picLabel = new JLabel();
        picLabel.setIcon(picLabelicon);
        

        现在您已经设置了图标,现在让我们动态更改它

        JLabel modify = new JLabel(new ImageIcon(newimg)); /* new label you wanted to use */
        picLabelicon=modify.getIcon();
        picLabel.setIcon(picLabelicon);
        revalidate();
        repaint();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-09-05
          • 1970-01-01
          • 2022-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-25
          • 2023-03-28
          相关资源
          最近更新 更多