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

Java unit 5

The document provides an overview of GUI programming in Java using Applets and Swing. It explains the differences between Applets and applications, the life cycle of an Applet, and various Swing components such as JApplet, JFrame, and GUI elements like buttons, checkboxes, and tables. Additionally, it includes code examples demonstrating how to create and use these components.

Uploaded by

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

Java unit 5

The document provides an overview of GUI programming in Java using Applets and Swing. It explains the differences between Applets and applications, the life cycle of an Applet, and various Swing components such as JApplet, JFrame, and GUI elements like buttons, checkboxes, and tables. Additionally, it includes code examples demonstrating how to create and use these components.

Uploaded by

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

GUI PROGRAMMING WITH SWING

1. What are Applets in Java?


Answer: Applets are Java programs that can be embedded in a web page and run in a browser. They are
designed to be small and fast and can provide interactive features to web applications.

Example:
```java
import java.applet.Applet;
import java.awt.Graphics;

public class SimpleApplet extends Applet {


public void paint(Graphics g) {
g.drawString("Hello, World!", 20, 20);
}
}

2. What are the differences between Applets and Applications?


Answer:
- Execution Environment: Applets run within a browser or an applet viewer, whereas applications run
standalone.
- Security Restrictions: Applets have strict security restrictions, such as limited file access and network
connectivity, while applications have more freedom.
- Life Cycle Methods: Applets have specific life cycle methods (`init`, `start`, `stop`, `destroy`) that
applications do not use.

3. What is the Life Cycle of an Applet?


Answer: The life cycle of an applet includes the following methods:
- `init()`: Initializes the applet.
- `start()`: Starts or resumes the execution of the applet.
- `stop()`: Stops the execution of the applet.
- `destroy()`: Cleans up resources before the applet is destroyed.

Example:
```java
import java.applet.Applet;
import java.awt.Graphics;

public class LifeCycleApplet extends Applet {


public void init() {
// Initialization code
}
public void start() {
// Code to execute when applet starts
}

public void stop() {


// Code to execute when applet stops
}

public void destroy() {


// Cleanup code
}

public void paint(Graphics g) {


g.drawString("Applet Life Cycle", 20, 20);
}
}
```

4. What are the types of Applets?


Answer:
- Local Applets: Stored on the user's local machine and loaded from the local file system.
- Remote Applets: Stored on a remote server and loaded over the internet.

5. How do you create an Applet?


Answer: To create an applet, extend the `Applet` class and override the `paint` method to provide the
applet's content.

Example:
```java
import java.applet.Applet;
import java.awt.Graphics;

public class MyApplet extends Applet {


public void paint(Graphics g) {
g.drawString("My Applet", 20, 20);
}
}
```

6. How do you pass parameters to Applets?


Answer: Parameters can be passed to applets using the `<PARAM>` tag in the HTML file.
Example:
HTML:
```html
<applet code="ParamApplet.class" width="300" height="300">
<param name="message" value="Hello, Applet!">
</applet>
```

Java:
```java
import java.applet.Applet;
import java.awt.Graphics;

public class ParamApplet extends Applet {


private String message;

public void init() {


message = getParameter("message");
if (message == null) {
message = "No parameter provided";
}
}

public void paint(Graphics g) {


g.drawString(message, 20, 20);
}
}
```

Swing in Java

7. What is Swing?
Answer: Swing is a part of Java's standard library for creating graphical user interfaces (GUIs). It provides
a rich set of components and a more flexible architecture than AWT.

8. What are the limitations of AWT that Swing addresses?


Answer:
- Platform Dependency: AWT components are heavyweight and rely on native code, making them less
portable.
- Limited Features: AWT provides fewer components and lacks advanced features.
- Look and Feel: AWT components have a native look and feel, limiting customization.
9. What is the MVC architecture in Swing?
Answer: The Model-View-Controller (MVC) architecture separates an application's data (Model), user
interface (View), and control logic (Controller). Swing components follow this architecture to allow for
greater flexibility and maintainability.

10. What are components and containers in Swing?


Answer:
- Components: Individual GUI elements such as buttons, text fields, and labels.
- Containers: Hold and manage the layout of components. Examples include `JFrame`, `JPanel`, and
`JDialog`.

11. How do you create a JApplet in Swing?


Answer: Extend the `JApplet` class and add components to the applet's content pane.

Example:
```java
import javax.swing.JApplet;
import javax.swing.JLabel;

public class MyJApplet extends JApplet {


public void init() {
JLabel label = new JLabel("Hello, JApplet!");
add(label);
}
}
```

12. How do you create a JFrame in Swing?


Answer: Create an instance of `JFrame`, set its properties, and add components.

Example:
```java
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyFrame extends JFrame {


public MyFrame() {
setTitle("My Frame");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello, JFrame!");
add(label);
}

public static void main(String[] args) {


MyFrame frame = new MyFrame();
frame.setVisible(true);
}
}
```

13. How do you use Icons and Labels in Swing?


Answer: Use the `JLabel` class to display text and images.

Example:
```java
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class IconLabelExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Icon and Label Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ImageIcon icon = new ImageIcon("path/to/icon.png");


JLabel label = new JLabel("Hello, Swing!", icon, JLabel.CENTER);
frame.add(label);

frame.setVisible(true);
}
}
```

14. How do you create Text Fields in Swing?


Answer: Use the `JTextField` class to create a single-line text input field.

Example:
```java
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TextFieldExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Text Field Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextField textField = new JTextField("Enter text here");


frame.add(textField);

frame.setVisible(true);
}
}
```

15. How do you create Buttons in Swing?


Answer: Use the `JButton` class to create a button.

Example:
```java
import javax.swing.JButton;
import javax.swing.JFrame;

public class ButtonExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Button Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton button = new JButton("Click Me");


frame.add(button);

frame.setVisible(true);
}
}
```

16. How do you create Check Boxes in Swing?


Answer: Use the `JCheckBox` class to create a checkbox.

Example:
```java
import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class CheckBoxExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Check Box Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JCheckBox checkBox = new JCheckBox("Check me");


frame.add(checkBox);

frame.setVisible(true);
}
}
```

17. How do you create Radio Buttons in Swing?


Answer: Use the `JRadioButton` class to create radio buttons and `ButtonGroup` to group them.

Example:
```java
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class RadioButtonExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Radio Button Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JRadioButton option1 = new JRadioButton("Option 1");


JRadioButton option2 = new JRadioButton("Option 2");

ButtonGroup group = new ButtonGroup();


group.add(option1);
group.add(option2);

frame.add(option1);
frame.add(option2);

frame.setVisible(true);
}
}
```

18. How do you create Combo Boxes in Swing?


Answer: Use the `JComboBox` class to create a drop-down list.

Example:
```java
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class ComboBoxExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Combo Box Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String[] options = { "Option 1", "Option 2", "Option 3" };


JComboBox<String> comboBox = new JComboBox<>(options);

frame.add(comboBox);

frame.setVisible(true);
}
}
```

19. How do you create Tabbed Panes in Swing?


Answer: Use the `JTabbedPane` class to create a tabbed pane.

Example:
```java
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class TabbedPaneExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Tabbed Pane Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTabbedPane tabbedPane = new JTabbedPane();

JPanel panel1 = new JPanel();


panel1.add(new JLabel("This is Tab 1"));
tabbedPane.addTab("Tab 1", panel1);

JPanel panel2 = new JPanel();


panel2.add(new JLabel("This is Tab 2"));
tabbedPane.addTab("Tab 2", panel2);

frame.add(tabbedPane);

frame.setVisible(true);
}
}
```

20. How do you create Scroll Panes in Swing?


Answer: Use the `JScrollPane` class to add scrollable view to components.

Example:
```java
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ScrollPaneExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Scroll Pane Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextArea textArea = new JTextArea(10, 20);


JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane);

frame.setVisible(true);
}
}
```

21. How do you create Trees in Swing?


Answer: Use the `JTree` class to create a tree structure.

Example:
```java
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class TreeExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Tree Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");


DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Node 1");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Node 2");

root.add(node1);
root.add(node2);

JTree tree = new JTree(root);


frame.add(tree);

frame.setVisible(true);
}
}
```

22. How do you create Tables in Swing?


Answer: Use the `JTable` class to create a table.

Example:
```java
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class TableExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Table Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String[] columnNames = { "Name", "Age", "Gender" };


Object[][] data = {
{ "Alice", 30, "Female" },
{ "Bob", 25, "Male" }
};

JTable table = new JTable(data, columnNames);


JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane);

frame.setVisible(true);
}
}
```

You might also like