swing handbook

Download Swing  Handbook

Post on 18-Nov-2014

667 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

Swing

Mr.P.M.Sawant. Dept. Of Computer Science Prof.Ramkirshna More ACS College,Akurdi Email Id:sawanprasad@gmail.Com.

Dept. Of Computer ScienceProf.Ramkirshna More ACS College,Akurdi

SwingSwing is a widget toolkit for Java. It is part of Sun Microsystems' Java Foundation Classes (JFC) an API for providing a graphical user interface (GUI) for Java programs.Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit. Swing provides a native look and feel that emulates the look and feel of several platforms, and also supports a pluggable look and feel that allows applications to have a look and feel unrelated to the underlying platform.

Relationship to AWTSince early versions of Java, a portion of the Abstract Window Toolkit (AWT) has provided platformindependent APIs for user interface components. In AWT, each component is rendered and controlled by a native peer component specific to the underlying windowing system. By contrast, Swing components are often described as lightweight because they do not require allocation of native resources in the operating system's windowing toolkit. The AWT components are referred to as heavyweight components. Much of the Swing API is generally a complementary extension of the AWT rather than a direct replacement. In fact, every Swing lightweight interface ultimately exists within an AWT heavyweight component because all of the top-level components in Swing (JApplet, JDialog, JFrame, and JWindow) extend an AWT top-level container. However, the use of both lightweight and heavyweight components within the same window is generally discouraged due to Z-order incompatibilities. The core rendering functionality used by Swing to draw its lightweight components is provided by Java 2D, another part of JFC

Dept. Of Computer ScienceProf.Ramkirshna More ACS College,Akurdi

SwingJAppletConstructor JApplet() Creates a swing applet instance.

Demo import java.awt.*; import javax.swing.*; /**/ public class JAppletDemo extends JApplet { public void init() { Container content = getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout()); content.add(new JButton("Button 1")); content.add(new JButton("Button 2")); content.add(new JButton("Button 3")); } }

Dept. Of Computer ScienceProf.Ramkirshna More ACS College,Akurdi

SwingJFrame Constructor Summary JFrame() Constructs a new frame that is initially invisible. JFrame(GraphicsConfiguration gc) reates a Frame in the specified GraphicsConfiguration of a screen device and a blank title. JFrame(String title) Creates a new, initially invisible Frame with the specified title. JFrame(String title, GraphicsConfiguration gc) Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device.

Demo import java.awt.*; import javax.swing.*; public class JFrameDemo { public static void main(String[] args) { JFrame f = new JFrame("JFrame Demo"); f.setSize(400, 150); Container content = f.getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout()); content.add(new JButton("Button 1")); content.add(new JButton("Button 2")); content.add(new JButton("Button 3")); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } Output

Dept. Of Computer ScienceProf.Ramkirshna More ACS College,Akurdi

SwingJLabel

Constructor Summary JLabel() Creates a JLabel instance with no image and with an empty string for the title. JLabel(Icon image) Creates a JLabel instance with the specified image. JLabel(Icon image, int horizontalAlignment) Creates a JLabel instance with the specified image and horizontal alignment. JLabel(String text) Creates a JLabel instance with the specified text. JLabel(String text, Icon icon, int horizontalAlignment) Creates a JLabel instance with the specified text, image, and horizontal alignment. JLabel(String text, int horizontalAlignment) Creates a JLabel instance with the specified text and horizontal alignment.

Demoimport java.awt.*; import javax.swing.*; public class JLabelDemo extends JFrame { public static void main(String[] args) { new JLabelDemo(); } public JLabelDemo() { JFrame f=new JFrame(); JLabel coloredLabel = new JLabel("Hello!",Label.CENTER); Container c=getContentPane(); c.setLayout(new GridLayout(0,1)); c.add(coloredLabel); pack(); f.add(c); f.setVisible(true); f.setSize(150,150); } }

Output

Dept. Of Computer ScienceProf.Ramkirshna More ACS College,Akurdi

SwingJButtonConstructor Summary JButton() Creates a button with no set text or icon. JButton(Action a) Creates a button where properties are taken from the Action supplied. JButton(Icon icon) Creates a button with an icon. JButton(String text) Creates a button with text. JButton(String text, Icon icon) Creates a button with initial text and an icon. Demo import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JButtonDemo extends JFrame implements ActionListener { private JButton btn_simple, btn_image; public JButtonDemo() { setTitle("Testing Buttons"); Container container = getContentPane(); container.setLayout(new GridLayout(0,2)); btn_simple = new JButton("Simple Button"); btn_simple.setMnemonic(KeyEvent.VK_S); container.add(btn_simple); Icon bug1 = new ImageIcon("D:\\menu_1.gif"); btn_image = new JButton("Image",bug1); btn_image.setMnemonic(KeyEvent.VK_I); container.add(btn_image); btn_image.addActionListener(this); btn_simple.addActionListener(this); setSize(300,100); setVisible(true); } public static void main(String args[]) { new JButtonDemo(); } public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(null,"You pressed: " + ae.getActionCommand()); } }

Dept. Of Computer ScienceProf.Ramkirshna More ACS College,Akurdi

SwingOutput

Dept. Of Computer ScienceProf.Ramkirshna More ACS College,Akurdi

Swing

JCheckBoxConstructor Summary JCheckBox() Creates an initially unselected check box button with no text, no icon. JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied. JCheckBox(Icon icon) Creates an initially unselected check box with an icon. JCheckBox(Icon icon, boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected. JCheckBox(String text) Creates an initially unselected check box with text. JCheckBox(String text, boolean selected) Creates a check box with text and specifies whether or not it is initially selected. JCheckBox(String text, Icon icon) Creates an initially unselected check box with the specified text and icon. JCheckBox(String text, Icon icon, boolean selected) Creates a check box with text and icon, and specifies whether or not it is initially selected. Demo import java.awt.Container; import java.awt.event.*; import javax.swing.*; class JCheckboxDemo { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container cp = frame.getContentPane(); Box box = new Box(BoxLayout.Y_AXIS); cp.add(box); box.add(new JLabel("Tick the sports you like...")); String[] sports = {"Football", "Rugby", "Cricket","Badminton"}; JCheckBox[] cba = new JCheckBox[sports.length]; for (int i = 0; i < sports.length; i++) { cba[i] = new JCheckBox(sports[i]); box.add(cba[i]); } frame.pack();

Dept. Of Computer ScienceProf.Ramkirshna More ACS College,Akurdi

Swingframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } } Output

Dept. Of Computer ScienceProf.Ramkirshna More ACS College,Akurdi

SwingJRadioButtonConstructor Summary JRadioButton() Creates an initially unselected radio button with no set text. JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied. JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no text. JRadioButton(Icon icon, boolean selected) Creates a radio button with the specified image and selection state, but no text. JRadioButton(String text) Creates an unselected radio button with the specified text. JRadioButton(String text, boolean selected) Creates a radio button with the specified text and selection state. JRadioButton(String text, Icon icon) Creates a radio button that has the specified text and image, and that is initially unselected. JRadioButton(String text, Icon icon, boolean selected) Creates a radio button that has the specified text, image, and selection state.

Demo import java.awt.*; import java.awt.event.*; import javax.swing.*; class JRadioButtonDemo { public static void main(String[] args) { ActionListener listener = new ActionListener() { public void actionPerformed (ActionEvent e) { System.out.println(e.getActionCommand());} }; Box p = new Box(BoxLayout.Y_AXIS); ButtonGroup group = new ButtonGroup(); String[] sa = {"ugli","kiwi","passion","kumquat"}; for(int i=0;i

Recommended

View more >