public class XJLoading extends JDialog {
private JLabel iconLabel;
private JLabel textLabel;

public XJLoading(JFrame frame) {
// TODO Auto-generated constructor stub
super(frame,null,true);

int width = 150;
int height = 150;
setLayout(null);
setUndecorated(true);

setSize(width, height);
setPreferredSize(new Dimension(width, height));
setMinimumSize(new Dimension(width, height));
setModal(true);
setResizable(false);
setLocation((frame.getWidth() - width) / 2, (frame.getHeight() - height) / 2);

Container container = getContentPane();
container.setLayout(null);
container.setSize(width, height);
container.setPreferredSize(new Dimension(width, height));
container.setMinimumSize(new Dimension(width, height));

ClassLoader cldr = this.getClass().getClassLoader(); 
java.net.URL imageURL = cldr.getResource("img/loading.gif"); 
ImageIcon imageIcon = new ImageIcon(imageURL);

iconLabel = new JLabel(); 
iconLabel.setBounds((width-70)/2, (height-70)/2, 70, 70);
iconLabel.setIcon(imageIcon); 
imageIcon.setImageObserver(iconLabel); 

container.add(iconLabel); 

textLabel = new JLabel(); 
textLabel.setBounds(10, iconLabel.getY()+iconLabel.getHeight()-10, width-20, 45);
textLabel.setHorizontalAlignment(JLabel.CENTER);
textLabel.setFont(new Font(null, Font.PLAIN, 15));
textLabel.setForeground(Color.red);
textLabel.setText("加载中...");//只显示“加载中” 不显示后面的...
textLabel.setText("加载中...a");//全显示
textLabel.setText("loading...");//全显示
container.add(textLabel); 
System.out.println(textLabel.getText());

container.setVisible(true);
}

public void setText(String text) {
textLabel.setText(text);
}
}如上代码和注释处,求大神解答!谢谢!

解决方案 »

  1.   

    文字显示不全,说明你的label不够宽呗
      

  2.   

    如果是label不够宽的话,就不会有以下效果了:textLabel.setText("加载中...");//只显示“加载中” 不显示后面的...
    textLabel.setText("加载中...a");//全显示
      

  3.   

    我觉得这是个 bug
    我的解决方案是重载 JLabel 的 paint 方法:
    class MyLabel extends JLabel { @Override
    public void paint(Graphics g) {
    //这一行绘制的仍然不带...,所以干脆绘制到不可见区域
    g.drawString(this.getText(), 0, -50);
    //第二次绘制带...,很奇怪,我也想知道为什么
    super.paint(g);
    }}
      

  4.   

    上全一点代码,想测试发现没有main