2011-12-13 40 views
6

Tôi có một JLabel và một nút, số JLabel hiển thị số lần nút được nhấn, tuy nhiên, tôi không thể biết cách cập nhật số JLabel hiển thị số lần nhấn nút.Cách thay đổi JLabel động

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

chỉnh sửa để làm cho mã dễ đọc. –

+0

Cảm ơn, tôi đã không thông báo cho các thành viên trước đây ... :) – DejanLekic

Trả lời

9

Vấn đề là bạn đang tạo một JLabel mới, khác không được hiển thị trong bảng điều khiển.

làm

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

+1 Bạn đã nhanh hơn tôi và có một giải pháp tốt hơn. – Jonas

+0

+1, cả hai bạn đều nhanh hơn tôi! –

2

Bạn chỉ gọi calculate() khi nút start được nhấp. Vì vậy, bạn có thể di chuyển phương thức đó vào ActionListener cho nút. Và bằng cách gọi setText trên JLabel, bạn không phải gọi repaint. Thông thường, bạn không phải gọi số repaint trong Swing. Ví dụ. thay đổi mã của bạn để một cái gì đó giống như thay vì điều này:

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."); 
    } 
}); 
+0

Vâng, những gì anh ta nói. :) 1+ –

2

Thay đổi 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(); 
Các vấn đề liên quan