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;
}
} 展开
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;
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询