2011-12-13 119 views
6

我有一個JLabel和一個按鈕,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(); 
    } 
} 
+0

編輯使代碼的可讀性。 – 2011-12-13 22:23:21

+0

謝謝,我以前沒有注意到會員...... :) – DejanLekic 2011-12-13 22:24:17

回答

9

問題是您正在創建一個新的不同的JLabel,它不會顯示在面板中。

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

+1你比我快,並有更好的解決方案。 – Jonas 2011-12-13 22:22:08

2

你只叫calculate()點擊按鈕start時。所以你可以將該方法移動到按鈕的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."); 
    } 
}); 
2

變化label1 = new JLabel("You have pressed button " + pressed + "times.");label1.setText("You have pressed button " + pressed + "times.");

1
/* try and understand this code, here i use an icon to set the labe's image and the getIcon method of Label's to change the icon of previous label using setIcon method. */     
Icon picLabelicon new ImageIcon(img); /* setting the icon initially*/ 
        JLabel picLabel = new JLabel(); 
        picLabel.setIcon(picLabelicon); 
        /* now you have set the icon initially now lets change it dynamically*/ 

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

        picLabel.setIcon(picLabelicon); 
      revalidate(); 
      repaint();