jlabel 不能连续两次set_Java中JLabel为什么setText后不刷新?

在Java Swing应用中遇到JLabel设置文本后不更新的问题,代码示例显示了一个4x4的方格矩阵,当尝试用CellPanel的setValue方法改变JLabel的内容时,发现JLabel未能刷新。问题可能出在Swing组件的刷新机制上。
摘要由CSDN通过智能技术生成

先上代码:packagecom.firstbpm.frame;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Toolkit;importjava.util.Random;importjavax.swing.BorderFactory;importjava...

先上代码:

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

top_margin += 5;

int left_margin = 0;

for(int j = 0;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;

}

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值