I'm writing some experimental GUI code. I'm trying to set it up so calling main
generates two windows, each with a button and a label. The label displays how many times the button has been clicked. However, I want it so if you click the button in one window, the label in the other updates. How would I do this?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class TestGUI extends JFrame {
private static int count;
private JButton button = new JButton("odp");
private JLabel label = new JLabel();
public TestGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(button);
setLayout(new FlowLayout(FlowLayout.RIGHT));
labelUpdateText();
add(label);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count++;
labelUpdateText();
}
});
pack();
setVisible(true);
}
private void labelUpdateText() {
label.setText("Count: " + count);
}
public static void main(String[] args) {
new TestGUI();
new TestGUI();
}
}