Professional Documents
Culture Documents
What is JFC?
JFC Java Foundation Classes Five key pieces:
Java 1.1 Abstract Window Toolkit (AWT) Java2D API ***Swing*** Native Drag and Drop Accessibility API
AWT
AWT stands for Abstract Window ToolKit. The Abstract Window Toolkit supports GUI Java programming. It is a portable GUI library for stand-alone applications and/or applets. The Abstract Window Toolkit provides the connection between your application and the native GUI.
Swing Pros
Portability: Pure Java design provides for fewer platform specific limitations. Behavior: Pure Java design allows for a greater range of behavior for Swing components since they are not limited by the native peers that AWT uses. Features: Swing supports a wider range of features like icons and pop-up tool-tips for components.
Vendor Support: Swing development is more active. Sun puts much more energy into making Swing robust. Look and Feel: The pluggable look and feel lets you design a single set of GUI components that can automatically have the look and feel of any OS platform (Microsoft Windows, Solaris, Macintosh, etc.). It also makes it easier to make global changes to your Java programs that provide greater accessibility (like picking a hi-contrast color scheme or changing all the fonts in all dialogs, etc.).
Window
ScrollPane
Frame
JComponent
JApplet JDialog
JFrame
JWindow
Object
Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects implement the methods of this class. The Component class is the abstract superclass of many of the AWT classes. It represents something that has a position, a size, can be painted on the screen, and can receive input events.
Component
Some Methods Defined in Class Component: setForeground(Color) set the components foreground color setBackground(Color) set the components background color setFont(Font) set the components font paint(Graphics) repaint( )
repaint the component schedule the component for repainting
addxxxListener(xxxListener)
add an xxx listener for the component (where xxx is Component, Focus, Key, Mouse, or MouseMotion)
An xxxListener is an object that waits for an event to occur, then executes certain actions in response to the event.
Swing Components
Swing classes
JComponent
JApplet JDialog
JFrame
JWindow
JList
JScrollPane
others
JTextField JTextArea
JEditorPane
JMenuItem JButton
JToggleButton
JCheckBox JRadioButton
More Components
FontChooser JColorChooser JDesktopIcon JDirectoryPane
JFileChooser
JOptionPane JProgressBar
10
JRootPane JSeparator JSlider JSplitPane JTabbedPane JTable JToolBar JToolTip JTree JViewport
Layout Managers
Each container has a layout manager, which controls the way the components are positioned in the container. One of the advantages of using layout managers is that there is no need for absolute coordinates where each component is to be placed or the dimensions of the component. The layout manager automatically handles the calculation of these. Programmer only specifies relative positions of the components within the container. There are many different layout managers, this presentation describes the most popular ones. Different layout managers can be used interchangeably and one inside the other. Whenever the dimensions of the container change (e.g. user resizes the window), layout manager recalculates the absolute coordinates of components for them to fit the new container size.
Border Layout
The Border Layout manager arranges components into five regions: North, South, East, West, and Center. Components in the North and South are set to their natural heights and horizontally stretched to fill the entire width of the container. Components in the East and West are set to their natural widths and stretched vertically to fill the entire width of the container. The Center component fills the space left in the center of the container.
import java.awt.*; import java.applet.*; import javax.swing.*; public class BorderLayoutExample2 extends Jframe { public static void main(String[] args) { BorderLayoutExample2 frame = new BorderLayoutExample2(); Frame.doSomething(); } void doSomething() { Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(new JButton("One"), BorderLayout.NORTH); c.add(new JButton("Two"), BorderLayout.WEST); c.add(new JButton("Three"), BorderLayout.CENTER); c.add(new JButton("Four"), BorderLayout.EAST); c.add(new JButton("Five"), BorderLayout.SOUTH); c.add(new JButton("Six"), BorderLayout.SOUTH);
Output:
Flow Layout
The Flow Layout manager arranges the components left-to-right, top-to-bottom in the order they were inserted into the container. When the container is not wide enough to display all the components, the remaining components are placed in the next row, etc. Each row is centered.
import java.awt.*; import java.applet.*; import javax.swing.*; public class FlowLayoutExample extends JApplet { public void init () { getContentPane().setLayout(new FlowLayout ()); getContentPane().add(new JButton("One")); getContentPane().add(new JButton("Two")); getContentPane().add(new JButton("Three")); getContentPane().add(new JButton("Four")); getContentPane().add(new JButton("Five")); getContentPane().add(new JButton("Six")); }
Output:
Grid Layout
The Grid Layout manager lays out all the components in a rectangular grid. All the components have identical sizes, since the manager automatically stretches or compresses the components as to fill the entire space of the container.
import java.awt.*; import java.applet.*; import javax.swing.*; public class GridLayoutExample extends JApplet { public void init() { Container c = getContentPane(); c.setLayout(new GridLayout(2, 4)); c.add(new JButton("One")); c.add(new JButton("Two")); c.add(new JButton("Three")); c.add(new JButton("Four")); c.add(new JButton("Five")); } }
Output:
b1=new JButton("Add"); b2=new JButton("Sub"); b3=new JButton("Mul"); b4=new JButton("Div"); l=new JLabel(); l1=new JLabel();
} public void awt1() { f.setLayout(new GridLayout(3,2)); f.setVisible(true); f.add(t1); f.add(t2); f.add(t3); f.add(b1); f.add(b2); f.add(b3); f.add(b4);
f.add(l); f.add(l1); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); f.pack(); } public void actionPerformed(ActionEvent e) { String s=new String(e.getActionCommand()); l.setText(s); if((s).equals("Add")) { int a=Integer.parseInt(t1.getText()); int b=Integer.parseInt(t2.getText());
Integer c=a+b; t3.setText(c.toString()); } else if((s).equals("Sub")) { int a=Integer.parseInt(t1.getText()); int b=Integer.parseInt(t2.getText()); integer c=a-b; t3.setText(c.toString()); } else if((s).equals("Mul")) { int a=Integer.parseInt(t1.getText()); int b=Integer.parseInt(t2.getText()); Integer c=a*b; t3.setText(c.toString()); }