Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

JAVA Gpu

Uploaded by

naninani102112
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

JAVA Gpu

Uploaded by

naninani102112
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT V

GUI PROGRAMMING WITH SWING

Introduction to Java GUI Programming with Swing:

Java Swing is a GUI toolkit and a part of JFC (Java Foundation Class) helpful in developing window-
based applications.

Java Swing is lightweight and platform-independent that contains various components and container
classes.

It provides a wide range of components, such as buttons, text fields, and menus, that can be used to create
attractive and functional GUIs.

In Java Swing, there are several components like a scroll bar, button, text field, text area, checkbox, radio
button, etc. These components jointly form a GUI that offers a rich set of functionalities and allows high-
level customization.

What is Java Swing?

Java Swing is a set of graphical user interface (GUI) components that are part of the Java
platform. Swing components are built on top of the Abstract Window Toolkit (AWT), but they provide a
number of advantages over AWT components. For example, Swing components are more lightweight and
efficient, and they are platform-independent.

How to install Java Swing

Java Swing is included in the Java Development Kit (JDK).

To install the JDK, go to the Oracle website and download the latest version of the JDK for your
operating system. Once the JDK is installed, you can start creating Swing GUIs.

Main Features of Swing Toolkit

 Platform Independent
 Customizable
 Extensible
 Configurable
 Lightweight

Swing and JFC

JFC is an abbreviation for Java Foundation classes, which encompass a group of features for
building Graphical User Interfaces(GUI) and adding rich graphical functionalities and interactivity to Java
applications.

Features of JFC

 Swing GUI components.


 Look and Feel support.
 Java 2D.

Introduction to Swing Classes

JPanel : JPanel is Swing's version of AWT class Panel and uses the same default layout, FlowLayout.
JPanel is descended directly from JComponent.

JFrame : JFrame is Swing's version of Frame and is descended directly from Frame class. The
component which is added to the Frame, is refered as its Content.

JWindow : This is Swing's version of Window and hass descended directly from Window class.
Like Window it uses BorderLayout by default.

JLabel : JLabel descended from Jcomponent, and is used to create text labels.

JButton : JButton class provides the functioning of push button. JButton allows an icon, string or both
associated with a button.

JTextField : JTextFields allow editing of a single line of text.

Creating a JFrame

There are two way to create a JFrame Window.

 By instantiating JFrame class.


 By extending JFrame class.

Swing components

Swing provides a wide range of components that can be used to create GUIs. Swing Framework contains
a large set of components which provide rich functionalities and allow high level of customization. All
these components are lightweight components. They all are derived from JComponent class.Some of the
most commonly used components include:

 Buttons
 Text fields
 Labels
 Menus
 Panels
 Layout managers

JButton

JButton class provides functionality of a button. The button can include some display text or
images. It yields an event when clicked and double-clicked. JButton class has three constuctors,

JButton(Icon ic)

JButton(String str)
JButton(String str, Icon ic)

It allows a button to be created using icon, a string or both. JButton supports ActionEvent. When a button
is pressed an ActionEvent is generated.

Syntax:

JButton okBtn = new JButton(“Click”);

EXAMPLE:

import javax.swing.*;

public class Jbutton_Class{

public static void main(String[] args) {

JFrame f=new JFrame("JButton ");

JButton b=new JButton("Click Here");

b.setBounds(85,110,105,50);

f.add(b);

f.setSize(300,300);

f.setLayout(null);

f.setVisible(true);

} }

JLabel

JLabel is a Java Swing component. JLabel can show text, images, or a combination of the two. JLabel does not
respond to input events like mouse or keyboard focus.

Syntax:

JLabel textLabel = new JLabel(“This is 1st L...”);

The JLabel Contains four constructors. They are as follows:


JLabel(): It generates a blank label with no text or image.
JLabel(String s): It generates a new label with the provided string.
JLabel(Icon I): This produces a new label with an image.
JLabel(String s, Icon i, int align): Creates a new label containing a string, an image, and a given
horizontal alignment using this.
JLabel(Icon i, int align): Creates a JLabel instance with the picture and horizontal alignment that you
provide.
JLabel(String s, int align): Creates a JLabel instance with the text and horizontal alignment that you
provide.

Exаmple:

import javax.swing.*;

class Main

public static void main(String args[])

JFrame myframe= new JFrame("Simple JLabel Example");

JLabel label1,label2;

label1=new JLabel("This is 1st Label");

label1.setBounds(50,50, 100,30);

label2=new JLabel("This is 2nd Label");

label2.setBounds(50,100, 100,30);

myframe.add(label1); myframe.add(label2);

myframe.setSize(300,300);

myframe.setLayout(null);

myframe.setVisible(true); } }
JTextField

The JTextField renders an editable single-line text box. Users can input non-formatted text in the
box. We can initialize the text field by calling its constructor and passing an optional integer parameter.
This parameter sets the box width measured by the number of columns. Also, it does not limit the number
of characters that can be input into the box. It has three constructors:

JTextField(): It generates a new TextField Constructor.


JTextField(int columns): It generates a new empty TextField with the given number of columns.
JTextField(String text): Constructor for creating a new empty text field with the specified string as its
first value.
JTextField(String text, int columns): Constructor that generates a new empty textField with a specified
number of columns and the given string.
JTextField(Document doc, String text, int columns): Constructor for a textfield with the specified text
storage model and a number of columns.

Syntax:

JTextField txtBox = new JTextField(50);

Exаmple:

import javax.swing.*;

class Main

public static void main(String args[])

JFrame myframe= new JFrame("SimpleTextFieldExample");

JTextField text1,text2;

text1=new JTextField("Welcome to Codingninjas");

text1.setBounds(50,100, 200,30);

text2=new JTextField("JTextField in Java");

text2.setBounds(50,150, 200,30);

myframe.add(text1); myframe.add(text2);

myframe.setSize(400,400);

myframe.setLayout(null);

myframe.setVisible(true);
} }

JCheckBox:

The JCheckBox renders a check-box with a label. The check-box has two states, i.e., on and off. On
selecting, the state is set to "on," and a small tick is displayed inside the box.

Syntax:

CheckBox chkBox = new JCheckBox(“Java Swing”, true);

Constructors of JCheckBox

JCheckBox() - Initially, an unselected check box button is created with no text or icon.
JCheckBox(Action a) - This constructor creates a checkbox where the properties are taken from the
Action supplied.
JCheckBox(Icon icon) - This constructor initially creates an unselected checkbox with an icon.
JCheckBox(Icon icon, boolean selected) - It creates a checkbox with an icon that specifies if it is
initially selected or unselected.
JCheckBox(String text) - This constructor creates an unselected checkbox with the text.
JCheckBox(String text, boolean selected) - A checkbox with text is created and specifies if it is initially
selected or unselected.
JCheckBox(String text, Icon icon) - An unselected checkbox is created with the specified icon and text.
JCheckBox(String text, Icon icon, boolean selected) - A checkbox is created with text and icon and
specifies if it is initially selected or unselected.

Exаmple:

import java.awt.*;

import javax.swing.*;

class solve extends JFrame

static JFrame f;
public static void main(String[] args)

f = new JFrame("Coding Ninjas");

f.setLayout(new FlowLayout());

JCheckBox c1 = new JCheckBox("Ninja 1");

JCheckBox c2 = new JCheckBox("Ninja 2");

JPanel p = new JPanel();

p.add(c1);

p.add(c2);

f.add(p);

f.setSize(400, 400);

f.show();

}}

JRadioButton

Radio button is a group of related button in which only one can be selected. JRadioButton class is
used to create a radio button in Frames.

Syntax:

JRadioButton jrb = new JRadioButton("Easy");

Exаmple:

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class Test extends JFrame

public Test()

{
JRadioButton jcb = new JRadioButton("A"); //creating JRadioButton.

add(jcb); //adding JRadioButton to frame.

jcb = new JRadioButton("B"); //creating JRadioButton.

add(jcb); //adding JRadioButton to frame.

jcb = new JRadioButton("C"); //creating JRadioButton.

add(jcb); //adding JRadioButton to frame.

jcb = new JRadioButton("none");

add(jcb);

setLayout(new FlowLayout());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(400, 400);

setVisible(true);

public static void main(String[] args)

new Test();

}}

JComboBox

Combo box is a combination of text fields and drop-down list.JComboBox component is used to
create a combo box in Swing.

Syntax:

JComboBox jcb = new JComboBox(name);

Exаmple:

import javax.swing.*;

public class ComboBoxExample {


JFrame f;

ComboBoxExample(){

f = new JFrame("ComboBox Example");

String country[]={“Apple”, “Guava”, “Grapes”, “Mango”, “Orange”};

JComboBox cb=new JComboBox(country);

cb.setBounds(50, 50,90,20);

f.add(cb);

f.setLayout(null);

f.setSize(400,500);

f.setVisible(true);

public static void main(String[] args) {

new ComboBoxExample();

}}

JTabbedPane

The JTabbedPane is another beneficial component that lets the user switch between tabs in an application.
It is a handy utility as it allows users to browse more content without navigating to different pages.

Syntax:

JTabbedPane jtabbedPane = new JTabbedPane();

jtabbedPane.addTab(“Tab_1”, new JPanel());

jtabbedPane.addTab(“Tab_2”, new JPanel());

The above code creates a two-tabbed panel with headings Tab_1 and Tab_2.
JMenuBar, JMenu and JMenuItem

In Java, the Swing toolkit contains a JMenuBar, JMenu, and JMenuItem class. It is under package
javax.swing.JMenuBar, javax.swing.JMenu and javax.swing.JMenuItem class. The JMenuBar class is
used for displaying menubar on the frame. The JMenu Object is used to pull down the menu bar's
components. The JMenuItem Object is used for adding the labeled menu item.

JMenuBar, JMenu and JMenuItem Declarations:

public class JMenuBar extends JComponent implements MenuElement, Accessible

public class JMenu extends JMenuItem implements MenuElement, Accessible

public class JMenuItem extends AbstractButton implements Accessible, MenuElement

JToggleButton

A JToggleButton is a button with two states. Selected and unselected are the two statuses. This
class is subclassed by the JRadioButton and JCheckBox classes. The toggle button toggles between being
pressed and unpressed when the user presses it. JToggleButton is a button that allows you to choose from
a list of options.

Constructors

JToggleButton(): Creates a toggle button with no text or image that is initially unselected.

JToggleButton(Action a): This method creates a toggle button with properties based on the Action.

JToggleButton(Icon icon): Creates a toggle button with the provided image but no text that is initially
unselected.

JToggleButton(Icon icon, boolean selected): Creates a toggle button with the provided image and
selection status, but no text.

JToggleButton(String text): Creates an unselected toggle button with the provided text.

JToggleButton(String text, boolean selected): Creates a toggle button with the supplied text and
selection status.

JToggleButton(String text, Icon icon): This method creates an unselected toggle button with the
provided text and image.

JToggleButton(String text, Icon icon, boolean selected): Creates a toggle button with the supplied text,
image, and selection status.

JAVA JSCROLLPANE
A JscrollPane is used to make scrollable view of a component. When screen size is limited, we use a
scroll pane to display a large component or a component whose size can change dynamically.

Constructors

JScrollPane()

JScrollPane(Component)

JScrollPane(int, int)

JScrollPane(Component, int, int)

JList

JList in Java is one of the Java Swing components. These components include buttons, sliders, list
boxes, checkboxes, etc. JList is defined in Java in the ‘java.swing’ package. JList is a component that
shows a list of objects and allows the user to choose one or more of them. JList is inherited from the
JComponent class. JComponent is an abstract class that serves as the foundation for all Swing
components.

Class Declaration

public class JList extends JComponent implements Scrollable, Accessible

JList() Creates a JList with an empty, read-only, model.

JList(ary[] listData) Creates a JList that displays the elements in the specified array.

JList(ListModel<ary> dataModel) Creates a JList that displays elements from the specified, non-
null, model.

JDialog

The JDialog control represents a top level window with a border and a title used to take some
form of input from the user. It inherits the Dialog class.

JDialog class declaration

public class JDialog extends Dialog implements WindowConstants, Accessible, RootPaneContainer

Constructors:

JDialog() - It is used to create a modeless dialog without a title and without a specified Frame
owner.

JDialog(Frame owner) - It is used to create a modeless dialog with specified Frame as its owner
and an empty title.

JDialog(Frame owner, String title, boolean modal) - It is used to create a dialog with the specified
title, owner Frame and modality.

You might also like