Anzeige eines JWindows

  • Themenstarter Themenstarter sp2009
  • Beginndatum Beginndatum
S

sp2009

Gast
Hallo,

Ich habe hier einen Ausschnitt zur Anzeige eines Swing-Fensters. Dies funktioniert auch.

Ich würde nur gern wissen, WARUM es NICHT funktioniert, wenn die markierte Zeile aktiviert wird.

Java:
class TrSiWindow extends JWindow /* handle IO to screen */
  implements ActionListener {

 ...
 
 /**. <pre>
  *=====================================================================
  * Constructor: (2) Create Standard-Class
  *=====================================================================
  </pre>*/
  protected TrSiWindow(  /* create window specified by control-class */
   TrSiControl cC) /* use current control-record */
   {
   /*-----------------------------------------------------------------*/
    Font      dF;    /* default-Font of the buttons */
    Rectangle cBnds; /* Bounds of the Screen */
    JPanel    cP;    /* current panel(s) */
    JButton   cB;    /* temporary storage of button */
    JLabel    cL;    /* temporary storage of label */
    Border    cBdr;  /* border-definition for buttons */
    double    d;     /* temporary value */
   /*=================================================================*/
    cCtrl = cC; /* save current control-definitions */
    cBnds = cCtrl.getBounds(); /* get requested bounds of the window */

    /* assure that aspect-value matches 4:3 */
//    d = (cBnds.getWidth() * 3) / 4; /* compute new height */
//    if(d<=cBnds.getHeight()) /* requested height is smaller */
//      cBnds.setRect(cBnds.getX(),cBnds.getY()+(cBnds.getHeight()-d)/2,
//                    cBnds.getWidth(),d);
//    else { /* width-value should be adjusted */
//      d = (cBnds.getHeight() * 4) / 3; /* compute new width */
//      cBnds.setRect(cBnds.getX()+(cBnds.getWidth()-d)/2,cBnds.getY(),
//                    d,cBnds.getHeight());
//    } /* width has been modified due to valid height */
    this.setBounds(cBnds); /* establish bounds of main window */
    System.out.println("ScreenBounds = "+cBnds.toString());

    Screen = this.getContentPane(); /* get and save drawing-instance */
    Screen.setLayout(null); /* activate new layout of the whole screen */

    BGI = new ImageIcon("stone2.jpg"); /* load image as icon */
    System.out.println("Background = "+BGI.toString());
    cL = new JLabel(BGI); /* create background-picture */
    cL.setBounds(cBnds); /* and add bounds of whole window */
    Screen.add(cL); /* set background-picture */

    dF = new Font(null,Font.BOLD,24); /* create new Default-Font */
    cBdr = BorderFactory.createCompoundBorder(
                BorderFactory.createBevelBorder(BevelBorder.RAISED),
                BorderFactory.createEtchedBorder());
    cBnds.grow(-(int)Math.ceil(cBnds.getWidth()/3),-(int)Math.ceil(cBnds.getHeight()/3));
      /* new bounds are 1/3 of the main-window, placed in the middle of it */
    System.out.println("GridBounds = "+cBnds.toString());

    cP = new JPanel(new GridLayout(5,1)); /* create additional panel for layout */
    cP.setBounds(cBnds); /* set bounds of the inner panel */
    cP.setOpaque(false); /* use transparent background */

    cB = new JButton(TrSiLogging.getKey("Win01B1")); /* create first
      * selection-button (Simulator) */
    cB.setBackground(Color.lightGray);
    cB.setFont(dF); /* activate new Font */
    cB.setBorder(cBdr); /* add Border to current Button */
    cB.addActionListener(this); /* use event of this class */
    cP.add(cB); /* insert button into first slot */

    cB = new JButton(TrSiLogging.getKey("Win01B2")); /* second button */
    cB.setBackground(Color.lightGray);
    cB.setFont(dF); /* activate new Font */
    cB.setBorder(cBdr); /* add Border to current Button */
    cB.addActionListener(this); /* use event of this class */
    cP.add(cB); /* insert button into second slot */

    cB = new JButton(TrSiLogging.getKey("Win01B3")); /* third button */
    cB.setBackground(Color.lightGray);
    cB.setFont(dF); /* activate new Font */
    cB.setBorder(cBdr); /* add Border to current Button */
    cB.addActionListener(this); /* use event of this class */
    cP.add(cB); /* insert button into third slot */

    cP.add(new JLabel()); /* add empty slot to layout */

    cB = new JButton(TrSiLogging.getKey("WinCan")); /* create 'cancel'-button */
    cB.setBackground(Color.red);
    cB.setFont(dF); /* activate new Font */
    cB.setBorder(cBdr); /* add Border to current Button */
    cB.addActionListener(this); /* use event of this class */
    cP.add(cB); /* insert button into fifth slot */

    Screen.add(cP,0); /* insert buttons in front of the background-icon */

//    this.pack();   //  <======= hier !!
    this.setVisible(true); /* activate display of window */
  }

PS: Dies ist nur ein Entwurf. Verschiedene Definitionen werden noch geändert (z.B. Icon->Image)

Mfg
sp2009
 
Hallo,

Wenn die Zeile 'this.pack()' aktiv ist, wird das Fenster in Windows NICHT erzeugt und angezeigt. Damit ist keine Selektion der dort definierten Button möglich und das Programm bleibt stehen.

Ist die Zeile nur als Kommentar vorhanden, wird das Fenster erzeugt, angezeigt und die Selektion der Button ist möglich.

Ich wüsste nur allzugern WARUM das so ist.

Mfg
sp2009

PS.: System: Win2K/WinXP; Runtime-Version 1.5_22
 
JAVA API hat gesagt.:
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated.

Wahrscheinlich ist iwie die "preferred size" 0 und daher wird das Fenser auf 0 verkleinert
 

Zurück
Oben