[英]Update the JLabel's label text during the event - Swing
Basically I want change the JLabel's Label text during on-click the button 'Generate PDF Record Book' 基本上,我想在单击“ Generate PDF Record Book”按钮时更改JLabel的Label文本
From the previous example says: 从前面的例子说:
label.setText("new value");
when I do that, the label value doesn't change at all, please give me some directions, thanks 当我这样做时,标签值完全不变,请给我一些指示,谢谢
initialize(); 初始化();
JLabel lblNewLabel = new JLabel("513 k bytes");
lblNewLabel.setBounds(407, 713, 151, 14);
frmViperManufacturingRecord.getContentPane().add(lblNewLabel);
On button Generate PDF Record Book click 在“ 生成PDF记录簿”按钮上,单击
JButton btnGeneratePdfHeader = new JButton("Generate PDF Record Book");
btnGeneratePdfHeader.setMnemonic('G');
btnGeneratePdfHeader.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final JLabel lblNewLabel = new JLabel("513 k bytes");
//java.io.File file = new java.io.File(strdfile);
//lblNewLabel.setSize(file.length());
//System.out.println(file.length());
String fileSize = file.length() + " k bytes";
System.out.println("I am here");
lblNewLabel.setText("new value");
}
});
You are creating a new JLabel
when pressing the button and then set the text of that label to "new value" 按下按钮时您正在创建一个新的JLabel
,然后将该标签的文本设置为“新值”
final JLabel lblNewLabel = new JLabel("513 k bytes");
lblNewLabel.setText("new value");
rather than changing the text of the label on your UI. 而不是在用户界面上更改标签的文本。 You will need to call setText("new value")
on a reference to the label you've already added to the UI instead. 您需要在对已经添加到UI的标签的引用上调用setText("new value")
。 For instance, that label would neeed to be a field in your UI class, eg final JLabel fileSizeLabel
and you would set that labels text by calling 例如,该标签将成为您的UI类中的一个字段,例如final JLabel fileSizeLabel
,您可以通过调用以下命令来设置标签文本
fileSizeLabel.setText("new value");
inside the buttons action listener. 在按钮动作监听器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.