Templates

Home

Frame class
Change look and feel
Dialog box
 

 

Frame class

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class myFrame extends JFrame
{ public myFrame()
  {
    //Set frame properties
    setSize(800,600);
    setTitle("Event handling");

//  Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
//    frame.setVisible(true);


    //To avoid writing all the code for the WindowsListener interface
    //to implement Exit of the application, you can avoid the
    //WindowsListener interface by writing this code
    addWindowListener(new WindowAdapter()
      {  public void windowClosing(WindowEvent e)
         { System.exit(0);
         }
      } );

  }
}

 

Change look and feel

If you want to use motif or metal look and feel, the use "com.sun.java.swing.plaf.motif.MotifLookAndFeel" or 
"com.sun.java.swing.plaf.metal.MetalLookAndFeel" instead of "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" in the example 
below.

 

      try
        { //Call the static setLookAndFeel method and give it the name of the
          //look and feel you want
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
          //Refresh components
          SwingUtilities.updateComponentTreeUI(this);
         }
       catch(Exception e) {}

Dialog box

The example below, shows how to make and show a modal dialog screen. Note that for simple dialogs, it's easyer to use 
the JOptionPane Class
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;



public class LogonScreen extends JDialog
{
  public LogonScreen(JFrame parent)
   {
     super(parent, "Logon to SAP",true);

    //---------------------------------------------------------------
    //Set frame properties
    //---------------------------------------------------------------
    setSize(400,400);
    setTitle("Logon to SAP");
    setLocation(225,155);

   //---------------------------------------------------------------
   // Center screen
   //---------------------------------------------------------------

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);


    Container myContentPane = getContentPane();

   //---------------------------------------------------------------
   // Create buttons
   //---------------------------------------------------------------
    JPanel buttonPanel = new JPanel();
    JButton cancel = new JButton("Cancel");
    JButton logOn = new JButton("Logon");
    buttonPanel.add(logOn);
    buttonPanel.add(cancel);

   //---------------------------------------------------------------
   // Action listeners
   //---------------------------------------------------------------
    cancel.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent evt)
        { setVisible(false);
        }
      } );

    logOn.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent evt)
        { sapLogon();
        }
      } );


    myContentPane.add(buttonPanel, "South");


   } //public LogonScreen()

   //---------------------------------------------------------------
   // Methods
   //---------------------------------------------------------------
   private void sapLogon()
   {
    //Do something
   }


 } //End of class
To Call the dialog box from a frame, add this method to the frame and call it:
 private void SapLogon()
  {
    LogonScreen logonscreen1 = new LogonScreen(this);
    logonscreen1.show();

  }

 

 

 

 

Converted from CHM to HTML with chm2web Standard 2.7 (unicode)