Unit-5: GUI Programming, Building Applets and Introduction to Annotations

Required packages,
  1. The java.awt package contains the core AWT graphics classes:
    • GUI Component classes (such as ButtonTextField, and Label),
    • GUI Container classes (such as FramePanelDialog and ScrollPane),
    • Layout managers (such as FlowLayoutBorderLayout and GridLayout),
    • Custom graphics classes (such as GraphicsColor and Font).
  2. The java.awt.event package supports event handling:
    • Event classes (such as ActionEventMouseEventKeyEvent and WindowEvent),
    • Event Listener Interfaces (such as ActionListenerMouseListenerKeyListener and WindowListener),
    • Event Listener Adapter classes (such as MouseAdapterKeyAdapter, and WindowAdapter).


Simple java application
import javax.swing.JOptionPane;

public class HelloWorldGUI1 {
   
   public static void main(String[] args) {
      JOptionPane.showMessageDialog( null, "Hello World!" );
   }

}
another simple source code for the java gui.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class HelloWorldGUI2 {
   
   private static class HelloWorldDisplay extends JPanel {
      public void paintComponent(Graphics g) {
         super.paintComponent(g);
         g.drawString( "Hello World!", 20, 30 );
      }
   }
   
   private static class ButtonHandler implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         System.exit(0);
      }
   }
   
   public static void main(String[] args) {
      
      HelloWorldDisplay displayPanel = new HelloWorldDisplay();
      JButton okButton = new JButton("OK");
      ButtonHandler listener = new ButtonHandler();
      okButton.addActionListener(listener);

      JPanel content = new JPanel();
      content.setLayout(new BorderLayout());
      content.add(displayPanel, BorderLayout.CENTER);
      content.add(okButton, BorderLayout.SOUTH);

      JFrame window = new JFrame("GUI Test");
      window.setContentPane(content);
      window.setSize(250,100);
      window.setLocation(100,100);
      window.setVisible(true);

   }
   
}

 JFrame is window frame
JFrame window = new JFrame("GUI Test");
to see the window set this configuration
window.setContentPane(content);
window.setSize(250,100);
window.setLocation(100,100);



other option is
  • Panel: a rectangular box under a higher-level container, used to layout a set of related GUI components in pattern such as grid or flow.
  • ScrollPane: provides automatic horizontal and/or vertical scrolling for a single child component.
others.



asd

No comments:

Post a Comment