Chapter iv(modern gui)

16
Introducing Swing(modern Java GUI)

Transcript of Chapter iv(modern gui)

Page 1: Chapter iv(modern gui)

Introducing Swing(modern Java GUI)

Page 2: Chapter iv(modern gui)

Introduction• Swing is a set of classes that provides more powerful

and flexible GUI components than does the AWT.• Swing program, including both applications and applets.• Swing has:– Component: derived from the JComponent class– Container: two types of container:

• The first are top-level containers: JFrame, JApplet, JWindow, and Jdialog : not inherit JComponent. Do, however, inherit the AWT classes Component and Container.

• Lightweight containers do inherit Jcomponent: Jpanel,

Page 3: Chapter iv(modern gui)

Component

Page 4: Chapter iv(modern gui)

Layout Manager• Flow Layout

• Grid Layout

• Border Layout

• Card Layout

Page 5: Chapter iv(modern gui)

DIALOG BOXES

• message dialog box, • coni rmation dialog box, and • input dialog box.

Page 6: Chapter iv(modern gui)

import javax.swing.*;public class TestSwing1{public static void main(String[] args){int age;String name;name=JOptionPane.showInputDialog("Enter your name:");age=Integer.parseInt(JOptionPane.showInputDialog("Enter your age:"));

JOptionPane.showMessageDialog(null,"Your name is :" + name + "\n\r Your age is " + age);

}

}

Page 7: Chapter iv(modern gui)

Your firstimport javax.swing.*;class TestSwing1 {TestSwing1() {// Create a new JFrame container.JFrame jfrm = new JFrame("First Swing step");// Give the frame an initial size.jfrm.setSize(275, 100);// Terminate the program when the user closes the application.jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Create a text-based label.JLabel jlab = new JLabel(" Swing means powerful GUIs.");//Add the label to the content pane.jfrm.add(jlab);//Display the frame.jfrm.setVisible(true);}public static void main(String args[]) {new TestSwing1();}}

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

class TestSwing1 extends JFrame {JLabel jlab ;TestSwing1() {// Create a new JFrame container.super ("First Swing step");// Give the frame an initial size.setSize(275, 100);// Terminate the program when the user closes the application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Create a text-based label.jlab=new JLabel();Font font = new Font("Limon S1", Font.BOLD, 30);jlab.setFont(font);

jlab.setText("sYsþIBiPBelakd¾QWcab;");//Add the label to the content pane.add(jlab);//Display the frame.setVisible(true);}public static void main(String args[]) {new TestSwing1();}}

Page 8: Chapter iv(modern gui)

import java.awt.*;import javax.swing.*;import java.util.*;public class TestSwing1 extends JFrame{public TestSwing1(int width, int height){super("Alphabet + Numeric");setLayout(new GridLayout(6,5));setBounds(0, 0, width, height);for(int i=0;i<26;i++){Character alphabet =(char)(i + 'A'); JButton button =new JButton(alphabet.toString());add(button);}for(int i=0;i<10;i++){JButton buttonNum=new JButton(i + "");add(buttonNum);}}public static void main(String[] args) { JFrame frame =new TestSwing1 (300, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}

Page 9: Chapter iv(modern gui)

import java.awt.GridLayout;GridLayout fl = new GridLayout(4,2); windowContent.setLayout(fl);

Page 10: Chapter iv(modern gui)
Page 11: Chapter iv(modern gui)
Page 12: Chapter iv(modern gui)
Page 13: Chapter iv(modern gui)

import javax.swing.*; import java.awt.*; public class TestSwing1 extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); // Call the paintComponent method of the parent g.setColor (Color.BLACK); // Use black for drawing in the panel Font font = new Font("Limon S1", Font.BOLD, 80); g.setFont (font); // Uses the Flat Brush font when drawing a String setBackground(Color.GRAY) ; g.drawString ("sYsþI kMuBüÚT½r)ak;kaNUt", 50, 50); } public static void main(String [ ] args) { JFrame frame = new JFrame("<---------->"); frame.setBounds(0, 0, 600, 200); TestSwing1 ts = new TestSwing1(); frame.add(ts); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

Page 14: Chapter iv(modern gui)

Event Handling

Page 15: Chapter iv(modern gui)
Page 16: Chapter iv(modern gui)