Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1 - I would rather avoid extending JFrame, since you are not really creating a new class of JFrame.</p> <p>So instead of having your whole class as a subclass of JFrame ( which is not ) you could create instances of them with out adding more behavior.</p> <p>2 - If you want to have two labels reflecting the value of a same "thing" you have to <strong>share</strong> that thing between them ( or have someone to update that value for you ) </p> <p>So, using the famous <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" rel="nofollow noreferrer">MVC</a> you'll need.</p> <ul> <li><p><strong>Model</strong> the counter you want to show in both labels.</p></li> <li><p><strong>View</strong> the labels where the model will be shown </p></li> <li><p><strong>Controller</strong> Something that deals between them.</p></li> </ul> <p>They all belong to an application, they are instance attributes of the app. </p> <p>To see how they fit together I'm pasting here the code:</p> <pre><code>import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.*; // TwoWindows is the Application public class TwoWindows { // shared state ( model ) private int clickCount = 0; // UI private List&lt;JLabel&gt; toUpdate = new ArrayList&lt;JLabel&gt;(); // listener ( listens for clicks on buttons kind of controller ) private ActionListener actionListener = new ActionListener() { // Each time update the UI public void actionPerformed( ActionEvent e ) { clickCount++; for( JLabel label : toUpdate ) { label.setText( "Count: " + ( clickCount ) ); } } }; // Createsa winddow with a label and a button public void showWindow( String named ) { JFrame f = new JFrame( named ); f.add( createButtonAndLabel() ); f.pack(); f.setVisible( true ); } // Creates the label and button and adds this.actionListener // to each button. private JComponent createButtonAndLabel() { JPanel panel = new JPanel(); JLabel label = new JLabel("Count: 0"); JButton clickMe = new JButton("Click me"); // adding the label to a "view" list. toUpdate.add( label ); // adding the listener to each button clickMe.addActionListener( actionListener ); panel.add( label ); panel.add( clickMe ); return panel; } // Run the app public static void main( String [] args ) { TwoWindows t = new TwoWindows(); t.showWindow("A"); t.showWindow("B"); } } </code></pre> <p>This way you could have a shared model and update as many views as you want. </p> <p><a href="http://img387.imageshack.us/img387/1106/capturadepantalla200910d.png" rel="nofollow noreferrer">alt text http://img387.imageshack.us/img387/1106/capturadepantalla200910d.png</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload