我的更新程序有一些JLabel,除文本外,一切都运行顺畅.
文本都是乱码,看起来就像是一次显示.我已经尝试将每个文本设置为自己的标签,并设置当方法被调用为不透明时不相关的文本.但我得到了nullpointerexceptions.我也试过分层我的JFrame但是它摆脱了我的JProgrssbar?
这是我的代码:
public static void displayText(int Stage) {
String txt = "";
if (Stage == 1) {
txt = "Checking Cache...";
}
if (Stage == 2) {
txt = "Downloading Cache...";
}
if (Stage == 3) {
txt = "Cache Download Complete!";
}
if (Stage == 4) {
txt = "Unpacking Files...";
}
if (Stage == 5) {
txt = "Launching Client!";
}
lbl = new JLabel();
lbl.setText(txt);
lbl.setBounds(137, 11, 200, 14);
frame.getContentPane().add(lbl);
}
Run Code Online (Sandbox Code Playgroud)
我试过用几种不同的方式重新格式化它仍然做同样的事情......
它正在做的一个例子:

您每次都要创建一个新标签并将其放在旧标签上.在类的范围内的某处声明标签(更具描述性的名称也会很好).然后,在您的方法中,只调用lbl.setText(txt).这将使用更新的文本更新预先存在的标签.
它应该看起来像这样:
public class yourGUI {
private JLabel progressLabel;
public static void main(String[] args) {
progressLabel = new JLabel();
progressLabel.setBounds(137, 11, 200, 14);
frame.getContentPane().add(progressLabel);
}
public static void displayText(int Stage) {
String txt = "";
if (Stage == 1) {
txt = "Checking Cache...";
} else if (Stage == 2) {
txt = "Downloading Cache...";
} else if (Stage == 3) {
txt = "Cache Download Complete!";
} else if (Stage == 4) {
txt = "Unpacking Files...";
} else { //assuming (Stage == 5), this is up to your discretion
txt = "Launching Client!";
}
progressLabel.setText(txt);
}
}
Run Code Online (Sandbox Code Playgroud)
此外,无需每次都检查每个if语句.