Java中JLabel为什么setText后不刷新?

先上代码:packagecom.firstbpm.frame;importjava.awt.Color;importjava.awt.Dimension;importja... 先上代码:
package com.firstbpm.frame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import com.firstbpm.panel.CellPanel;

public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static final Integer SIZE = 4;
private static final Integer WIDTH = SIZE*100-10;
private static final Integer HEIGHT = SIZE*100 +10;

private CellPanel[][] panels ;
public MainFrame(){

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((int)(d.getWidth() - WIDTH)/2, (int)(d.getHeight() - HEIGHT)/2);
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setResizable(false);
this.setVisible(true);
init();
start();
}
private void init(){
//初始化一个4*4的方格矩阵
panels = new CellPanel[SIZE][SIZE];
int top_margin = 0;
for(int i = 0;i<panels.length;i++){
top_margin += 5;
int left_margin = 0;
for(int j = 0;j<panels[i].length;j++){
panels[i][j] = new CellPanel();
CellPanel cell = panels[i][j];
cell = new CellPanel();
cell.setBounds(left_margin += 5,top_margin , CellPanel.WIDTH, CellPanel.HEIGHT);
cell.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.add(cell);
left_margin += CellPanel.WIDTH;
}
top_margin += CellPanel.HEIGHT;
}
}
private void start(){
Random r = new Random();
for(int i = 0;i<2;){
int row = r.nextInt(SIZE);
int col = r.nextInt(SIZE);
if(panels[row][col].getValue() != null){
continue;
}
panels[row][col].setValue(2l);//这里setValue以后对应的JLabel不刷新?
i++;
}
}
public static void main(String[] args) {
new MainFrame();
}
}

package com.firstbpm.panel;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class CellPanel extends JPanel{
private static final long serialVersionUID = 1L;
public static final Integer WIDTH = 90;
public static final Integer HEIGHT = 90;
private Long value;
private JLabel label = new JLabel();
public CellPanel(){
this.setLayout(null);
label.setBounds(10, 10, 10, 10);
this.add(label);
}
public Long getValue() {
return value;
}
public void setValue(Long value) {
this.value = value;
this.label.setText(value.toString());

}
public JLabel getLabel() {
return label;
}
public void setLabel(JLabel label) {
this.label = label;
}
}
展开
 我来答
yugi111
2014-04-14 · TA获得超过8.1万个赞
知道大有可为答主
回答量:5.1万
采纳率:70%
帮助的人:1.3亿
展开全部
panels[row][col].setValue (2l);// 这里setValue以后对应的JLabel不刷新?
SwingUtilities.updateComponentTreeUI (this);
i++;
追问
没用啊,还是没有
追答
我都有了,你还没有,、
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式