0% found this document useful (0 votes)
3 views

Assignment 2

The document discusses the differences between AWT and Swing, highlighting Swing's platform independence due to its integration with the Java Foundation Classes. It provides code examples for creating a login form using JFrame and JApplet, as well as demonstrating various Swing components like JTabbedPane, JComboBox, and JRadioButton. Additionally, it explains the MVC architecture in the context of Swing, detailing the roles of the model, view, and controller.

Uploaded by

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

Assignment 2

The document discusses the differences between AWT and Swing, highlighting Swing's platform independence due to its integration with the Java Foundation Classes. It provides code examples for creating a login form using JFrame and JApplet, as well as demonstrating various Swing components like JTabbedPane, JComboBox, and JRadioButton. Additionally, it explains the MVC architecture in the context of Swing, detailing the roles of the model, view, and controller.

Uploaded by

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

Assignment 2

1. Differentiate between AWT & Swing

2. Why Swing is called platform independent?

Swing is called platform-independent because it is part of the Java Foundation Classes (JFC), which
are designed to provide a consistent user interface across different operating systems. Here are the
key reasons why Swing is platform-independent

3. Create a login form on JFrame & JApplet.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class LoginApplet extends JApplet implements ActionListener {

JTextField usernameField;

JPasswordField passwordField;

JLabel messageLabel;

public void init() {

setLayout(new FlowLayout());

add(new JLabel("Username:"));
usernameField = new JTextField(15);

add(usernameField);

add(new JLabel("Password:"));

passwordField = new JPasswordField(15);

add(passwordField);

JButton loginButton = new JButton("Login");

loginButton.addActionListener(this);

add(loginButton);

messageLabel = new JLabel();

add(messageLabel);

public void actionPerformed(ActionEvent e) {

String username = usernameField.getText();

String password = new String(passwordField.getPassword());

if (username.equals("admin") && password.equals("password")) {

messageLabel.setText("Login successful!");

} else {

messageLabel.setText("Invalid credentials!");

4. WAP to add Image on JButton & JLabel

5. WAP to demonstrate JCombobox ,JRadioButton


6. WAP to create a tree & Tabel & add it to the JScrollpane

7. WAP to demonstrate TabbedPane.

import javax.swing.*;

public class JTabbedPaneDemo extends JApplet

public void init()

JTabbedPane jtp = new JTabbedPane();

jtp.addTab("Languages", new LangPanel());

jtp.addTab("Colors", new ColorsPanel());

jtp.addTab("Flavors", new FlavorsPanel());

getContentPane().add(jtp);

class LangPanel extends JPanel

public LangPanel()

JButton b1 = new JButton("Marathi");

add(b1);

JButton b2 = new JButton("Hindi");

add(b2);

JButton b3 = new JButton("Bengali");

add(b3);

JButton b4 = new JButton("Tamil");

add(b4);

class ColorsPanel extends JPanel

{
public ColorsPanel()

JCheckBox cb1 = new JCheckBox("Red");

add(cb1);

JCheckBox cb2 = new JCheckBox("Green");

add(cb2);

JCheckBox cb3 = new JCheckBox("Blue");

add(cb3);

class FlavorsPanel extends JPanel

public FlavorsPanel()

JComboBox jcb = new JComboBox();

jcb.addItem("Vanilla");

jcb.addItem("Chocolate");

jcb.addItem("Strawberry");

add(jcb);

8. Explain MVC architecture.

Swing API architecture follows MVC architecture. The MVC design pattern consists of three modules:
model, view and controller. Model -The model represents the state (data) and business logic of the
application. View -The view module is responsible to display data i.e. it represents the presentation

Controller -The controller module acts as an interface between view and model. It intercepts all the
requests i.e. receives input and commands to Model / View to change accordingly

You might also like