chapter iv(modern gui)
TRANSCRIPT
Introducing Swing(modern Java 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,
Component
Layout Manager• Flow Layout
• Grid Layout
• Border Layout
• Card Layout
DIALOG BOXES
• message dialog box, • coni rmation dialog box, and • input dialog box.
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);
}
}
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();}}
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); }}
import java.awt.GridLayout;GridLayout fl = new GridLayout(4,2); windowContent.setLayout(fl);
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); } }
Event Handling