I have 2 JFrames, lets name them frm1 and frm2
In frm1, there''s a JButton, in frm2, there''s a JLabel
Now my question is how can I change the JLabel when I perform an Action in JButton
I try using this code://in frm1
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new frm1().SetLabelText();
}
//in frm2
public void SetLabelText() {
jLabel1.setText("Text Here");
}
解决方案
Declaring jLabel1 as public it is possible to assign directly text to it using a reference to the instance of frm2. Make sure that frm1 contains a field with this reference.
Or if you want to realize it with a public method declared in frm2 also make sure that frm1 contains a field with this reference.
Write referenceTofrm2.SetLabelText(); instead of new frm1().SetLabelText();